Making Twisted Python simple client server example to work -
i new twisted python (twisted 12.x, python 2.6.x, unfortunately have use older version), , running client/server in server echoes simple message client (example chap 2 of twisted o'reilly book) running server in terminal, , client in separate terminal. client , server stuck (not returning). failing?
server:
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(50000, echofactory()) reactor.run()
client:
from twisted.internet import reactor, protocol class echoclient(protocol.protocol): def connectionmade(self): self.transport.write("hello, world!") def datareceived(self, data): print "server said:", data self.transport.loseconnection() class echofactory(protocol.clientfactory): def buildprotocol(self, addr): return echoclient() def clientconnectionfailed(self, connector, reason): print "connection failed." reactor.stop() def clientconnectionlost(self, connector, reason): print "connection lost." reactor.stop() reactor.connecttcp("localhost", 50000, echofactory()) reactor.run()
your datareceived
, clientconnectionfailed
, , clientconnectionlost
methods not indented, means free functions never being called, rather overriding methods on protocol
or clientfactory
. client code default datareceived
implementation, "do nothing".
also, way, python 2.6 out of security support , not receive updates. please upgrade version 2.7.9 @ least. should upgrade recent version of twisted, if new code; there no legitimate reason use software old; dangerous , irresponsible so.
Comments
Post a Comment