So this is embarrassing. I've got an application that I threw together in Flask and for now it is just serving up a single static HTML page with some links to CSS and JS. And I can't find where in the documentation Flask describes returning static files. Yes, I could use render_template but I know the data is not templatized. I'd have thought send_file or url_for was the right thing, but I could not get those to work. In the meantime, I am opening the files, reading content, and rigging up a Response with appropriate mimetype:
import os.path
from flask import Flask, Response
app = Flask(__name__)
app.config.from_object(__name__)
def root_dir(): # pragma: no cover
return os.path.abspath(os.path.dirname(__file__))
def get_file(filename): # pragma: no cover
try:
src = os.path.join(root_dir(), filename)
# Figure out how flask returns static files
# Tried:
# - render_template
# - send_file
# This should not be so non-obvious
return open(src).read()
except IOError as exc:
return str(exc)
@app.route('/', methods=['GET'])
def metrics(): # pragma: no cover
content = get_file('jenkins_analytics.html')
return Response(content, mimetype="text/html")
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def get_resource(path): # pragma: no cover
mimetypes = {
".css": "text/css",
".html": "text/html",
".js": "application/javascript",
}
complete_path = os.path.join(root_dir(), path)
ext = os.path.splitext(path)[1]
mimetype = mimetypes.get(ext, "text/html")
content = get_file(complete_path)
return Response(content, mimetype=mimetype)
if __name__ == '__main__': # pragma: no cover
app.run(port=80)
有人愿意给出一个代码示例或url吗?我知道这非常简单。
所以我把事情搞定了(基于@user1671599的答案),想和你们分享。
(我希望我做得对,因为这是我在Python中的第一个应用程序)
我做了这个
项目结构:
server.py:
from server.AppStarter import AppStarter
import os
static_folder_root = os.path.join(os.path.dirname(os.path.abspath(__file__)), "client")
app = AppStarter()
app.register_routes_to_resources(static_folder_root)
app.run(__name__)
AppStarter.py:
from flask import Flask, send_from_directory
from flask_restful import Api, Resource
from server.ApiResources.TodoList import TodoList
from server.ApiResources.Todo import Todo
class AppStarter(Resource):
def __init__(self):
self._static_files_root_folder_path = '' # Default is current folder
self._app = Flask(__name__) # , static_folder='client', static_url_path='')
self._api = Api(self._app)
def _register_static_server(self, static_files_root_folder_path):
self._static_files_root_folder_path = static_files_root_folder_path
self._app.add_url_rule('/<path:file_relative_path_to_root>', 'serve_page', self._serve_page, methods=['GET'])
self._app.add_url_rule('/', 'index', self._goto_index, methods=['GET'])
def register_routes_to_resources(self, static_files_root_folder_path):
self._register_static_server(static_files_root_folder_path)
self._api.add_resource(TodoList, '/todos')
self._api.add_resource(Todo, '/todos/<todo_id>')
def _goto_index(self):
return self._serve_page("index.html")
def _serve_page(self, file_relative_path_to_root):
return send_from_directory(self._static_files_root_folder_path, file_relative_path_to_root)
def run(self, module_name):
if module_name == '__main__':
self._app.run(debug=True)
在生产环境中,在应用程序前面配置HTTP服务器(Nginx, Apache等),从静态文件夹向/static提供请求。专用的web服务器非常擅长有效地提供静态文件,尽管与低容量的Flask相比,您可能不会注意到差异。
Flask自动创建一个/static/<path:filename>路由,它将在定义你的Flask应用程序的Python模块旁边的静态文件夹下提供任何文件名。使用url_for链接到静态文件:url_for('static', filename='js/ analysis .js')
您还可以使用send_from_directory在自己的路由中提供来自某个目录的文件。这接受一个基本目录和一个路径,并确保该路径包含在目录中,从而可以安全地接受用户提供的路径。如果您希望在提供文件之前检查某些内容,例如登录用户是否具有权限,那么这可能非常有用。
from flask import send_from_directory
@app.route('/reports/<path:path>')
def send_report(path):
return send_from_directory('reports', path)
不要对用户提供的路径使用send_file或send_static_file。这将使您暴露于目录遍历攻击。Send_from_directory的设计目的是安全地处理已知目录下用户提供的路径,如果该路径试图逃离该目录,则会引发错误。
如果在内存中生成一个文件而不将其写入文件系统,则可以将BytesIO对象传递给send_file,使其像文件一样提供服务。在本例中,您需要将其他参数传递给send_file,因为它不能推断文件名或内容类型等内容。