DNS from Python (was Re: Subprocess puzzle and two questions)

Aahz aahz at pythoncraft.com
Thu Nov 15 06:42:58 CET 2012

In article <mailman.3700.1352930072.27098.python-list at python.org>,
Chris Angelico  <rosuav at gmail.com> wrote:
>On Thu, Nov 15, 2012 at 3:20 AM, Roy Smith <roy at panix.com> wrote:
>> My first thought to solve both of these is that it shouldn't be too
>> hard to hand-craft a minimal DNS query and send it over UDP.  Then, I
>> hunted around a bit and found that somebody had already done that, in
>> spades.  Take a look at http://www.dnspython.org; it might be exactly
>> what's needed here.
>Yeah, that sounds like a good option. I'm slightly surprised that
>there's no way with the Python stdlib to point a DNS query at a
>specific server, but dnspython might be the solution. On the flip
>side, dnspython is dauntingly large; it looks like a full
>implementation of DNS, but I don't see a simple entrypoint that wraps
>it all up into a simple function that can be bracketed with
>time.time() calls (granted, I only skimmed the docs VERY quickly). So
>it may be simpler to hand-craft an outgoing UDP packet once, save it
>as a string literal, send that, and just wait for any response. That
>eliminates all DNS protocolling and just times the round trip.

>From one of my scripts lying around:

    domain = MAILTO.split('@',1)[1]
    server = str(dns.resolver.query(domain, 'MX')[0].exchange)

You'll need to play around a bit to find out what that does, but it
should point you in the right direction.
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"LL YR VWL R BLNG T S"  -- www.nancybuttons.com

More information about the Python-list mailing list