码迷,mamicode.com
首页 > 其他好文 > 详细

twisted

时间:2017-12-05 21:24:26      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:write   reason   一个   cto   return   client   容器   data   prot   

Protocol 协议
Factory 工厂
reactor 反应器 (看作一个容器)
========= server.py =========

from twisted.internet import reactor
from twisted.internet.protocol import Factory,Protocol


class Echo(Protocol):
def connectionMade(self):
self.transport.write(‘hi,welcome!‘)
self.transport.loseConnection()

def dataReceived(self, data):
print(data)
self.transport.write(‘haha...‘+data)

factory = Factory()
factory.protocol = Echo

reactor.listenTCP(8001,factory)
reactor.run()

========= client.py =========
from twisted.internet.protocol import Protocol,ClientFactory
from twisted.internet import reactor


class Echo(Protocol):
def connectionMade(self):
self.transport.write(‘hi, server‘)

def dataReceived(self, data):
print(data)


class EchoClientFactory(ClientFactory):
def startedConnecting(self, connector):
print(‘connection starting ...‘)

def buildProtocol(self, addr):
print(‘addr‘)
return Echo()

def clientConnectionLost(self, connector, reason):
print(‘lose reason‘,reason)

def clientConnectionFailed(self, connector, reason):
print(‘faild reason: ‘,reason)

reactor.connectTCP(‘127.0.0.0‘,8001,EchoClientFactory())
reactor.run()

twisted

标签:write   reason   一个   cto   return   client   容器   data   prot   

原文地址:http://www.cnblogs.com/liyugeng/p/7989250.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!