Python Twisted中connectionLost和clientConnectionLost之间的差异
我是新来的。假设我正在编写一个通过TCP连接到服务器的客户端,我想知道协议中定义的connectionLost与工厂中定义的clientConnectionLost之间的区别。例如,考虑连接到ECHO服务器的以下ECHORE客户端:Python Twisted中connectionLost和clientConnectionLost之间的差异,python,twisted,Python,Twisted,我是新来的。假设我正在编写一个通过TCP连接到服务器的客户端,我想知道协议中定义的connectionLost与工厂中定义的clientConnectionLost之间的区别。例如,考虑连接到ECHO服务器的以下ECHORE客户端: from twisted.internet import reactor, protocol class EchoClient(protocol.Protocol): def connectionMade(self): self.trans
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()
当我终止echo服务器程序时,“connectionLost called”和“clientConnectionLost called”都会被打印出来。那么这两者的区别是什么呢
谢谢如果您使用的是
connectTCP
,那么这些API大致相同。主要区别在于一个ClientFactory
可能正在处理多个连接
但是,您不应该在应用程序中使用
connectTCP
;这是一个低级API,在Twisted的历史上,它主要用于内部实现机制,而不是应用程序。相反,采用API,如果您使用,您将只使用IFactory
,而不是ClientFactory
,因此在代码中不会有额外的clientConnectionFailed
和clientConnectionLost
方法。感谢这么多Glyph,我现在就明白了。收到项目创建者本人的回复感觉很棒:)