Twisted Names

Twisted Names is both a domain name server as well as a client resolver library. Twisted Names comes with an "out of the box" nameserver which can read most BIND-syntax zone files as well as a simple Python-based configuration format. Twisted Names can act as an authoritative server, perform zone transfers from a mastfrer to act as a secondary, act as a caching nameserver, or any combination of these. Twisted Names' client resolver library provides functions to query for all commonly used record types as well as a replacement for the blocking gethostbyname() function provided by the Python stdlib socket module.


Running a caching DNS server is as easy as running twistd. See twistd dns --help for more details

$ twistd -n dns --port 5354 --recursive --cache

This is how you can do some name resolution with Twisted Names.

$ python -m twisted.conch.stdio
>>> from twisted.names import client
>>> client.getHostByName('')
<Deferred at 0x9dcfbac current result: ''>
>>> client.lookupMailExchange('')
<Deferred at 0x90eb0ec current result: ([<RR type=MX class=IN ttl=86303s auth=False>], [], [<RR type=A class=IN ttl=86304s auth=False>])>

Twisted has pluggable resolvers, the default resolver reads /etc/resolv.conf (Or C:\windows\system\hosts.txt on windows), /etc/hosts and has a cache. You can create an alternate resolver to resolve names. Such as in the following example.

$ python -m twisted.conch.stdio
>>> from twisted.names import client
>>> resolver = client.createResolver(servers=[('', 53), ('', 53)], hosts='alternate_hosts_file')
>>> resolver.getHostByName('')
<Deferred at 0x9dcfbac current result: ''>

You can affect all future name resolution in the reactor and from the twisted.names.client by installing a custom resolver into the reactor.

from twisted.internet import reactor
from twisted.names import client
reactor.installResolver(client.createResolver(servers=[('', 53), ('', 53)], hosts='alternate_hosts_file'))


See the Downloads page.


Documentation is available for Twisted Names. An API reference of Twisted and all subprojects is also available.


Subscribe to the twisted-python mailing list or visit the #twisted channel on to ask questions.

Report a bug or request a feature using the issue tracker (registration required), or browse Twisted Names tickets.

Last modified 3 months ago Last modified on 07/06/21 07:58:46