File: example3.rst

package info (click to toggle)
unbound 1.6.0-2~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 20,444 kB
  • sloc: ansic: 79,862; sh: 5,040; yacc: 1,900; makefile: 1,315; python: 1,302; perl: 141
file content (39 lines) | stat: -rw-r--r-- 1,002 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
.. _example_asynch:

Asynchronous lookup
===================

This example performs the name lookup in the background. 
The main program keeps running while the name is resolved. 

Source code
-----------

::

	#!/usr/bin/python
	import time
	import unbound
	
	ctx = unbound.ub_ctx()
	ctx.resolvconf("/etc/resolv.conf")
	
	def call_back(my_data,status,result):
		print "Call_back:", my_data
		if status == 0 and result.havedata:
			print "Result:", result.data.address_list
			my_data['done_flag'] = True
	
	
	my_data = {'done_flag':False,'arbitrary':"object"}
	status, async_id = ctx.resolve_async("www.seznam.cz", my_data, call_back, unbound.RR_TYPE_A, unbound.RR_CLASS_IN)
	        
	while (status == 0) and (not my_data['done_flag']):
		status = ctx.process()
		time.sleep(0.1)
	
	if (status != 0):
		print "Resolve error:", unbound.ub_strerror(status)

The :meth:`unbound.ub_ctx.resolve_async` method is able to pass on any Python
object. In this example, we used a dictionary object ``my_data``.