Flask Snippets

Snippets are unofficial and unmaintained.

This is an archived view of user-submitted snippets. Despite being hosted on the Flask site, they are not official. No Flask maintainer has curated or checked the snippets for security, correctness, or design.

Deploying Flask on Cherrypy WSGI server

Posted by esaurito on 2010-05-31 @ 20:52 and filed in Application Structure

CherryPy comes with a WSGI compliant server, so running a Flask application on top of CherryPy is a piece of cake.

Here is the Flask hello world app for reference:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run()

The following snippet let you run the Flask app on top of the WSGI server shipped with CherryPy.

from cherrypy import wsgiserver
from hello import app

d = wsgiserver.WSGIPathInfoDispatcher({'/': app})
server = wsgiserver.CherryPyWSGIServer(('0.0.0.0', 8080), d)

if __name__ == '__main__':
   try:
      server.start()
   except KeyboardInterrupt:
      server.stop()

You can access the server running on 0.0.0.0:8080. WSGIPathInfoDispatcher take as argument a dictionary mapping a path to an application object, so you can easily deploy multiple (Flask) application on a single CherryPy server.

This snippet by esaurito can be used freely for anything you like. Consider it public domain.

Comments