头图

1、可以使用send_from_directory从目录发送文件,这在某些情况下非常方便。

from flask import Flask, request, send_from_directory
 
# set the project root directory as the static folder, you can set others.
app = Flask(__name__, static_url_path='')
 
@app.route('/js/<path:path>')
def send_js(path):
    return send_from_directory('js', path)
 
if __name__ == "__main__":
    app.run()

2、可以使用app.send_file或app.send_static_file,但强烈建议不要这样做。

因为它可能会导致用户提供的路径存在安全风险。

send_from_directory旨在控制这些风险。

最后,首选方法是使用NGINX或其他Web服务器来提供静态文件,将能够比Flask更有效地做到这一点。

以上就是本次分享的全部内容,现在想要学习编程的小伙伴指路Python技术大本营,欢迎各位的到来哦~


Python技术大本营
46 声望17 粉丝

关注公粽号:Python编程学习圈,可免费领取最新Python技术资料包。