{{{ #!div class="column-layout" style="display: table;" {{{ #!div class="leftcol" style="display: table-cell" {{{ #!div class="leftcolcol" {{{ #!div id="download" [[Image(/chrome/common/download_icon.png, id=download_icon, title=Downloads, alt="", link=wiki:Downloads)]] = Downloads = == Source == * [[ProjectVersion(https://pypi.python.org/packages/source/T/Twisted/Twisted-%(base)s.tar.bz2#md5=%(md5)s Twisted %(base)s tar.bz2)]] == Windows (32-bit) == * [[ProjectVersion(https://pypi.python.org/packages/2.7/T/Twisted/Twisted-%(base)s.win32-py2.7.msi Twisted %(base)s for Python 2.7)]] == Other == * [wiki:Downloads More downloads] == Dependencies == * [https://pypi.python.org/pypi/zope.interface#download zope.interface] }}} {{{ #!div class="side-panel twisted-news" == [http://labs.twistedmatrix.com/ Twisted News] == {{{ #!div id="twisted-news" class="feed-content" [[Image(/chrome/common/throbber.gif, link=, class=throbber, title="", alt="")]] }}} }}} {{{ #!div class="side-panel donations" == [WhyDonate Donate to Twisted] == Donations are [wiki:TwistedSoftwareFoundation#Background tax-deductible]. {{{ #!html
PayPal
Flattr us!
}}} = [wiki:TwistedSponsors Twisted Sponsors] = Participate in the (newly-revamped) [wiki:TwistedSoftwareFoundation#BenefitsofSponsorship Twisted Project Sponsorship Program]! For [wiki:TwistedSoftwareFoundation#SponsorshipLevels Silver Sidewinder and higher-level] sponsors, we will display your logo here on the front page for one year. {{{ #!div class="twisted-sponsors" === Silver Sidewinder === [[Image(TSF/SponsorLogos:dreamhost.gif, title=DreamHost, link=http://dreamhost.com/?q=twisted)]] }}} === Bronze Sponsors (under previous program) === [[Image(TSF/SponsorLogos:flowroute-logo.gif, title=flowroute, link=http://www.flowroute.com/)]] [[Image(TSF/SponsorLogos:found_160x80.png, title=FoundIT, link=http://www.found.no/)]] }}} }}} }}} {{{ #!div class="centercol" style="display: table-cell" = What is Twisted? = Twisted is an event-driven networking engine written in Python and licensed under the open source [http://www.opensource.org/licenses/mit-license.php MIT license]. == Code Examples == {{{ #!html }}} {{{ #!div class="tab-content" {{{ #!div class="tab-pane active" id="echoserver" Twisted makes it easy to implement custom network applications. Here's a TCP server that echoes back everything that's written to it: {{{ #!python 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 [http://twistedmatrix.com/documents/current/core/howto/servers.html writing servers], [http://twistedmatrix.com/documents/current/core/howto/clients.html writing clients] and the [http://twistedmatrix.com/documents/current/core/howto/index.html core networking libraries ], including support for SSL, UDP, scheduled events, unit testing infrastructure, and much more. }}} {{{ #!div class="tab-pane" id="webserver" 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: {{{ #!python 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 [http://twistedmatrix.com/documents/current/web/howto/web-in-60/index.html web application development], [http://twistedmatrix.com/documents/current/web/howto/twisted-templates.html templates] and Twisted's [http://twistedmatrix.com/documents/current/web/howto/client.html HTTP client]. }}} {{{ #!div class="tab-pane" id="pubsubserver" Here's a simple publish/subscribe server, where clients see all messages posted by other clients: {{{ #!python 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): self.factory.clients.add(self) def connectionLost(self, reason): self.factory.clients.remove(self) 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()) reactor.run() }}} You can test this out by opening two terminals and doing `telnet localhost 1025` in each, then typing things. }}} {{{ #!div class="tab-pane" id="imap4client" Twisted includes a sophisticated IMAP4 client library. {{{ #!python import sys from twisted.internet import protocol, defer, endpoints, task from twisted.mail import imap4 from twisted.python import failure @defer.inlineCallbacks def main(reactor, username="alice", password="secret", strport="ssl:host=example.com:port=993"): endpoint = endpoints.clientFromString(reactor, strport) factory = protocol.Factory() factory.protocol = imap4.IMAP4Client try: client = yield endpoint.connect(factory) yield client.login(username, password) yield client.select('INBOX') info = yield client.fetchEnvelope(imap4.MessageSet(1)) print 'First message subject:', info[1]['ENVELOPE'][1] except: print "IMAP4 client interaction failed" failure.Failure().printTraceback() # 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 [http://twistedmatrix.com/documents/current/core/howto/endpoints.html#auto7 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 [wiki:Documentation documentation] and [http://twistedmatrix.com/documents/current/api/ API reference]. == Community == [[Image(http://oreilly.com/catalog/covers/twistedadn.s.gif, link=http://www.amazon.com/gp/product/1449326110/ref=as_li_qf_sp_asin_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1449326110&linkCode=as2&tag=twisted-sfconservancy-20, right)]] * Get in touch with the [wiki:TwistedCommunity Twisted community] through [wiki:TwistedCommunity#MailLists email], [http://stackoverflow.com/questions/tagged/twisted Stack Overflow] or [wiki:TwistedCommunity#IRC IRC]; * Learn about the Twisted [wiki:TwistedDevelopment development process] and how to [wiki:ContributingToTwistedLabs contribute]; * Read about [wiki:ProjectsUsingTwisted software using] Twisted and their [wiki:SuccessStories success stories]; * Find out what [wiki:TwistedMatrixLaboratories Twisted Matrix Laboratories] is; * Learn about the [wiki:TwistedSponsors individuals and organisations] that aid Twisted with donations of hardware, software, hosting and other things; * Help improve Twisted on [wiki:Windows Windows]! }}}