简体   繁体   中英

Client immediately disconnects

I'm on Python 2.7 and I'm having a really strange issue with a simple Twisted client.

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

The problem I'm having is that I see "Connection made." in my logs immediately followed by "connection lost: Connection was closed cleanly.". Why is my connection dropping immediately after connection?

我换成LineReceiverProtocol从相同的封装,改变了lineReceived方法dataReceived ,一切“只是工作。™”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM