免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2427 | 回复: 0
打印 上一主题 下一主题

twisted task clientfactory 问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-07-05 09:09 |只看该作者 |倒序浏览
本帖最后由 moartel 于 2012-07-05 09:10 编辑

想实现的功能,
客户端每10秒发送一次数据到服务器
  1. from twisted.internet.protocol import Protocol,ClientFactory,Factory
  2. from twisted.internet import reactor,task

  3. host = '10.150.195.48'
  4. port = 56677

  5. class Echo(Protocol):
  6.     def connectionMade(self):
  7.         self.transport.write("An apple a day keeps the doctor away")
  8.         #self.transport.write("An apple a day keeps the doctor away\r\n")
  9.         self.transport.loseConnection()

  10. class EchoClientFactory(ClientFactory):
  11.     def startedConnecting(self, connector):
  12.         print 'Started to connect.'

  13.     def buildProtocol(self, addr):
  14.         print 'Connected.'
  15.         return Echo()

  16. #    reactor.stop()
  17. #    def clientConnectionLost(self, connector, reason):
  18. #        print 'Lost connection.  Reason:', reason

  19. #    def clientConnectionFailed(self, connector, reason):
  20. #        print 'Connection failed. Reason:', reason
  21. def main():
  22.     reactor.connectTCP(host, port, EchoClientFactory())
  23.     reactor.run()
  24. #if __name__ == '__main__':
  25. #    main()

  26. m = task.LoopingCall(main())
  27. m.start(10.0)


  28. reactor.run()
复制代码
尝试写了代码,可是不工作,请教各位如何实现

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP