现在的位置: 主页 > 主打产品 > 文章列表

python手记-twisted(4)

作者:北京夜明珠节能科技有限公司 来源:www.ymzjn.com 发布时间:2017-09-05 11:57:52
python手记-twisted(4) from twisted.internet.protocol import Protocol from twisted.internet import reactor from twisted.internet.protocol import Factory from twisted.internet.endpoints import TCP4ServerEndpoint #aspl 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 # 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" # endpoint=TCP4ServerEndpoint(reactor,8001) endpoint.listen(EchoFactory("myhaspl:")) reactor.run()

$ telnet 120.55.*.* 8001

Trying 120.55.*.*...

Connected to 120.55.*.*.

Escape character is '^]'.

welcome!

there are currently 1 open connections.

hello

myhaspl:hello

world

myhaspl:world

quit

myhaspl:byebye

Connection closed by foreign host.

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:黄冈SEO http://huanggang.4567w.com

上一篇:网站设计者 是这样炼成的 下一篇:最后一页