Building an Echo Server Using Tornado
superjared.com
superjared.com
from twisted.internet import protocol, reactor
class Echo(protocol.Protocol):
def dataReceived(self, data):
self.transport.write(data)
if __name__ == '__main__':
factory = protocol.ServerFactory()
factory.protocol = Echo
reactor.listenTCP(8000,factory)
reactor.run()It's not that we need to constantly respond to every Tornado post with a Twisted rebuttal - but it is appropriate to respond to articles the contain FUD and demonstrate far less elegant, far more verbose, non cross-platform solutions.
from eventlet import api
def handle_socket(address):
sock, ip = address
while True:
sock.send(sock.recv(1))
api.tcp_server(api.tcp_listener(('0.0.0.0', 6000)), handle_socket) def handle_socket((sock, ip)):
while True:
sock.send(sock.recv(1)) socket -server acceptConnections 9999
proc acceptConnections {fd clientaddr clientport} {
fconfigure $fd -blocking 0
fileevent $fd readable [list replyToClient $fd]
}
proc replyToClient fd {
set s [read $fd]
puts -nonewline $fd $s
flush $fd
}
vwait forever
Note that this is a multiplexing non blocking server, exactly like Twisted / Tornado. All features built-in in the language. Tcl failed copying the obvious missing features from other languages. Other languages are failing copying the good things from Tcl.p.s. note the poetry contained in the "vwait forever" statement ;) Our program is willing to run without stop until the end of the universe.