0

我在 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()

我遇到的问题是我看到“已建立连接”。在我的日志中紧跟“连接丢失:连接已完全关闭。”。为什么我的连接在连接后立即断开?

4

1 回答 1

0

我从同一个包中替换,并将方法更改为LineReceiver所有“刚刚工作。™”ProtocollineReceiveddataReceived

于 2013-01-11T21:21:27.233 回答