与CherryPy一起使用Flask来提供静态文件 [英] Using Flask with CherryPy to serve static files

查看:520
本文介绍了与CherryPy一起使用Flask来提供静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的目标是有一个服务器,可以返回基于URL的静态文件传入。我使用Flask作为我的Web框架,我打算使用CherryPy作为我的Web服务器。网络描述了许多设置CherryPy的Flask的方法,我不知道我是否正确地做了。



我一直在使用的资源:





我的Flask应用程序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()

运行CherryPy的脚本: p>

 导入os 
导入cherrypy $ b $从测试导入应用
从cherrypy导入wsgiserver

def option_1():
cherrypy.tree.graft(app,'/')

#如果我注释掉,服务器工作
#cherrypy。 tree.mount(None,'/',config = {
#'/':{
#'tools.staticd ir.on':True,
#'tools.staticdir.dir':app.static_folder
#},
#})

cherrypy.config.update ({'server.socket_port':5000})

cherrypy.engine.start()
cherrypy.engine.block()


def ()'
d = wsgiserver.WSGIPathInfoDispatcher({'/':app})
server = b $ b server.start()
除了KeyboardInterrupt:
server.stop()

if __name__ =='__main__':
#option_1()
option_2()

我有两个问题:


  1. 在设置CherryPy运行Flask方面,option_1和option_2都起作用,那么两者有什么不同?

  2. 建议让Web服务器提供静态文件而不是Web框架。我正确地做这个?当我读取响应头时,服务器不是'Werkzeug',所以我假定CherryPy服务器正在发送它。


解决方案

选项1是高级CherryPy



option_1 实际上使用了整个CherryPy web框架(它们的文档指的是应用层)来挂载你的WSGI应用程序 - 插件,工具等可以被充分利用。为了用CherryPy提供静态文件,你需要改变注释掉的代码,如下所示:

  cherrypy.tree.mount (None,'/ static',config = {
'/':{
'tools.staticdir.on':True,
'tools.staticdir.dir':app.static_folder
$,
})



选项2是低级CherryPy



option_2 ,另一方面,简单地使用CherryPy的WSGI服务器实现( CORE图层)为您的Flask应用程序提供服务 - 它不使用任何CherryPy的更类似于框架的方面。如果您还可以通过Flask的路由层提供静态文件,那么您甚至可以删除 WSGIPathInfoDispatcher 中间件,并直接挂载 app 下的 CherryPyWSGIServer 。如果您希望CherryPy管理 / static 路由服务,那么您将需要安装 cherrypy.tools.staticdir.handler / static 路由中的$ c>,如下所示:

  static_handler = tools.staticdir.handler(section ='/',dir = app.static_folder)
d = wsgiserver.WSGIPathInfoDispatcher({'/':app,'/ static':static_handler})


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:

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:

  1. In terms of setting up CherryPy to run Flask, both option_1 and option_2 works, so what is the difference between the two?
  2. 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.

解决方案

Option 1 is high-level CherryPy

option_1 actually uses the entirety of the CherryPy web framework (which their documentation refers to as the APPLICATION layer) to mount your WSGI application - plugins, tools, etc. can be utilized fully. To serve static files with CherryPy you would want to change your commented out code to be something like this:

cherrypy.tree.mount(None, '/static', config={
    '/': {
        'tools.staticdir.on': True,
        'tools.staticdir.dir': app.static_folder
    },
})

Option 2 is low-level CherryPy

option_2, on the other hand, simply makes use of CherryPy's WSGI server implementation (the CORE layer) to serve your Flask app - it does not use any of the more framework-like aspects of CherryPy. If you are fine with serving your static files through Flask's routing layer as well, you can even remove the WSGIPathInfoDispatcher middleware and directly mount app under the CherryPyWSGIServer. If you want CherryPy to manage the /static route serving then you will want to mount an instance of cherrypy.tools.staticdir.handler under the /static route, like so:

static_handler = tools.staticdir.handler(section='/', dir=app.static_folder)
d = wsgiserver.WSGIPathInfoDispatcher({'/': app, '/static': static_handler})

这篇关于与CherryPy一起使用Flask来提供静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆