[Twisted-Python] Beginner's question about names and callbacks

Tim Allen screwtape at froup.com
Mon Jan 26 01:46:20 EST 2009


On Mon, Jan 26, 2009 at 01:06:37AM -0500, Adan Broderick wrote:
> I'm trying to write a program that will call my own function whenever a
> query to a nameserver times out. I have figured out how to run the DNS
> server. And I also know how to use filterAnswers(). But how do I catch the
> "Failure: twisted.names.error.DNSQueryTimeoutError:" messages by adding code
> to the resolver I've constructed?

I'm not familiar with twisted.names, but looking at the source to
client.Resolver, it seems that something winds up calling _lookup() with
a timeout, which passes it off to the queryUDP() and sets up the basic
result handling. If queryUDP() returns a result, it goes to filterAnswers,
which checks for truncation and re-sends with queryTCP().

Since the actual deferreds that deal with the timeouts are returned from
queryUDP() and queryTCP(), it looks like you should be overriding those
methods and capturing the results instead. Something like this:

class MyResolver(client.Resolver):

    def _handle_query_timeout(self, fail):
	fail.trap(DNSQueryTimeoutError)
	print "Got an error:", fail.getErrorMessage()

    def queryUDP(self, *args, **kwargs):
	d = client.Resolver.queryUDP(self, *args, **kwargs)
	d.addErrback(self._handle_query_timeout)
	return d

    def queryTCP(self, *args, **kwargs):
	d = client.Resolver.queryTCP(self, *args, **kwargs)
	d.addErrback(self._handle_query_timeout)
	return d




More information about the Twisted-Python mailing list