如何在烧瓶中提供不同的文件夹?

时间:2016-06-29 01:55:33

标签: http flask

我只是在尝试烧瓶。我正在尝试提供除烧瓶服务器启动之外的其他文件夹。这就是我想出来的。

alt_foler = '/folder/withindex/'
app = Flask('test')

@app.route('/')
@app.route('/<path:path>')
def redirect(path):
     return alt_folder + path

但是当我给出127.0.0.1/index.html时,我只看到

'/folder/withindex/index.html' instead of actually serving the index.html file located at the given location. How to set this correctly?

2 个答案:

答案 0 :(得分:1)

虽然您应该使用像Nginx或Apache这样的HTTP服务器来提供静态文件,但如果您坚持使用Flask,则可以使用send_from_directory

@app.route('/<path:path>')
def redirect(path):
    return send_from_directory(alt_folder, path)

答案 1 :(得分:0)

我想出来了。

实际上,重定向功能应该返回文件的内容而不是文件。

def redirect(path):
    return open(alt_folder + path, 'r').read()
相关问题