Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
Twisted DNS server!
# twistd -y
import socket
from twisted.internet.protocol import Factory, Protocol
from twisted.internet import reactor
from twisted.names import dns
from twisted.names import client, server
TO = ''
TTL = 60
class DNSServerFactory(server.DNSServerFactory):
def gotResolverResponse(self, (ans, auth, add), protocol, message, address):
qname = message.queries[0]
if CHANGE in qname:
for answer in ans:
if answer.type != dns.A:
if CHANGE not in
answer.payload.address = socket.inet_aton(TO)
answer.payload.ttl = TTL
args = (self, (ans, auth, add), protocol, message, address)
return server.DNSServerFactory.gotResolverResponse(*args)
verbosity = 0
resolver = client.Resolver(servers=[('', 53)])
factory = DNSServerFactory(clients=[resolver], verbose=verbosity)
protocol = dns.DNSDatagramProtocol(factory)
factory.noisy = protocol.noisy = verbosity
reactor.listenUDP(53, protocol)
reactor.listenTCP(53, factory)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.