CINXE.COM

Twisted

<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Twisted</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="icon" type="image/x-icon" href="assets/images/favicon.ico"> <link href="vendor/css/bootstrap-v4.5.3.min.css" rel="stylesheet" type="text/css" media="all" /> <link href="vendor/css/bootstrap-icons.css" rel="stylesheet" type="text/css" media="all" /> <link href="vendor/css/default.min.css" rel="stylesheet" type="text/css" media="all" /> <link href="assets/css/style.css" rel="stylesheet" type="text/css" media="all" /> </head> <body> <header> <div class="navbar-container layer-2"> <nav class="navbar navbar-expand-lg justify-content-between navbar-light shadow"> <div class="container"> <div class="col flex-fill px-0 d-flex justify-content-between"> <a class="navbar-brand" href="/" title="Twisted"> <img src="assets/images/twisted-logo-small.svg" alt="Twisted" class="logo"/> <h1 class="d-inline align-middle">Twisted</h1> </a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation" > <span class="navbar-toggler-icon"></span> </button> </div> <div class="collapse navbar-collapse"> <ul class="navbar-nav mr-auto"> <li class="nav-item"> <a class="nav-link" href="https://github.com/twisted/trac-wiki-archive/blob/trunk/FrequentlyAskedQuestions.mediawiki"> FAQ </a> </li> <li class="nav-item"> <a class="nav-link" href="https://docs.twisted.org/"> Docs </a> </li> <li class="nav-item"> <a class="nav-link" href="https://pypi.org/project/Twisted/"> Download </a> </li> <li class="nav-item"> <a class="nav-link" href="#about"> About </a> </li> <li class="nav-item"> <a class="nav-link" href="#donate"> Donate </a> </li> </ul> </div> </div> </nav> </div> </header> <section class="pb-2"> <img src="assets/images/ribbon.svg" class="ribbon d-none d-md-block"> <div class="container"> <div class="row justify-content-center text-center mb-2 layer-2"> <div class="col-9 col-xl-12"> <h1 class="display-4 font-weight-bold"> An event-driven networking engine </h1> <h2> Written in Python </h2> <p class="lead">Licensed under the open source MIT License</p> <div class="d-flex flex-md-row flex-column justify-content-center align-items-center my-4"> <a href="https://github.com/twisted/twisted" class="btn btn-lg btn-outline-dark mx-3 mb-2 mb-md-0"> <i class="bi bi-github"></i> <span>View Github</span> </a> <a href="https://docs.twisted.org/" class="btn btn-lg btn-primary text-white"> View Documentation </a> </div> <a href="https://pypi.org/project/Twisted/"> Check PyPi download page <i class="bi bi-arrow-right-short"></i> </a> </div> </div> <div class="row justify-content-center mt-4"> <div class="col-xl-4 col-lg-8 col-md-8 col-12"> <div> <pre class="bash-code"> <code class="rounded language-bash pt-0"> $ virtualenv try-twisted $ . try-twisted/bin/activate $ pip install twisted[tls] $ twist --help </code> </pre> </div> </div> </div> </div> </section> <section class="p-0 pt-1 pb-4 border-bottom border-secondary premium-sponsors"> <div class="bg-secondary text-white py-1"> <div class="container"> <div class="row align-items-center justify-content-center"> <div class="col-auto"> <span class="h6">Premium Sponsors</span> </div> </div> </div> </div> <div class="container-fluid"> <div class="row align-items-center justify-content-center"> <div class="col-auto"> <a href="https://thinkst.com/" target="_blank"> <img src="assets/images/sponsor-thinkst.svg" alt="Thinkst Canary" title="Thinkst Canary" class="img-fluid"/> </a> </div> </div> </div> </section> <section class="has-divider"> <div class="container"> <div class="row justify-content-center"> <div class="col"> <nav> <div class="nav nav-pills nav-fill" id="nav-tab" role="tablist"> <a class="nav-item nav-link active" id="nav-echo-server-tab" data-toggle="tab" href="#nav-echo-server" role="tab" aria-controls="nav-echo-server" aria-selected="true"> Echo Server </a> <a class="nav-item nav-link" id="nav-web-server-tab" data-toggle="tab" href="#nav-web-server" role="tab" aria-controls="nav-web-server" aria-selected="false"> Web Server </a> <a class="nav-item nav-link" id="nav-publish-tab" data-toggle="tab" href="#nav-publish" role="tab" aria-controls="nav-publish" aria-selected="false"> Publish/Subscribe </a> <a class="nav-item nav-link" id="nav-mail-tab" data-toggle="tab" href="#nav-mail" role="tab" aria-controls="nav-mail" aria-selected="false"> Mail Client </a> <a class="nav-item nav-link" id="nav-ssh-tab" data-toggle="tab" href="#nav-ssh" role="tab" aria-controls="nav-ssh" aria-selected="false"> SSH Client </a> </div> </nav> <div class="tab-content" id="nav-tabContent"> <div class="tab-pane fade show active" id="nav-echo-server" role="tabpanel" aria-labelledby="nav-echo-server-tab"> <p> Twisted makes it easy to implement custom network applications. Here's a TCP server that echoes back everything that's written to it: </p> <pre> <code class="language-python" id="server"> from twisted.internet import protocol, reactor, endpoints class Echo(protocol.Protocol): def dataReceived(self, data): self.transport.write(data) class EchoFactory(protocol.Factory): def buildProtocol(self, addr): return Echo() endpoints.serverFromString(reactor, "tcp:1234").listen(EchoFactory()) reactor.run() </code> </pre> <p> Learn more about <a href="https://docs.twisted.org/en/stable/core/howto/servers.html"> writing servers</a>, <a href="https://docs.twisted.org/en/stable/core/howto/clients.html"> writing clients</a> and the <a href="https://docs.twisted.org/en/stable/core/howto/index.html"> core networking libraries</a>, including support for SSL, UDP, scheduled events, unit testing infrastructure, and much more. </p> </div> <div class="tab-pane fade" id="nav-web-server" role="tabpanel" aria-labelledby="nav-web-server-tab"> <p> 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: </p> <pre> <code class="language-python"> from twisted.web import server, resource from twisted.internet import reactor, endpoints class Counter(resource.Resource): isLeaf = True numberRequests = 0 def render_GET(self, request): self.numberRequests += 1 request.setHeader(b"content-type", b"text/plain") content = u"I am request #{}\n".format(self.numberRequests) return content.encode("ascii") endpoints.serverFromString(reactor, "tcp:8080").listen(server.Site(Counter())) reactor.run() </code> </pre> <p> Learn more about <a href="https://docs.twisted.org/en/stable/web/howto/web-in-60/index.html"> web application development</a>, <a href="https://docs.twisted.org/en/stable/web/howto/twisted-templates.html"> templates</a> and Twisted' <a href="https://docs.twisted.org/en/stable/web/howto/client.html"> HTTP client</a>. </p> </div> <div class="tab-pane fade" id="nav-publish" role="tabpanel" aria-labelledby="nav-publish-tab"> <p> Here's a simple publish/subscribe server, where clients see all messages posted by other clients: </p> <pre> <code class="language-python"> 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): self.factory.clients.add(self) def connectionLost(self, reason): self.factory.clients.remove(self) def lineReceived(self, line): for c in self.factory.clients: source = u"<{}> ".format(self.transport.getHost()).encode("ascii") c.sendLine(source + 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()) reactor.run() </code> </pre> <p> You can test this out by opening two terminals and doing telnet localhost 1025 in each, then typing things. </p> </div> <div class="tab-pane fade" id="nav-mail" role="tabpanel" aria-labelledby="nav-mail-tab"> <p> Twisted includes a sophisticated IMAP4 client library. </p> <pre> <code class="language-python"> import sys from twisted.internet import protocol, defer, endpoints, task from twisted.mail import imap4 from twisted.python import failure async def main( reactor, username="alice", password="secret", strport="tls:example.com:993" ): endpoint = endpoints.clientFromString(reactor, strport) factory = protocol.Factory.forProtocol(imap4.IMAP4Client) try: client = await endpoint.connect(factory) await client.login(username.encode("utf-8"), password.encode("utf-8")) await client.select("INBOX") info = await client.fetchEnvelope(imap4.MessageSet(1)) print("First message subject:", info[1]["ENVELOPE"][1]) except: print("IMAP4 client interaction failed") print(failure.Failure().getTraceback()) task.react(lambda *a, **k: defer.ensureDeferred(main(*a, **k)), sys.argv[1:]) </code> </pre> <p> Give this a try, supplying your IMAP4 username, app password (<a class="ext-link" href="https://support.google.com/accounts/answer/185833?hl=en">generate one for gmail</a>, <a class="ext-link" href="https://www.fastmail.com/help/clients/apppassword.html">generate one for fastmail</a>), and <a class="ext-link" href="https://docs.twisted.org/en/stable/core/howto/endpoints.html"> client endpoint description </a> for your IMAP4 server. You'll see the subject of the first message in your mailbox printed. </p> <p> See the <a class="wiki" href="https://github.com/twisted/trac-wiki-archive/blob/trunk/TwistedMail.mediawiki"> TwistedMail </a> documentation for more information. </p> </div> <div class="tab-pane fade" id="nav-ssh" role="tabpanel" aria-labelledby="nav-ssh-tab"> <p> Twisted includes an SSH client & server, "conch" (i.e.: the Twisted Shell). </p> <pre> <code class="language-python"> import sys, os from twisted.internet import protocol, defer, endpoints, task from twisted.conch.endpoints import SSHCommandClientEndpoint async def main(reactor, username="alice", sshhost="example.com", portno="22"): envAgent = endpoints.UNIXClientEndpoint(reactor, os.environ["SSH_AUTH_SOCK"]) endpoint = SSHCommandClientEndpoint.newConnection( reactor, "echo 'hello world'", username, sshhost, int(portno), agentEndpoint=envAgent, ) class ShowOutput(protocol.Protocol): received = b"" def dataReceived(self, data): self.received += data def connectionLost(self, reason): finished.callback(self.received) finished = defer.Deferred() factory = protocol.Factory.forProtocol(ShowOutput) await endpoint.connect(factory) print("SSH response:", await finished) task.react(lambda *a, **k: defer.ensureDeferred(main(*a, **k)), sys.argv[1:]) </code> </pre> <p> You can use this client to run "hello world" on any SSH server that your local SSH agent can authenticate to, if you pass your username, host name, and optionally port number on the command line. </p> </div> </div> </div> </div> </div> <div class="divider"> <img class="bg-dark" src="assets/images/divider.svg" alt="divider graphic" data-inject-svg /> </div> </section> <section class="bg-dark text-light py-2" id="about"> <div class="container"> <div class="row mb-4"> <div class="col"> <h1>About Twisted</h1> </div> </div> <div class="row"> <div class="col-md-4 d-flex mb-4"> <i class="icon bi bi-puzzle"></i> <div class="ml-2"> <h3>More Protocols</h3> <p> Twisted also supports many common network protocols, including SMTP, POP3, IMAP, SSHv2, and DNS. </p> <p>For more information see our <a class="wiki" href="https://docs.twisted.org/"> documentation </a> and <a class="ext-link" href="https://docs.twisted.org/en/stable/api/"> API reference</a>. </p> </div> </div> <div class="col-md-4 d-flex mb-4"> <i class="icon bi bi-people-fill"></i> <div class="ml-2"> <h3>Community</h3> <p> Get in touch with the <a class="wiki" href="https://docs.twisted.org/en/latest/community.html"> Twisted community </a> through <a class="wiki" href="https://docs.twisted.org/en/latest/community.html#mail-lists"> email</a>, <a class="ext-link" href="https://stackoverflow.com/questions/tagged/twisted"> Stack Overflow </a> or <a class="wiki" href="https://docs.twisted.org/en/latest/community.html#real-time-chat"> Gitter / IRC</a>. </p> </div> </div> <div class="col-md-4 d-flex mb-4"> <i class="icon bi bi-lightbulb-fill"></i> <div class="ml-2"> <h3>Contribute</h3> <p> Learn about the Twisted <a class="wiki" href="https://github.com/twisted/trac-wiki-archive/blob/trunk/TwistedDevelopment.mediawiki"> development process</a> and how to <a class="wiki" href="https://github.com/twisted/trac-wiki-archive/blob/trunk/ContributingToTwistedLabs.mediawiki"> contribute</a>. </p> <p> Help improve Twisted on <a class="wiki" href="https://github.com/twisted/trac-wiki-archive/blob/trunk/Windows.mediawiki"> Windows</a>! </p> </div> </div> <div class="col-md-4 d-flex mb-4"> <i class="icon bi bi-search"></i> <div class="ml-2"> <h3>Wiki</h3> <p> Read about <a class="wiki" href="https://github.com/twisted/trac-wiki-archive/blob/trunk/ProjectsUsingTwisted.mediawiki"> software using </a> Twisted and their <a class="wiki" href="https://github.com/twisted/trac-wiki-archive/blob/trunk/SuccessStories.mediawiki"> success stories</a>. </p> </div> </div> <div class="col-md-4 d-flex mb-4"> <i class="icon bi bi-gift-fill"></i> <div class="ml-2"> <h3>Sponsors</h3> <p> Learn about the <a href="https://docs.twisted.org/en/latest/development/sponsorship.html"> individuals and organisations </a> that sponsor Twisted development. </p> </div> </div> <div class="col-md-4 d-flex mb-4"> <i class="icon bi bi-grid-3x3-gap"></i> <div class="ml-2"> <h3>Twisted Matrix Laboratories</h3> <p> Find out what <a class="wiki" href="https://github.com/twisted/trac-wiki-archive/blob/trunk/TwistedMatrixLaboratories.mediawiki"> Twisted Matrix Laboratories </a>is. </p> </div> </div> </div> </div> </section> <section class="py-4"> <div class="container"> <div class="row justify-content-center"> <div class="col-xl-11"> <div class="row"> <div class="col-md-6 d-flex mb-4" > <div class="icon-round bg-primary mr-3"> <i class="icon bi bi-github text-primary"></i> </div> <div> <h4>See our code on Github</h4> <p>See the code <a class="ext-link" href="https://github.com/twisted/twisted"> <span class="icon"></span>for Twisted </a> (<a class="ext-link" href="https://github.com/twisted"><span class="icon"></span>and more</a>) on GitHub. </p> </div> </div> <div class="col-md-6 d-flex mb-4"> <div class="icon-round bg-primary mr-3"> <i class="icon bi bi-shield-check text-primary"></i> </div> <div> <h4>Supported Python</h4> <p class="mb-0">It supports CPython 3.7+ and PyPy3.</p> <p class="mb-0"> <a href="https://labs.twistedmatrix.com/2020/03/twisted-drops-python-27-support.html"> Twisted 20.3.0 </a> was the last version with Python 2.7 and PyPy2 support. </p> </div> </div> <div class="col-md-6 d-flex mb-4 align-items-center"> <div class="icon-round bg-primary mr-3"> <i class="icon bi bi-envelope-open text-primary"></i> </div> <div> <h4>Join the <a href="https://mail.python.org/mailman3/lists/twisted.python.org/"> discussion list </a> </h4> </div> </div> <div class="col-md-6 d-flex mb-4 align-items-center"> <div class="icon-round bg-primary mr-3"> <i class="icon bi bi-shield-lock-fill text-primary"></i> </div> <div> <h4>Report a <a href="https://github.com/twisted/twisted/security/policy"> security issue </a> </h4> </div> </div> <div class="col-md-6 d-flex mb-4 align-items-center"> <div class="icon-round bg-primary mr-3"> <i class="icon bi bi-stack text-primary"></i> </div> <div> <h4>Ask on <a href="https://stackoverflow.com/questions/tagged/twisted"> Stack Overflow </a> </h4> </div> </div> </div> </div> </div> </div> </section> <section class="py-0"> <div class="container-fluid"> <div class="bg-secondary text-white py-2 row text-center"> <div class="col"> <span class="h2">Twisted Sponsors</span> </div> </div> <div class="premium-sponsors row align-items-center justify-content-center"> <div class="col-auto"> <a href="https://thinkst.com/" target="_blank"> <img src="assets/images/sponsor-thinkst.svg" alt="Thinkst Canary" title="Thinkst Canary" class="img-fluid"/> </a> </div> </div> <div class="standard-sponsors row align-items-center justify-content-center pb-4"> <div class="col-auto"> <a href="https://www.sftpplus.com/" target="_blank"> <img src="assets/images/sponsor-sftpplus.svg" alt="" title="" class="img-fluid"/> </a> </div> </div> <div class="row text-center"> <div class="col"> <p> Financial support can be provided for the Twisted project via <a href="https://psfmember.org/civicrm/contribute/transact/?reset=1&id=44"> the Python Software Foundation</a> or via <a href="https://github.com/sponsors/twisted/"> GitHub Sponsors</a> ! </p> <p> For donations greater than $400 per month, we will display your logo at the top of the page. For donations greater than $200 per month, we will display your logo on this page. Check <a href="https://github.com/sponsors/twisted/"> GitHub Sponsors</a> for more information about sponsoring perks. </p> </div> </div> </div> </section> <section class="pt-2 pb-4" id="donate"> <div class="container"> <div class="row"> <div class="col"> <div class="card card-body border-0 o-hidden mb-0 shadow-3d text-light bg-primary"> <div class="position-relative d-flex flex-column flex-md-row justify-content-between align-items-center"> <div class="text-center mb-md-0"> <h1>Donate to Twisted</h1> <p> Donations are tax-deductible in USA<br> <a href="https://www.python.org/psf-landing/" class="text-dark"> via the Python Software Foundation</a>. </p> </div> <div class="donation-buttons layer-2 text-center"> <a href="https://psfmember.org/civicrm/contribute/transact/?reset=1&id=44" class="btn btn-lg btn-light mb-2"> Donate <span class="small">(via PSF)</span> </a> </div> </div> <div class="decoration middle-x d-none d-lg-block layer-1"> <img class="bg-dark" src="assets/images/deco-blob.svg" alt="deco-blob decoration" data-inject-svg /> </div> </div> </div> </div> </div> </section> <!-- Required vendor scripts (Do not remove) --> <script type="text/javascript" src="vendor/js/jquery-v3.5.1.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"> </script> <script type="text/javascript" src="vendor/js/bootstrap-v4.5.3.min.js"></script> <script type="text/javascript" src="vendor/js/highlight.min.js"></script> <script src="vendor/js/clipboard.min.js"></script> <!-- SVGInjector (replaces img tags with SVG code to allow easy inclusion of SVGs with the benefit of inheriting colors and styles)--> <script type="text/javascript" src="vendor/js/svg-inject-v1.2.3.js"></script> <script type="text/javascript" src="assets/js/code.js"></script> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10