我在 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()
我遇到的问题是我看到“已建立连接”。在我的日志中紧跟“连接丢失:连接已完全关闭。”。为什么我的连接在连接后立即断开?