def main(): from twisted.internet.app import Application factory = Factory() factory.protocol = Echo app = Application("echo") app.listenTCP(8000,factory) app.run(save=0)
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA from twisted.protocols.protocol import Protocol, Factory from twisted.internet import udp ### Protocol Implementation # This is just about the simplest possible protocol class Echo(Protocol): def dataReceived(self, data): "As soon as any data is received, write it back." self.transport.write(data) ### Persistent Application Builder # This builds a .tap file if __name__ == '__main__': # Since this is persistent, it's important to get the module naming right # (If we just used Echo, then it would be __main__.Echo when it attempted # to unpickle) import echoserv from twisted.internet.app import Application factory = Factory() factory.protocol = echoserv.Echo app = Application("echo") app.listenTCP(8000,factory) app.listenUDP(8000, factory) app.save("start")
from OpenSSL import SSL class ServerContextFactory: def getContext(self): """Create an SSL context. This is a sample implementation that loads a certificate from a file called 'server.pem'.""" ctx = SSL.Context(SSL.SSLv23_METHOD) ctx.use_certificate_file('server.pem') ctx.use_privatekey_file('server.pem') return ctx ### Persistent Application Builder # This builds a .tap file if __name__ == '__main__': import echoserv from twisted.protocols.protocol import Factory from twisted.internet.app import Application from twisted.internet import ssl from echoserv_ssl import ServerContextFactory factory = Factory() factory.protocol = echoserv.Echo app = Application("echo-ssl") app.listenSSL(8000, factory, ServerContextFactory()) app.save("start")
"""Thread that does processing on data received from Echo protocol""" def __init__(self, protocol): threading.Thread.__init__(self) self.protocol = protocol def run(self): while 1: # read data from queue data = self.protocol.messagequeue.get() if data != None: # write back data unchanged self.protocol.send(data) else: # connection was closed return ### Persistent Application Builder # This builds a .tap file if __name__ == '__main__': from twisted.internet.app import Application factory = Factory() factory.protocol = Echo app = Application("echo") app.listenTCP(8000, factory) app.run(save=0)