Python Twisted

It's a beast, but it's usable. FTP ?, SMTP ?, what else ?

Twisted implements a variety of networking and communication protocols and exposes them all as method-calls on your Python objects. Client and server implementations are provided for various standard protocols, including:

  • HTTP (twisted.web)
  • IMAP, POP, SMTP (twisted.mail)
  • DNS (twisted.names)
  • TLS (core)
  • SSH,
  • Telnet (twisted.conch)
  • IRC, XMPP, OSCAR (twisted.words)
  • Ethernet, IP, TUN/TAP (twisted.pair)
  • NMEA (twisted.positioning)

Note is still Python 2.7 ... maybe ... download for .whl is still 2.7 ...

Recent developments -> ... almost there.

Also see PythonServers#Wokkel extensions to Twisted servers ...

List of Twisted projects on GitHub - - updated Oct 2017

Twisted 17.9.0

Twisted is an event-based framework for internet applications, supporting Python 2.7 and Python 3.3+ [ yeah ! ] ...

... Twisted supports all major system event loops -- select (all platforms), poll (most POSIX platforms), epoll (Linux), kqueue (FreeBSD, OS X), IOCP (Windows), and various GUI event loops (GTK+2/3, QT, wxWidgets).

Still a few Python 3 issues, but full support for OSX now. Working the Twisted Python 2.7 asynchronous events into the new Python 3.3 asyncio model must have been challenging. Good job, folks !

This module provides infrastructure for writing single-threaded concurrent code using coroutines, multiplexing I/O access over sockets and other resources, running network clients and servers, and other related primitives. Here is a more detailed list of the package contents: ...

... transport and protocol abstractions (similar to those in Twisted);

Twisted also supports many common network protocols, including SMTP, POP3, IMAP, SSHv2, and DNS.

Projects Using Twisted

Limited Documentation for Twisted - Twisted Introduction

This multi-part series introduces Asynchronous Programming and the Twisted networking framework.

Simple Examples

IMAP4 Client

Twisted includes a sophisticated IMAP4 client library.

import sys

from twisted.internet import protocol, defer, endpoints, task
from twisted.mail import imap4
from twisted.python import failure

def main(reactor, username="alice", password="secret",
    endpoint = endpoints.clientFromString(reactor, strport)
    factory = protocol.Factory()
    factory.protocol = imap4.IMAP4Client
        client = yield endpoint.connect(factory)
        yield client.login(username, password)
        info = yield client.fetchEnvelope(imap4.MessageSet(1))
        print 'First message subject:', info[1]['ENVELOPE'][1]
        print "IMAP4 client interaction failed"

# This API requires Twisted 12.3 or later, or a trunk checkout:
task.react(main, sys.argv[1:])

Publish/Subscribe Server

Here's a simple publish/subscribe server, where clients see all messages posted by other clients:

from twisted.internet import reactor, protocol, endpoints
from twisted.protocols import basic

class PubProtocol(basic.LineReceiver):
    def __init__(self, factory):
        self.factory = factory

    def connectionMade(self):

    def connectionLost(self, reason):

    def lineReceived(self, line):
        for c in self.factory.clients:
            c.sendLine("<{}> {}".format(self.transport.getHost(), line))

class PubFactory(protocol.Factory):
    def __init__(self):
        self.clients = set()

    def buildProtocol(self, addr):
        return PubProtocol(self)

endpoints.serverFromString(reactor, "tcp:1025").listen(PubFactory())

Proxy Server

Need something lighter and more dumbed down than enterprise-ready Nginx.

Found this example, but ...

# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.

This example demonstrates a very simple HTTP proxy.

    $ python

Then configure your web browser to use localhost:8080 as a proxy, and visit a
URL (This is not a SOCKS proxy). When browsing in this configuration, this
example will proxy connections from the browser to the server indicated by URLs
which are visited.

See also for a proxy with additional features.

from twisted.web import proxy, http
from twisted.internet import reactor

class ProxyFactory(http.HTTPFactory):
    def buildProtocol(self, addr):
        return proxy.Proxy()

reactor.listenTCP(8080, ProxyFactory())



txmongo is a Python/Twisted driver for MongoDB that implements the wire protocol on non-blocking sockets. The API derives from the original pymongo.

Latest release 2015-01-23, tar.gz size 34KB

Requires Twisted, pymongo

In practice, pymongo dependencies other than pymongo.error are limited in the connection module and are very thin at that. parse_uri and auth._auth_key used once each... the rest is a reimplementation using Twisted callbacks.

In short: pymongo calls are blocking, when ran they freeze the twisted engine until the call returns, which is randomly destructive to twisted internal state because it's the opposite of what twisted is designed for.

Instead you should look for a twisted comparable driver such as tx-mongo. tx-mongo has a smaller audience then pymongo ...


This multi-part series introduces Asynchronous Programming and the Twisted networking framework ...

Also See



Search wiki for 'twisted'

Last modified 6 weeks ago Last modified on 10/27/2017 01:36:01 PM