Flask中提供靜態(tài)文件的實(shí)例講解
1、可以使用send_from_directory從目錄發(fā)送文件,這在某些情況下非常方便。
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,但強(qiáng)烈建議不要這樣做。
因?yàn)樗赡軙?huì)導(dǎo)致用戶提供的路徑存在安全風(fēng)險(xiǎn)。
send_from_directory旨在控制這些風(fēng)險(xiǎn)。
最后,首選方法是使用NGINX或其他Web服務(wù)器來(lái)提供靜態(tài)文件,將能夠比Flask更有效地做到這一點(diǎn)。
知識(shí)點(diǎn)補(bǔ)充:
如何在Flask中提供靜態(tài)文件
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)
到此這篇關(guān)于Flask中提供靜態(tài)文件的實(shí)例講解的文章就介紹到這了,更多相關(guān)Flask中如何提供靜態(tài)文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python集合基本概念與相關(guān)操作實(shí)例分析
這篇文章主要介紹了Python集合基本概念與相關(guān)操作,結(jié)合實(shí)例形式分析了Python集合的功能、原理、基本使用方法及操作注意事項(xiàng),需要的朋友可以參考下2019-10-10詳解python函數(shù)的閉包問(wèn)題(內(nèi)部函數(shù)與外部函數(shù)詳述)
這篇文章主要介紹了python函數(shù)的閉包問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05Python實(shí)現(xiàn)調(diào)用jar或執(zhí)行java代碼的方法詳解
這篇文章主要介紹了Python實(shí)現(xiàn)調(diào)用jar或執(zhí)行java代碼的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12Python 新建文件夾與復(fù)制文件夾內(nèi)所有內(nèi)容的方法
今天小編就為大家分享一篇Python 新建文件夾與復(fù)制文件夾內(nèi)所有內(nèi)容的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10淺談PyQt5 的幫助文檔查找方法,可以查看每個(gè)類的方法
今天小編就為大家分享一篇淺談PyQt5 的幫助文檔查找方法,可以查看每個(gè)類的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06Python Social Auth構(gòu)建靈活而強(qiáng)大的社交登錄系統(tǒng)實(shí)例探究
這篇文章主要為大家介紹了Python Social Auth構(gòu)建靈活而強(qiáng)大的社交登錄系統(tǒng)實(shí)例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01淺談Python 命令行參數(shù)argparse寫(xiě)入圖片路徑操作
這篇文章主要介紹了淺談Python 命令行參數(shù)argparse寫(xiě)入圖片路徑操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07使用Python進(jìn)行體育競(jìng)技分析(預(yù)測(cè)球隊(duì)成績(jī))
這篇文章主要介紹了用Python進(jìn)行體育競(jìng)技分析(預(yù)測(cè)球隊(duì)成績(jī)),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05