Created
October 8, 2013 22:13
-
-
Save aurynn/6892706 to your computer and use it in GitHub Desktop.
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 stdio, protocol, reactor | |
from twisted.protocols.basic import LineReceiver | |
import json | |
class myProtocol(LineReceiver): | |
def connectionMade(self): | |
# self.sendLine(json.dumps({"name": "aurynn", "message":"hello"})) | |
self.io.getNetwork(self) | |
def lineReceived(self, data): | |
# Do some stuff w/ the data | |
p = json.loads(data) | |
try: | |
self.io.say(str( "<%(name)s> %(message)s" % p )) | |
except AttributeError: | |
pass | |
def saySomething(self, name, line): | |
self.sendLine(json.dumps({"name": name, "message": line})) | |
class UserInputHandler(LineReceiver): | |
delimiter = str( "\n" ) | |
name = "aurynn" | |
def getNetwork(self, proto): | |
self.remote = proto | |
self.sendLine(str( "connected to server..." )) | |
def say(self, line): | |
self.sendLine(str(line)) | |
self.transport.write("<%s> " % self.name) | |
def connectionMade(self): | |
self.sendLine(str( "Connected to console!" )) | |
def lineReceived(self, line): | |
if not line: return | |
try: | |
self.remote.saySomething(self.name, line) | |
except AttributeError: | |
self.sendLine(str( "not connected..." )) | |
class myFactory(protocol.Factory): | |
def clientConnectionLost(self, r, a): | |
pass | |
def startedConnecting(self, boo): | |
pass | |
def buildProtocol(self, addr): | |
m = myProtocol() | |
m.io = self.io | |
m.factory = self | |
return m | |
factory = myFactory() | |
io = UserInputHandler() | |
factory.io = io | |
df = reactor.connectTCP( | |
"10.22.33.126", | |
1234, | |
factory | |
) | |
stdio.StandardIO(io) | |
reactor.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment