wiki:WikiStart

Version 214 (modified by itamar, 2 years ago) (diff)

Switch to tabbed code examples

Downloads

Source

  • Error: Macro ProjectVersion(http://pypi.python.org/packages/source/T/Twisted/Twisted-%(base)s.tar.bz2#md5=%(md5)s Twisted %(base)s tar.bz2) failed
    list index out of range

Windows (32-bit)

Other

Dependencies

Donate to Twisted

Donations are tax-deductible.

PayPal

Twisted Sponsors

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

Silver

appropriate solutions DreamHost logo

Bronze

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):
        self.transport.write(data)

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

reactor.listenTCP(1234, EchoFactory())
reactor.run()

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()))
reactor.run()

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):
    delimiter = "\n"

    def __init__(self, factory):
        self.factory = factory

    def connectionMade(self):
        self.factory.clients.add(self)

    def connectionLost(self, reason):
        self.factory.clients.remove(self)

    def lineReceived(self, line):
        message = "<%s> %s\n" % (self.transport.getHost(), line)
        for c in self.factory.clients:
            if c != self:
                c.transport.write(message)


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

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


if __name__ == '__main__':
    reactor.listenTCP(1025, PubFactory())
    reactor.run()

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.

Community

http://oreilly.com/catalog/covers/twistedadn.s.gif