我使用的是Python 2.7,而且我对一个简单的Twisted客户端有一个非常奇怪的问题。
from twisted.internet.protocol import ClientFactory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor
import json, sys, time
class MySpecialClient(LineReceiver):
def connectionMade(self):
print "Connection made."
def lineReceived(self, line):
print "Got a line."
class MySpecialClientFactory(ClientFactory):
protocol = MySpecialClient
def clientConnectionFailed(self, connector, reason):
print 'connection failed:', reason.getErrorMessage()
reactor.stop()
def clientConnectionLost(self, connector, reason):
print 'connection lost:', reason.getErrorMessage()
reactor.stop()
def main():
factory = MySpecialClientFactor()
reactor.connectTCP('localhost', 5050, factory)
reactor.run()
if __name___ == "__main__":
main()
我遇到的问题是我看到“连接已经完成”。在我的日志中紧接着“连接丢失:连接干净地关闭。”。为什么我的连接在连接后立即掉线?
答案 0 :(得分:0)
我将LineReceiver
替换为Protocol
来自同一个软件包,并将lineReceived
方法更改为dataReceived
,所有内容都“正常工作。”