]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/unbound/libunbound/python/doc/examples/example3.rst
Upgrade Unbound to 1.6.0. More to follow.
[FreeBSD/FreeBSD.git] / contrib / unbound / libunbound / python / doc / examples / example3.rst
1 .. _example_asynch:
2
3 Asynchronous lookup
4 ===================
5
6 This example performs the name lookup in the background. 
7 The main program keeps running while the name is resolved. 
8
9 Source code
10 -----------
11
12 ::
13
14         #!/usr/bin/python
15         import time
16         import unbound
17         
18         ctx = unbound.ub_ctx()
19         ctx.resolvconf("/etc/resolv.conf")
20         
21         def call_back(my_data,status,result):
22                 print "Call_back:", my_data
23                 if status == 0 and result.havedata:
24                         print "Result:", result.data.address_list
25                         my_data['done_flag'] = True
26         
27         
28         my_data = {'done_flag':False,'arbitrary':"object"}
29         status, async_id = ctx.resolve_async("www.seznam.cz", my_data, call_back, unbound.RR_TYPE_A, unbound.RR_CLASS_IN)
30                 
31         while (status == 0) and (not my_data['done_flag']):
32                 status = ctx.process()
33                 time.sleep(0.1)
34         
35         if (status != 0):
36                 print "Resolve error:", unbound.ub_strerror(status)
37
38 The :meth:`unbound.ub_ctx.resolve_async` method is able to pass on any Python
39 object. In this example, we used a dictionary object ``my_data``.