Create a gist now

Instantly share code, notes, and snippets.

Bottle app on Tornado
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from tornado.web import Application, StaticFileHandler
from tornado.websocket import WebSocketHandler
from bottle import get, app
@get('/')
def index():
return 'Welcome'
class MessageDispatcher(object):
clients = []
@classmethod
def add_client(cls):
clients.add(cls)
@classmethod
def remove_client(cls):
clients.remove(cls)
@classmethod
def broadcast(cls, message):
for c in clients:
c.send(message)
class WSHandler(WebSocketHandler):
clients = []
def open(self):
logging.info('Client connected')
MessageDispatcher.add_client(self)
def on_message(self, message):
logging.log('Received message')
if message == 'close':
MessageDispatcher.remove_client(self)
self.close()
def on_close(self):
logging.info('Client disconnected')
MessageDispatcher.remove_client(self)
if __name__ == "__main__":
# How do you mount Bottle's app here?
HTTPServer(Application([(r"/ws", WSHandler),
(r"/css/(.*)", StaticFileHandler, {"path": "./css/"}),
(r"/js/(.*)", StaticFileHandler, {"path": "./js/"}),
(r"/img/(.*)", StaticFileHandler, {"path": "./img/"}),
("/(.*)", MOUNT_APP_HERE)])
).listen(1024)
IOLoop.instance().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment