First time using a web framework, hope to get advice on the correct approach.
My aim is to have a server which can return static files based on a url passed in. I use Flask as my web framework and I intent to use CherryPy as my web server. The web describes many ways of setting up Flask with CherryPy and I am not sure if I am doing it correctly.
Resources I have been using:
- http://rhodesmill.org/brandon/2011/wsgi-under-cherrypy/
- Flask, CherryPy and static content
- http://fgimian.github.io/blog/2012/12/08/setting-up-a-rock-solid-python-development-web-server/
- http://flask.pocoo.org/docs/quickstart/
- http://flask.pocoo.org/snippets/24/
A simplified version of my Flask app, test.py:
from flask import Flask
from flask import request
from flask import send_from_directory
import os
FOLDER='contents'
ROOT=os.path.abspath(os.path.join('.', FOLDER))
@app.route("/get")
def route_3():
return flask.send_from_directory(os.path.join(ROOT, 'p01', 'p02'), 'file12.zip', as_attachment=True)
if __name__ == "__main__":
app.config.update(DEBUG=True)
app.run()
My script for to run CherryPy:
import os
import cherrypy
from test import app
from cherrypy import wsgiserver
def option_1():
cherrypy.tree.graft(app, '/')
# If I comment this out, the server works
#cherrypy.tree.mount(None, '/', config={
# '/': {
# 'tools.staticdir.on': True,
# 'tools.staticdir.dir': app.static_folder
# },
# })
cherrypy.config.update({'server.socket_port': 5000})
cherrypy.engine.start()
cherrypy.engine.block()
def option2():
d = wsgiserver.WSGIPathInfoDispatcher({'/': app})
server = wsgiserver.CherryPyWSGIServer(('127.0.0.1', 5000), d)
try:
server.start()
except KeyboardInterrupt:
server.stop()
if __name__ == '__main__':
#option_1()
option_2()
I have two questions:
- In terms of setting up CherryPy to run Flask, both option_1 and option_2 works, so what is the difference between the two?
- It is recommended to have the web server serve the static files as opposed to the web framework. Am I doing this correctly? When I read the response header, the server is not ' Werkzeug', so I am assuming that the CherryPy server is sending it.