"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)