# Simple Websocket using Tornado # Ted Patrick (ted@tedpatrick.com) # # A simple websocket server using Python & Tornado # # Make sure to install Tornado before running: # easy_install tornado import webbrowser import tornado.ioloop import tornado.web import tornado.websocket SERVER_PORT = 8888 SERVER_URI = "localhost:" + str( SERVER_PORT ) class EchoWebSocket( tornado.websocket.WebSocketHandler ): def open( self ): print "WebSocket opened" def on_message( self , message ): print "You said: " + message self.write_message( message ) def on_close( self ): print "WebSocket closed" class MainHandler( tornado.web.RequestHandler ): def get( self ): self.write( """ WebSocket Test

Websocket Test

Console: socket.send( "message" ) or socket.close()


""") application = tornado.web.Application([ (r"/", MainHandler), (r"/live", EchoWebSocket), ]) if __name__ == "__main__": application.listen( SERVER_PORT ) webbrowser.open( "http://" + SERVER_URI ) tornado.ioloop.IOLoop.instance().start()