Skip to content

Instantly share code, notes, and snippets.

@esamson33
Created March 17, 2016 02:01
Show Gist options
  • Save esamson33/73b6709f0958444daea3 to your computer and use it in GitHub Desktop.
Save esamson33/73b6709f0958444daea3 to your computer and use it in GitHub Desktop.
from twisted.application.service import Service
from twisted.protocols.basic import LineReceiver
from twisted.python import log
from twisted.internet.protocol import ClientCreator
from twisted.internet import reactor
class MyProtocol(LineReceiver):
def lineReceived(self, line):
# process bot command
log.msg("recv:", line)
def disconnect(self):
if self.transport:
self.transport.loseConnection()
class MyService(Service):
def __init__():
self._client = None
def startService():
d = ClientCreator(reactor, MyProtocol).connectTCP(HOST, PORT)
d.addCallback(gotClient)
d.addErrback(log.err)
def stopService():
# clean up
if self._client:
self._client.disconnect
def gotClient(self, prot):
# keep client reference
self._client = prot
def sendMessage(self, message):
# send a message to the server; call from web interface
if self._client:
self._client.sendLine(message)
return True
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment