I attended an introduction to Twister, a Python asynchronous engine, on July 13, 2012, at Rackspace’s new office. This asynchronous engine is a modular library that allows different types of processes to perform non-block operations such as handling network traffic, running web pages, hosting IRC channels, and running a DNS server. Its nearest and most advertised equivalent is Node.js on the Javascript language. Unlike Node.js Twister has been around for around 10 years and the Python language 20 years. Originally developed by Cliff, a team of experienced developers including people at Rackspace maintain the code base and migrate it to the new Python 3 language.
Twister is designed such that code is written as functions that respond to events happening. It is a low-layer library, which does not provide web framework functionality. It cannot be compared to Django or Ruby on Rails, see previous blog post.
Although Twisted provides functionality to serve web pages, apache
is a better web server and bind
is a better DNS server. Twisted’s selling feature is interconnecting these functions in a single process because of Twisted’s modular architecture. Other servers built ground up from C and have their own idiosyncratic connections.
Some of Twisted’s functionality are listed below.
Web Server
1 |
twistd -n web --port 8080 --path . twistd -n web --port 8080 --wsgi your.app
|
wsgi – standard python interface
twistd = wsgi container
IRC Channel
1 |
twistd -n words --irc-port tcp:6667 --auth memory:me:pw --group mygroup
|
DNS Server
1 |
twistd -n dns -p 5553 --hosts-file=/etc/hosts twistd -n dns -p 5553 --pyzone=my.py twistd -n dns -p 5553 --bindzone=my.zone
|
1 |
(digg - custom)
|
Conch?
1 |
sudo twistd -n conch -p tcp:2222
|
Mail Server
1 |
sudo twistd -n mail -E -H localhost -d localhost=emails
|
TCP Write Back Example
This example writes the same TCP stream back as it is received.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
from twisted.internet.protocol import (Protocol, Factory)
class Echo(Protocol):
def dataReceived(self, data):
self.transport.write(data)
class EchoFactory(Factory):
def buildProtocol(self, addr):
return Echo()
from twisted.internet import reactor
reactor.listenTCP(8765, EchoFactory())
reactor.run() # call once only
# exit
|
Installation
To ensure that twisted is installed correctly, the following unit tests are run:
trial twisted