Twisted

Web Name: Twisted

WebSite: http://twistedmatrix.com

ID:97467

Keywords:

Twisted,

Description:

Twisted SponsorsParticipate in the Twisted Project Sponsorship Program! For Silver Sidewinder and higher-level sponsors, we will display your logo here on the front page for one year. Golden Tree SnakeSilver SidewinderWhat is Twisted?Twisted is an event-driven networking engine written in Python and licensed under the open source ​MIT license. It supports CPython 2.7, CPython 3.5+, PyPy2, and PyPy3. ​Twisted 20.3.0 is the last version with Python 2.7 and PyPy2 support.Code Examples Echo Server Web Server Publish/Subscribe Mail Client SSH ClientTwisted 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, endpointsclass 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()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, resourcefrom twisted.internet import reactor, endpointsclass 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()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, endpointsfrom twisted.protocols import basicclass 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()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 sysfrom twisted.internet import protocol, defer, endpoints, taskfrom twisted.mail import imap4from twisted.python import failureasync 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())Give this a try, supplying your IMAP4 username, app password (​generate one for gmail, ​generate one for fastmail), 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.Twisted includes an SSH client server, "conch" (i.e.: the Twisted Shell).import sys, osfrom twisted.internet import protocol, defer, endpoints, taskfrom twisted.conch.endpoints import SSHCommandClientEndpointasync 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:])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.More ProtocolsTwisted also supports many common network protocols, including SMTP, POP3, IMAP, SSHv2, and DNS. For more information see our documentation and ​API reference.CommunityGet in touch with the Twisted community through email, ​Stack Overflow or IRC;Learn about the Twisted development process and how to contribute;Read about software using Twisted and their success stories;Find out what Twisted Matrix Laboratories is;Learn about the individuals and organisations that aid Twisted with donations of hardware, software, hosting and other things;Help improve Twisted on Windows!Human beings, please do not send messages to this email address!

TAGS:Twisted 

<<< Thank you for your visit >>>

An event-driven networking engine written in Python and MIT licensed.

Websites to related :
Infinite Energy Magazine

  Support the New Energy Foundation with every Internet Search FREE! Box 2816 Concord, NH 03302-2816 Phone: 603.485.4700 Fax: 603.485.4710 staff@infini

^_^让我们荡起双桨-搜狐博客

  提示:您需要开启Javascript 检测到您使用的浏览器不支持脚本语言(javascript),或该功能已被禁用。 搜狐博客提供了一套非常强大的用户界面,可以让您很方便地制作出属

Baumann Wohlenberg - Optimisatio

  The perfect solution needs to be customized! BaumannWohlenberg cutting systems are no „off-the-shelf-solutions“ but are made to measure exactly your

Home | Central Florida Repack

  RECONDITION PRODUCELearn MorePACK PRODUCELearn MoreYour Packing, Reconditioning, Logistics PartnerAt Central Florida Repack Logistics we specialize

3PL Warehouse in Atlanta, GA, Mc

  COVID 19 - Update from Warehouse BasicsDuring these trying times, Warehouse Basics wants you to know that we are practicing all recommended Coronaviru

home - Phonojack

  Snapchat story is a popular way to share something with your friends. You can share photos or videos to the story section of your... Snapchat story i

Over 100 Years Transportation an

  Ask a question/request a quote:sales@peoplesservices.comDiscuss your logistics needs:Call John at 330.458.2357All other inquiries, please call:330.453

About Lenvale Paper UK’s leadin

  Unit 1, Phoenix Industrial Estate, Commissioners Road, Strood, Kent ME2 4HZ+44 (0) 1634 242660info@lenvalepaper.co.ukAbout UsOur ServicesContact UsLen

The Nuckolls Group Welcome

  Colin Nuckolls is the Sheldon and Dorothea Buckler Professor of Chemistry in the Department of Chemistry at Columbia University. His research focuses

GAK - Buy Electric Guitars | Amp

  GAK.co.uk - Electric Guitars | Amps | Digital Pianos | DrumsFender American Professional II Jazzmaster, Rosewood Fingerboard, MercuryThe new standard

ads

Hot Websites