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