Problem with DNS resolver library -- pydns

Dave Brueck dave at pythonapocrypha.com
Mon Oct 14 15:13:24 EDT 2002


On Mon, 14 Oct 2002, VanL wrote:

> Has anyone used the pynds resolver libraries?  I think I must be missing 
> something, because I'm just not getting out what I think that I should get.

I've been using the version that is (was?) in the Demo directory (it has 
since become its own SF project, I think). I'm not using the highest-level 
interface though. Here's what I'm doing:

        m = dnslib.Mpacker()
        m.addHeader(0, 0, dnsopcode.QUERY, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)
        m.addQuestion(hostname, dnstype.A, dnsclass.IN)
        sock.send(m.getbuf())

(where sock is a UDP socket that has been "connected" to the DNS, hostname 
is the host you're looking up)

To read the response I do:

        msg = dnslib.Munpacker(data)
        id, qr, opcode, aa, tc, rd, ra, z, rcode, qdcount, \
            ancount, nscount, arcount = msg.getHeader()
        ret = []
        minTTL = sys.maxint # We get back TTLs for each IP, but just use 
the minimum of them all

        if rcode == 0: # > 0 = error
            # Skip over questions
            for i in range(qdcount):
                msg.getQuestion()

            # Extract the IP addresses
            for i in range(ancount):
                name, rtype, klass, ttl, rdlength = msg.getRRheader()
                if rtype == dnstype.A and hasattr(msg, 'getAdata'):
                    ret.append(msg.getAdata())
                    if ttl < minTTL:
                        minTTL = ttl
                else:
                    # Just skip over it
                    msg.getbytes(rdlength)

The reason I'm not using the higher-level API is simply because of the 
socket framework I'm using - AFAIK that API works fine too so maybe the 
only difference is that I'm calling getAdata? Also, the above code is from 
a DNS cache module so that's why I'm worrying about TTL values.

HTH,
Dave





More information about the Python-list mailing list