# 2013-05-12 johnpfeiffer simple variation on an echo server
from twisted.protocols import basic
from twisted.internet import protocol, reactor
class HTTPEchoProtocol( basic.LineReceiver ):
def __init__(self):
self.lines = []
self.simple_password = 'echo'
self.approved = False
def lineReceived( self, line ):
if self.simple_password in line:
self.approved = True
self.lines.append( line )
if not line:
if self.approved:
self.sendResponse()
else:
self.transport.loseConnection()
def sendResponse( self ):
self.sendLine( "HTTP/1.1 200 OK" )
self.sendLine( "" )
responseBody = "You said:\r\n\r\n" + "\r\n".join( self.lines )
self.transport.write( responseBody )
self.transport.loseConnection()
class HTTPEchoFactory(protocol.ServerFactory):
def buildProtocol( self, addr ):
return HTTPEchoProtocol()
reactor.listenTCP( 8888, HTTPEchoFactory() )
reactor.run()