Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I figured this out.. You have to use client.lookupAllRecords and then test the type of records returned..</p> <pre><code>#!/usr/bin/python from twisted.internet import defer, reactor from twisted.names import client, dns def got_arg(*args): for a in args[0][0]: if a.payload.TYPE == dns.A: print 'A - ipv4',a.payload elif a.payload.TYPE == dns.AAAA: print 'AAAA - ipv6',a.payload def get_arg(arg): d = client.lookupAllRecords('www.google.com').addCallback(got_arg) if __name__ == '__main__': get_arg('www.google.com') reactor.run() </code></pre> <p><strong>EDIT</strong></p> <p>I realize this solution is insufficient - specifically in the case where the first batch of records only contains a single CNAME pointing to another address..</p> <p>The reason for this is that the twisted.names.common.extractRecord() looks first for ipv6 addresses and immediately returns them - with no obvious mechanism to override this behavior. </p> <p>So, I put together a hacky solution for this so that we don't even try to resolve down the chain on ipv6 addresses by making an ugly monkey patch..</p> <pre><code>#!/usr/bin/python import socket from twisted.names import dns from twisted.names import common def myExtractRecord(resolver, name, answers, level=10): if not level: return None for r in answers: if r.name == name and r.type == dns.A: return socket.inet_ntop(socket.AF_INET, r.payload.address) for r in answers: if r.name == name and r.type == dns.CNAME: result = myExtractRecord( resolver, r.payload.name, answers, level - 1) if not result: return resolver.getHostByName( str(r.payload.name), effort=level - 1) return result # No answers, but maybe there's a hint at who we should be asking about # this for r in answers: if r.type == dns.NS: from twisted.names import client r = client.Resolver(servers=[(str(r.payload.name), dns.PORT)]) return r.lookupAddress(str(name) ).addCallback( lambda (ans, auth, add): myExtractRecord(r, name, ans + auth + add, level - 1)) common.extractRecord = myExtractRecord </code></pre> <p>I put that in dnsclient.py and use it in my main prog by:</p> <pre><code>from twisted.names import client import dnsclient </code></pre> <p>If there's a more elegant solution for this, I'm all ears. I think this would be more friendly if getHostByName accepted a mask of dns.A,dns.AAAA,dns.A &amp;&amp; dns.AAAA for figuring out which records to walk the tree of.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload