DNS from Python


Python also includes the gethostbyname and gethostbyaddr calls and puts them in the built-in socket module. The gethostbyname function returns a string containing the IP address of the host :

 #!/usr/bin/python from socket import * ... "Look up the IP number of the given host" try: ip = gethostbyname(host) except: print host,"does not resolve" else: print "The address of",host,"is",ip 

Other bindings also exist for Python. Please see the vaults of Parnassus at http://www.vex.net/parnassus/. There you can find bindings for the adns library for asynchronous DNS queries, a DNS server written in Python (!), and a DNS-only interface for resolving names. The latter is not documented, but the test suite documents it quite well. The following is an example :

 #!/usr/bin/python import DNS # automatically load nameserver(s) from /etc/resolv.conf # (works on unix - on others, YMMV) DNS.ParseResolvConf() r = DNS.DnsRequest(qtype='ANY') res = r.req("http://www.microsoft.com",qtype='A') # res.answers is a list of dictionaries of answers print len(res.answers),'different A records' # each of these has an entry for 'data', which is the result. print map(lambda x:x['data'], res.answers) 


The Concise Guide to DNS and BIND
The Concise Guide to DNS and BIND
ISBN: 0789722739
EAN: 2147483647
Year: 1999
Pages: 183

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net