[Twisted-Python] Re: How can I disconnect ssl connection from the client side?

Atsuo Ishimoto ishimoto at gembook.org
Mon Feb 2 03:48:50 MST 2009


Investigating further, I found this is not SSL related problem, but in
my code may have bug. In the following script, I expect that reactor
stops in 2 secs after it started, but connectionLost() is not fired at
all.

Is this a bug in Twisted? Or Am I missing something?


=========================================
from twisted.protocols import basic
from twisted.internet import protocol, reactor

serverfactory = protocol.Factory()
serverfactory.protocol = basic.LineOnlyReceiver
port = reactor.listenTCP(9084, serverfactory, interface='127.0.0.1')

CANCELED = False
class Client(basic.LineReceiver):
    def connectionMade(self):
        class dmyfile:
            def read(self, n):
                if CANCELED:
                    return
                else:
                    return '1'

        s = basic.FileSender()
        print "start sending"

        def f1(lastChunk):
            print "finished"

        def f2(reason):
            print "failed"
            print reason

        s.beginFileTransfer(
            dmyfile(), self.transport, None
        ).addCallbacks(f1, f2)

    def connectionLost(self, conn, reason=protocol.connectionDone):
        print "protocol:connectionlost"


class Factory(protocol.ClientFactory):
    protocol = Client

    def clientConnectionLost(self, conn, reason):
        print "clientConnectionLost"
        reactor.stop()

conn = reactor.connectTCP('127.0.0.1', 9084, Factory())

def cancel():
    print "cancel", conn.state
    global CANCELED
    CANCELED = True
    conn.disconnect()

reactor.callLater(2, cancel) # disconnect 2 sec later
reactor.run()




More information about the Twisted-Python mailing list