Easily invoke common protocols with Twisted

Spin up Python-friendly services with 0 lines of code

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

Learn from experts at this free, live webcast.

Laura Klein

Twisted Python:
the engine of your Internet


Presented by Jessica McKellar
Date: Jun 18, 2013 at 10am PT

Register now

Run a DNS server on port 5553, resolving domains out of a file called hosts in the format of /etc/hosts.

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 emails directory.

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.

tags: , , , , , , ,