【发布时间】:2015-09-07 21:43:56
【问题描述】:
我是 Twisted 的新手。假设我正在编写一个通过 TCP 连接到服务器的客户端。我想知道 Protocol 中定义的 connectionLost 与 Factory 中定义的 clientConnectionLost 之间有什么区别。例如,考虑以下连接到回显服务器的回显客户端:
from twisted.internet import reactor, protocol
class EchoClient(protocol.Protocol):
def connectionMade(self):
self.transport.write("Hello, World!")
def connectionLost(self,reason):
print "connectionLost called "
def dataReceived(self,data):
print "Server said: ", data
class EchoFactory(protocol.ClientFactory):
def buildProtocol(self, addr):
return EchoClient()
def clientConnectionFailed(self, connector, reason):
print "Connection failed"
reactor.stop()
def clientConnectionLost(self, connector, reason):
print "clientConnectionLost called"
reactor.stop()
reactor.connectTCP("localhost",8000,EchoFactory())
reactor.run()
当我终止回显服务器程序时,“connectionLost called”和“clientConnectionLost called”都会被打印出来。那么这两者有什么区别呢?
谢谢
【问题讨论】: