Python Programming (twisted-based client programming)

Source: Internet
Author: User

"Disclaimer: Copyright all, welcome reprint. Do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "


Python's twisted is more interesting, both for server programming and for client programming. Programming in this regard. The simplest example is echo.


The client code, such as the following,

#!/usr/bin/pythonfrom twisted.internet.protocol Import Protocol, clientfactoryfrom sys import Stdoutfrom Twisted.internet Import Reactorclass Echo (Protocol):d ef datareceived (Self, data): Stdout.write (Data) class Echoclientfactory (clientfactory):d ef startedconnecting (self, connector):p rint ' Started to connect. '   def buildprotocol (self, addr):p rint ' Connected. ' Return Echo ()   def clientconnectionlost (self, connector, reason):p rint ' Lost connection. Reason: ', Reasondef clientconnectionfailed (self, connector, Reason):p rint ' Connection failed. Reason: ', reasonif __name__ = = ' __main__ ': reactor.connecttcp (' localhost ', 1234, echoclientfactory ()) Reactor.run ()

Server code such as the following,

#!/usr/bin/pythonfrom 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 () if __name__ = = ' __main__ ':    Endpoints.serverfromstring (reactor, "tcp:1234"). Listen (Echofactory ())    Reactor.run ()


Python Programming (twisted-based client programming)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.