Skip to content

Instantly share code, notes, and snippets.

@alivesay
Created August 27, 2012 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alivesay/3492003 to your computer and use it in GitHub Desktop.
Save alivesay/3492003 to your computer and use it in GitHub Desktop.
How to disconnect w/ Twisted cleanly?
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