DNS lookups using Python

Using the PyDNS library found in Python Package Index and hosted at SourceForge, some code snippets for usage:

import DNS
reqobj = DNS.Request(name="blog.rayfoo.info")
resp = reqobj.req()

for i in resp.answers: print i
{'name': 'blog.rayfoo.info', 'data': '',
'typename': 'A', 'classstr': 'IN', 'ttl': 1790, 'type': 1,
'class': 1, 'rdlength': 4}

PDG.py 1.0 - blog.rayfoo.info 1
;; options: recurs
;; got answer:
;; -HEADER- opcode 0, status NOERROR, id 60299
;; flags: qr rd ra; Ques: 1, Ans: 1, Auth: 0, Addit: 0
;;      blog.rayfoo.info, type = A, class = IN

blog.rayfoo.info        1790    A



;; Total query time: 367 msec
;; To SERVER: <sanitized>
;; WHEN: Sat Jul 31 11:06:24 2010

>>> resp.args
{'protocol': 'udp', 'name': 'blog.rayfoo.info',
'server': 'sanitized', 'rd': 1, 'opcode': 0,
'timeout': 30, 'timing': 1, 'elapsed': 367.52486228942871,
'qtype': 1, 'port': 53}

2 thoughts on “DNS lookups using Python”

  1. Thanks for giving this snippet! There is a real lack of documentation for the PyDNS module… ;/ Might ask the author if he’ll allow me to modify the docs and contribute nicer documentation.

    1. You’re welcome 🙂 Took a couple of hours of doing dir() on the different objects before I had a better idea on how to use it, glad it helped heh.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s