import cherrypy # Import CherryPy server module # Import plugin modules for CherryPy from ws4py.server.cherrypyserver import WebSocketPlugin, WebSocketTool from ws4py.websocket import WebSocket # Import modules for # the ws4py plugin. from ws4py.messaging import TextMessage class ChatWebSocketHandler(WebSocket): def received_message(self, m): msg=m.data.decode("utf-8") print msg cherrypy.engine.publish('websocket-broadcast', "Broadcast Message: Received a message") def closed(self, code, reason="A client left the room without a proper explanation."): cherrypy.engine.publish('websocket-broadcast', TextMessage(reason)) class Root(object): @cherrypy.expose def index(self): return "index" @cherrypy.expose def ws(self): print "Handler created: %s" % repr(cherrypy.request.ws_handler) if __name__ == '__main__': WebSocketPlugin(cherrypy.engine).subscribe() # initialize websocket # plugin cherrypy.tools.websocket = WebSocketTool() # cherrypy.config.update({'server.socket_host': '0.0.0.0', 'server.socket_port': 9003, 'tools.staticdir.root': '/home/pi'}) cherrypy.quickstart(Root(), '', config={ '/ws': { 'tools.websocket.on': True, 'tools.websocket.handler_cls': ChatWebSocketHandler } });