Hacker News new | past | comments | ask | show | jobs | submit login
Building an Echo Server Using Tornado (superjared.com)
37 points by superjared on Sept 15, 2009 | hide | past | favorite | 9 comments



Echo Server in Twisted Matrix:

  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()


Exactly, thanks for posting this (you beat me to it).

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.


Yet, it is just nice seeing how tornado can be re-used outside the core web development.


Echo Server in Eventlet (http://eventlet.net/):

    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)


You can destructure the address tuple in the arguments:

  def handle_socket((sock, ip)):
      while True:
         sock.send(sock.recv(1))


Yeah; I didn't want to confuse people who don't know that Eventlet's API passes a tuple (they might think it sends handle_socket 2 arguments)


Echo server in 10-years old Tcl (more?):

    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.


Tornados are dangerous, but I'm glad you're eager to serve their echos.


this is a nice joke. wondering why do hackers vote that down




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: