Skip to content

Instantly share code, notes, and snippets.

@timhughes
Created March 16, 2016 19:20
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 timhughes/f85da0c2d88ecb4ab5e3 to your computer and use it in GitHub Desktop.
Save timhughes/f85da0c2d88ecb4ab5e3 to your computer and use it in GitHub Desktop.
contrived example
class MyClientProtocol(protocol.Protocol):
def dataTransmit(message):
self.transport.write(data)
def dataReceived(self, message):
self.factory.service.handle_message(message)
class MyClientFactory(protocol.ClientFactory):
protocol = MyClientProtocol
def __init__(self, service):
self.service = service
class MyService(service.Service):
def startService(self):
self.config = load_config(config_file)
def handleMessage(self, message):
# do something with data
decoded_msg = self.decodeMessage(message)
if decoded_msg['type'] == 'logon_response':
decoded_msg['result'] == 'good':
self.doSomthing()
def sendMessage(self, message)
###############################
# Not sure what to do here without coupling it to the protocol
##############################
def decodeMessage(self, data):
return json.loads(data)
def encodeMessage(self, data):
return json.dumps(data)
def logon(self):
logon_message_data = {
'user': self.config.user,
'pass': self.config.pass
}
msg = self.encodeMessage(logon_message_data)
self.sendMessage(msg)
def doSomthing(self)
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment