DNS/MX Lookup?

Andrew Bennetts andrew-pythonlist at puzzling.org
Mon Jun 16 10:55:22 CEST 2003


On Mon, Jun 16, 2003 at 07:43:02AM +0000, Chris White wrote:
> Well, I tried the python-dns module that exists out there, but it's not
> working so for.
> 
> It would be nice if there was a socket function to pull up info about mx
> servers for a domain name.  If anyone knows a working with (yes, here it
> comes) windoze operating system that simply uses the default nameserver or
> lets me set it for that matter (the later would be more prefered, as it
> would make it more cross platform compatible IMHO). 
> 
> Also, if anyone knows some good easy to understand (snicker) RFC's for DNS
> Querying (or some page that breaks it down) that would be great as well.

It's probably overkill, but you can do it with Twisted:

---- mxquery.py ----
#!/usr/bin/env python
"""Command-line util for resolving MXs.

Usage:
    mxquery.py example.com              # Uses DNS servers in /etc/resolv.conf
or
    mxquery.py example.com 1.2.3.4      # Uses DNS server 1.2.3.4

Adapted from doc/examples/dns-service.py in the Twisted distribution.
"""

from twisted.names import client
from twisted.internet import reactor
import sys

def printAnswer((answers, auth, add)):
    if not len(answers):
        print 'No answers'
    else:
        print '\n'.join([str(x.payload) for x in answers])
    reactor.stop()

def printFailure(arg):
    print "error: could not resolve:", arg
    reactor.stop()

if len(sys.argv) not in (2,3):
    sys.stderr.write('%s: usage:\n' % sys.argv[0] +
                     '  %s domain [dns server]\n' % sys.argv[0])
    sys.exit(1)
   
domain = sys.argv[1]
if len(sys.argv) == 2:
    resolver = client.Resolver('/etc/resolv.conf')
else:
    resolver = client.Resolver(servers=[(sys.argv[2],53)])

d = resolver.lookupMailExchange('%s' % (domain,), [3])
d.addCallbacks(printAnswer, printFailure)

reactor.run()
----

-Andrew.






More information about the Python-list mailing list