Last Updated: February 25, 2016
·
6.308K
· romario8

how to run flask on tornado server

Here is my snippet

# -*- coding: utf-8 -*-
from tornado.wsgi import WSGIContainer
from tornado.ioloop import IOLoop
from tornado.web import FallbackHandler, RequestHandler, Application
from app import app


class MainHandler(RequestHandler):
    def get(self):
        self.write("This message comes from Tornado ^_^")

tr = WSGIContainer(app)

application = Application([
    (r"/tornado", MainHandler),
    (r".*", FallbackHandler, dict(fallback=tr)),
])

if __name__ == "__main__":
    application.listen(80, address='0.0.0.0')
    IOLoop.instance().start()

Same for SSL connection

# -*- coding: utf-8 -*-
from tornado.wsgi import WSGIContainer
from tornado.ioloop import IOLoop
from tornado.web import FallbackHandler, RequestHandler, Application
from tornado.httpserver import HTTPServer
from app import app


class MainHandler(RequestHandler):
    def get(self):
        self.write("This message comes from Tornado ^_^")

tr = WSGIContainer(app)

application = Application([
    (r"/tornado", MainHandler),
    (r".*", FallbackHandler, dict(fallback=tr)),
])

http_server = HTTPServer(application, ssl_options={'certfile': '/etc/ca.crt', 'keyfile': '/etc/ca.key'})

if __name__ == "__main__":
    http_server.listen(443, address='0.0.0.0')
    IOLoop.instance().start()