Skip to content

Instantly share code, notes, and snippets.

@george-silva
Created January 9, 2014 21:56
Show Gist options
  • Save george-silva/8342803 to your computer and use it in GitHub Desktop.
Save george-silva/8342803 to your computer and use it in GitHub Desktop.
Twisted huh?
# coding: utf-8
from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor
class Gateway(LineReceiver):
"""Classe para lidar com as conexões"""
def __init__(self, modulos_autorizados=[]):
self.modulos_autorizados = modulos_autorizados
self.modulos_conectados = []
def connectionMade(self):
self.sendLine("Modulo conectado")
def connectionLost(self, reason):
pass
def lineReceived(self, line):
if line[:4] not in self.modulos_conectados:
self.modulos_conectados.append(line[:4])
if line[:4] in self.modulos_autorizados:
self.sendLine("ack")
else:
self.sendLine("ref")
class GatewayFactory(Factory):
def __init__(self, modulos_autorizados=["ABCD", "EFGH"]):
self.modulos_autorizados = modulos_autorizados
def buildProtocol(self, addr):
return Gateway(self.modulos_autorizados)
if __name__ == "__main__":
reactor.listenTCP(8666, GatewayFactory())
reactor.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment