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
Comment by aliane abdelouahab on 2011-11-22 @ 13:58
hi am sorry, but from Firebug, it seems that the server is still Werkzeug!!!
Maybe a little wrong by Cosmia Luna on 2012-03-12 @ 06:12
aliane abdelouahab wrote: hi am sorry, but from Firebug, it seems that the server is still Werkzeug!!!
The following code should work:
from cherrypy import wsgiserver from hello import app
d = wsgiserver.WSGIPathInfoDispatcher({'/': app.wsgi_app}) server = wsgiserver.CherryPyWSGIServer(('0.0.0.0', 8080), d)
if __name__ == '__main__': try: server.start() except KeyboardInterrupt: server.stop()
i see werkzeug is still activated during routing by scape on 2012-06-22 @ 02:28
File "C:\Python27\werkzeug\werkzeug\routing.py", line 1423, in match raise NotFound() NotFound: 404: Not Found
from this: @app.route("/") meanwhile, cherrypy is still properly serving and routing the urls. I also tried Cosmia's update, it ran but did not change the behavior.