Twisted is a framework for writing, testing, and deploying event-driven clients and servers in Python. In my previous Twisted blog post, we explored an architectural overview of Twisted and examples of simple TCP, UDP, SSL, and HTTP echo servers.
While Twisted makes it easy to build servers in just a few lines of Python, you can actually use Twisted to spin up servers with 0 lines of code!
We can accomplish this with
twistd (pronounced twist-dee), a command line utility that ships with Twisted for deploying your Twisted applications. In addition to providing a standardized deployment interface for common production features like daemonization, logging, and authentication,
twistd can use Twisted’s plugin architecture to run flexible servers for a variety of protocols. Here are some examples:
twistd web --port 8000 --path .
Run an HTTP server on port 8000, serving both static and dynamic content out of
the current working directory. Visit
http://localhost:8000 to see the directory listing:
This is a great way to easily host a simple site or blog, share files, or demo website mockups.
twistd dns -v -p 5553 --hosts-file=hosts
Run a DNS server on port 5553, resolving domains out of a file called
hosts in the format of
sudo twistd conch -p tcp:2222
Run an ssh server on port 2222 (assumes ssh keys have been set up).
twistd mail -E -H localhost -d localhost=emails
Run an ESMTP POP3 server, accepting email for
localhost and saving it to the
You can also use
twistd to spin up an FTP server, SOCKS proxy, XMPP router, and more. See
twistd -h for the full list of protocols with off-the-shelf server implementations.