Created
August 27, 2012 20:29
-
-
Save alivesay/3492003 to your computer and use it in GitHub Desktop.
How to disconnect w/ Twisted cleanly?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from twisted.internet import protocol, reactor | |
from twisted.words.protocols import irc | |
from twisted.internet.endpoints import TCP4ClientEndpoint | |
class TestProtocol(irc.IRCClient): | |
nickname = 'test098765' | |
def signedOn(self): | |
self.join('##test098765') | |
class TestFactory(protocol.ClientFactory): | |
protocol = TestProtocol | |
def clientConnectionLost(self, connector, reason): | |
print 'I am never called.' | |
class Test(object): | |
def __init__(self): | |
self.protocol = None | |
reactor.addSystemEventTrigger('before', 'shutdown', self._shutdown) | |
def go(self): | |
endpoint = TCP4ClientEndpoint(reactor, 'irc.freenode.net', 6667) | |
factory = TestFactory() | |
d = endpoint.connect(factory) | |
d.addCallbacks(self._callback_connected, self._callback_error) | |
reactor.run() | |
def _callback_connected(self, protocol): | |
self.protocol = protocol | |
def _callback_error(self, reason): | |
raise Exception(reason) | |
def _shutdown(self): | |
self.protocol.quit('Goodbye IRC...') | |
if __name__ == "__main__": | |
t = Test() | |
t.go() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment