Debian Codegraph @

examples/blockingdns.py

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #!/usr/bin/python
2 """
3 Do a DNS lookup using Twisted's APIs.
4 """
5 from __future__ import print_function
6
7 # The Twisted code we'll be using:
8 from twisted.names import client
9
10 from crochet import setup, wait_for
11 setup()
12
13
14 # Crochet layer, wrapping Twisted's DNS library in a blocking call.
15 @wait_for(timeout=5.0)
16 def gethostbyname(name):
17 """Lookup the IP of a given hostname.
18
19 Unlike socket.gethostbyname() which can take an arbitrary amount of time
20 to finish, this function will raise crochet.TimeoutError if more than 5
21 seconds elapse without an answer being received.
22 """
23 d = client.lookupAddress(name)
24 d.addCallback(lambda result: result[0][0].payload.dottedQuad())
25 return d
26
27
28 if __name__ == '__main__':
29 # Application code using the public API - notice it works in a normal
30 # blocking manner, with no event loop visible:
31 import sys
32 name = sys.argv[1]
33 ip = gethostbyname(name)
34 print(name, "->", ip)
35