1 #!/usr/bin/env python 2 import sys 3 4 if 'linux' in sys.platform: 5 from twisted.internet import epollreactor 6 try: 7 epollreactor.install() 8 print "EPOLL Reactor installed." 9 except: 10 pass 11 elif 'win' in sys.platform: 12 from twisted.internet import iocpreactor 13 try: 14 iocpreactor.install() 15 print "IOCP Reactor installed." 16 except: 17 pass 18 elif 'freebsd' in sys.platform: 19 from twisted.internet import kqreactor 20 try: 21 kqreactor.install() 22 print "KQUEUE Reactor installed." 23 except: 24 pass 25 26 27 from twisted.internet import reactor, protocol 28 29 30 class Echo(protocol.Protocol): 31 def dataReceived(self, data): 32 self.transport.write(data) 33 34 35 class EchoFactory(protocol.Factory): 36 def buildProtocol(self, addr): 37 return Echo() 38 39 40 if __name__ == '__main__': 41 reactor.listenTCP(1234, EchoFactory()) 42 reactor.run()