python手记-twisted(4)
2016-07-09 17:22
381 查看
from twisted.internet.protocol import Protocol from twisted.internet import reactor from twisted.internet.protocol import Factory from twisted.internet.endpoints import TCP4ServerEndpoint #http://blog.csdn.net/myhaspl class Echo(Protocol): def connectionMade(self): self.factory.numProtocols=self.factory.numProtocols+1 self.transport.write("welcome!\nthere are currently %d open connections.\n"%(self.factory.numProtocols,)) def dataReceived(self,data): mydata=data.strip() if mydata!="quit" and mydata!="quit\r\n" and mydata!="quit\r": self.transport.write(self.factory.message+data) else: self.transport.write(self.factory.message+"byebye\n") self.transport.numProtocols=self.factory.numProtocols-1 self.transport.loseConnection() def connectionLost(self, reason): self.factory.numProtocols = self.factory.numProtocols - 1 #http://blog.csdn.net/myhaspl class EchoFactory(Factory): #use the default buildProtocol to create protocol protocol=Echo numProtocols=0 def __init__(self,message=None): self.message=message or "hello,world" #http://blog.csdn.net/myhaspl endpoint=TCP4ServerEndpoint(reactor,8001) endpoint.listen(EchoFactory("myhaspl:")) reactor.run()未经博主允许不得转载。http://blog.csdn.net/myhaspl
本博客所有内容是原创,如果转载请注明来源
http://blog.csdn.net/myhaspl/
$ telnet 120.55.*.* 8001Trying 120.55.*.* ...Connected to 120.55.*.* .Escape character is '^]'.welcome!there are currently 1 open connections.hellomyhaspl:helloworldmyhaspl:worldquitmyhaspl:byebyeConnection closed by foreign host.相关文章推荐
- python学习——递归函数
- CascadeCNN python测试程序详解(test.py)
- python学习——函数的参数
- 我的Python第二篇 条件分支和循环篇
- Python简介和安装
- Python学习笔记之文件操作总结
- python学习——调用函数
- 使用python爬取学校门户网站相关信息并格式化输出
- python学习——函数
- python学习——使用dict和set
- 用 Python 进行数据分析,不懂 Python,求合适的 Python 书籍或资料推荐?
- Python 爬虫框架pyspider尝试
- python学习——循环
- python学习——条件判断
- python学习——使用list和tuple
- python学习——字符串和编码
- python 浮点,zip函数
- Flask 学习笔记1 --Qickstart
- 31.Python的单元测试工具——doctest
- Install pip on MAC