Version 224 (modified by Jean-Paul Calderone, 9 years ago) (diff)

Update the amazon link to 2nd edition of the book and to use SFC provided affiliate id



  • Error: Macro ProjectVersion( Twisted %(base)s tar.bz2) failed
    Error loading Twisted version information

Windows (32-bit)

  • Error: Macro ProjectVersion( Twisted %(base)s for Python 2.7) failed
    Error loading Twisted version information
  • Error: Macro ProjectVersion( Twisted %(base)s for Python 2.6) failed
    Error loading Twisted version information



Donate to Twisted

Donations are tax-deductible.

Flattr us!

Twisted Sponsors

Become a 2012 sponsor today! For any donation above the bronze level, we will display your logo here on the front page.


appropriate solutions DreamHost logo


Dyn, Inc.

What is Twisted?

Twisted is an event-driven networking engine written in Python and licensed under the open source MIT license.

Code Examples

Twisted makes it easy to implement custom network applications. Here's a TCP server that echoes back everything that's written to it:

from twisted.internet import protocol, reactor

class Echo(protocol.Protocol):
    def dataReceived(self, data):

class EchoFactory(protocol.Factory):
    def buildProtocol(self, addr):
        return Echo()

reactor.listenTCP(1234, EchoFactory())

Learn more about writing servers, writing clients and the core networking libraries , including support for SSL, UDP, scheduled events, unit testing infrastructure, and much more.

Twisted includes an event-driven web server. Here's a sample web application; notice how the resource object persists in memory, rather than being recreated on each request:

from twisted.web import server, resource
from twisted.internet import reactor

class HelloResource(resource.Resource):
    isLeaf = True
    numberRequests = 0
    def render_GET(self, request):
        self.numberRequests += 1
        request.setHeader("content-type", "text/plain")
        return "I am request #" + str(self.numberRequests) + "\n"

reactor.listenTCP(8080, server.Site(HelloResource()))

Learn more about web application development, templates and Twisted's HTTP client.

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

from twisted.internet import reactor, protocol
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)

reactor.listenTCP(1025, PubFactory())

You can test this out by opening two terminals and doing telnet localhost 1025 in each, then typing things.

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:])

Give this a try, supplying your IMAP4 username, password, and client endpoint description for your IMAP4 server. You'll see the subject of the first message in your mailbox printed.

See the TwistedMail documentation for more information.

More Protocols

Twisted also supports many common network protocols, including SMTP, POP3, IMAP, SSHv2, and DNS. For more information see our documentation and API reference.