Basic Twisted echo server

Nothing mind blowing here. I’m playing with Python’s twisted engine, and here is a slightly modified server from their home page example:

from twisted.internet import protocol, reactor
 
class Echo(protocol.Protocol):
    def dataReceived(self, data):
        newdata = 'reversed: ' + ''.join(reversed(data.strip())) + 'n'
        self.transport.write(newdata)
 
class EchoFactory(protocol.Factory): #build an Echo object for each connection
    def buildProtocol(self, addr):                                                                                      
        return Echo()
 
reactor.listenTCP(8000, EchoFactory()) #register a callback
reactor.run()

Run the script and telnet to port 8000 of this box. This is what it looks like:

Connected to erdos.
Escape character is '^]'.
hello
reversed: olleh
reverse this!
reversed: !siht esrever

The example is also here, with a more detailed explanation of the basics.

The API reference helps to see what methods need to be implemented while sub-classing one of its classes.