I am trying to create a database-driven DNS server (in particular, to process only MX records and push everything else up) in Twisted using Python 2.7. The code below works (in terms of getting the result), but does not work asynchronously. Instead, any DNS queries entering the system block the entire program from any other queries until a response is received to the first. We need to scale this, and at the moment we cannot understand where we did wrong. If someone has a working example to share or see a problem, we will be infinitely grateful.
import settings import db from twisted.names import dns, server, client, cache from twisted.application import service, internet from twisted.internet import defer class DNSResolver(client.Resolver): def __init__(self, servers): client.Resolver.__init__(self, servers=servers) @defer.inlineCallbacks def _lookup_mx_records(self, hostname, timeout):
EDIT NO. 1
blakev suggested that I cannot use the generator correctly (which is certainly possible). But if I simplify this a bit so as not to even use the database, I still cannot process more than one DNS query at a time. To test this, I disabled the class. The following is my entire executable test file. Even in this heavily stripped-down version of my server, Twisted does not accept any requests until the first one appears.
import sys import logging from twisted.names import dns, server, client, cache from twisted.application import service, internet from twisted.internet import defer class DNSResolver(client.Resolver): def __init__(self, servers): client.Resolver.__init__(self, servers=servers) def lookupMailExchange(self, name, timeout=None): """ The twisted function which is called when an MX record lookup is requested. :param name: The domain name being queried for (eg example.org). :param timeout: Time in seconds to wait for the query response. (optional, default: None) :return: A DNS response for the record query. """ logging.critical("Query for " + name) return defer.succeed([ (dns.RRHeader(name, dns.MX, dns.IN, 600, dns.Record_MX(1, "10.0.0.9", 600)),), (), () ])