Skip to content

Instantly share code, notes, and snippets.

Created June 12, 2012 20:47
Show Gist options
  • Save anonymous/2920032 to your computer and use it in GitHub Desktop.
Save anonymous/2920032 to your computer and use it in GitHub Desktop.
from sys import executable
from os import environ
from twisted.internet import reactor, defer
from twisted.internet.protocol import Protocol
from twisted.internet.protocol import Factory
from twisted.internet import protocol
from twisted.protocols.basic import LineReceiver
import sys
import time
import Queue
import os
implementation = """
import os
import time
import sys
import string
def censor():
BW = ["sfs", "fsdfwe", "tryt", "sfsvx"]
while (True):
a = sys.stdin.readline()
if a:
for bannedWord in BW:
a = string.replace(a, bannedWord , '<banned>')
sys.stdout.write(a)
sys.stdout.flush()
"""
class Echo(LineReceiver):
def lineReceived(self, data):
d = self.factory.process.sendData(data+'\r\n')
#reactor.callLater(3,d.addCallback, printData)
d.addCallback(printData)
prot.transport.write(data)
def createProcess():
pp = censoringProtocol()
Process = reactor.spawnProcess(pp, executable, [executable, "-c", implementation], env=environ, childFDs = {0:"w", 1:"r", 2:"r"})
pp.process = Process
return pp
class EchoFactory(Factory):
protocol = Echo
telnetConnectionList = []
process = None
deferQ = None
def __init__(self):
EchoFactory.process = createProcess()
EchoFactory.deferQ = Queue.Queue()
def buildProtocol (self, addr):
newProtocol = Factory.buildProtocol(self, addr)
EchoFactory.telnetConnectionList.append(newProtocol)
return newProtocol
class censoringProtocol(protocol.ProcessProtocol):
def __init__(self):
self.process = None
def connectionMade(self):
print "connectionMade!"
def outReceived(self, data):
EchoFactory.deferQ.get().callback(data)
def sendData(self,data):
d = defer.Deferred()
EchoFactory.deferQ.put(d)
self.process.writeToChild(0, data)
return d
reactor.listenTCP(11111, EchoFactory())
print 'in parent', os.getpid()
reactor.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment