Search code examples
pythontwisted

twisted dns doesn't work


I'd like to know why the following doesn't work.

from twisted internet import defer, reactor
from twisted.python.failure import Failure
import twisted.names.client

def do_lookup(do_lookup):
    d = twisted.names.client.getHostByName(domain)
    d.addBoth(lookup_done)

def lookup_done(result):
    print 'result:', result
    reactor.stop()

domain = 'twistedmatrix.com'    
reactor.callLater(0, do_lookup, domain) 
reactor.run()

Results in:

result: [Failure instance: Traceback
(failure with no frames): <class
'twisted.names.error.ResolverError'>:
Stuck at response without answers or
delegation ]

Solution

  • Correcting your example to the following, so that it is syntactically valid:

    from twisted.internet import reactor
    import twisted.names.client
    
    def do_lookup(domain):
        d = twisted.names.client.getHostByName(domain)
        d.addBoth(lookup_done)
    
    def lookup_done(result):
        print 'result:', result
        reactor.stop()
    
    domain = 'twistedmatrix.com'
    reactor.callLater(0, do_lookup, domain)
    reactor.run()
    

    I get:

    $ python so-example.py 
    result: 66.35.39.65
    

    So, to answer your question: your local DNS environment is broken, not twisted.names. Or maybe there's a bug. You'll need to track it down further.