- 论坛徽章:
- 0
|
本帖最后由 moartel 于 2012-07-05 09:10 编辑
想实现的功能,
客户端每10秒发送一次数据到服务器- from twisted.internet.protocol import Protocol,ClientFactory,Factory
- from twisted.internet import reactor,task
- host = '10.150.195.48'
- port = 56677
- class Echo(Protocol):
- def connectionMade(self):
- self.transport.write("An apple a day keeps the doctor away")
- #self.transport.write("An apple a day keeps the doctor away\r\n")
- self.transport.loseConnection()
- class EchoClientFactory(ClientFactory):
- def startedConnecting(self, connector):
- print 'Started to connect.'
- def buildProtocol(self, addr):
- print 'Connected.'
- return Echo()
- # reactor.stop()
- # def clientConnectionLost(self, connector, reason):
- # print 'Lost connection. Reason:', reason
- # def clientConnectionFailed(self, connector, reason):
- # print 'Connection failed. Reason:', reason
- def main():
- reactor.connectTCP(host, port, EchoClientFactory())
- reactor.run()
- #if __name__ == '__main__':
- # main()
- m = task.LoopingCall(main())
- m.start(10.0)
- reactor.run()
复制代码 尝试写了代码,可是不工作,请教各位如何实现
|
|