]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/unbound/libunbound/python/doc/examples/example2.rst
Upgrade Unbound to 1.6.0. More to follow.
[FreeBSD/FreeBSD.git] / contrib / unbound / libunbound / python / doc / examples / example2.rst
1 .. _example_setup_ctx:
2
3 Lookup from threads
4 ===================
5
6 This example shows how to use unbound module from a threaded program.
7 In this example, three lookup threads are created which work in background.
8 Each thread resolves different DNS record.
9
10 Source code
11 -----------
12
13 ::
14
15     #!/usr/bin/python
16     from unbound import ub_ctx, RR_TYPE_A, RR_CLASS_IN
17     from threading import Thread
18
19     ctx = ub_ctx()
20     ctx.resolvconf("/etc/resolv.conf")
21
22     class LookupThread(Thread):
23         def __init__(self,ctx, name):
24             Thread.__init__(self)
25             self.ctx = ctx
26             self.name = name
27
28         def run(self):
29             print "Thread lookup started:",self.name
30             status, result = self.ctx.resolve(self.name, RR_TYPE_A, RR_CLASS_IN)
31             if status == 0 and result.havedata:
32                 print "  Result:",self.name,":", result.data.address_list
33
34     threads = []
35     for name in ["www.fit.vutbr.cz","www.vutbr.cz","www.google.com"]:
36         thread = LookupThread(ctx, name)
37         thread.start()
38         threads.append(thread)
39
40     for thread in threads:
41         thread.join()