]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/unbound/libunbound/python/doc/examples/example3.rst
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / unbound / libunbound / python / doc / examples / example3.rst
1 .. _example_asynch:
2
3 ==============================
4 Asynchronous lookup
5 ==============================
6
7 This example performs the name lookup in the background. 
8 The main program keeps running while the name is resolved. 
9
10 ::
11
12         #!/usr/bin/python
13         import time
14         import unbound
15         
16         ctx = unbound.ub_ctx()
17         ctx.resolvconf("/etc/resolv.conf")
18         
19         def call_back(my_data,status,result):
20                 print "Call_back:", my_data
21                 if status == 0 and result.havedata:
22                         print "Result:", result.data.address_list
23                         my_data['done_flag'] = True
24         
25         
26         my_data = {'done_flag':False,'arbitrary':"object"}
27         status, async_id = ctx.resolve_async("www.seznam.cz", my_data, call_back, unbound.RR_TYPE_A, unbound.RR_CLASS_IN)
28                 
29         while (status == 0) and (not my_data['done_flag']):
30                 status = ctx.process()
31                 time.sleep(0.1)
32         
33         if (status != 0):
34                 print "Resolve error:", unbound.ub_strerror(status)
35
36 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`.