| •1 |
|
| 2 |
""" |
| 3 |
Do a DNS lookup using Twisted's APIs. |
| 4 |
""" |
| 5 |
from __future__ import print_function |
| 6 |
|
| 7 |
|
| 8 |
from twisted.names import client |
| 9 |
|
| 10 |
from crochet import setup, wait_for |
| 11 |
setup() |
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 30 |
|
| 31 |
import sys |
| 32 |
name = sys.argv[1] |
| 33 |
ip = gethostbyname(name) |
| 34 |
print(name, "->", ip) |
| 35 |
|