python 詳解如何寫(xiě)flask文件下載接口
簡(jiǎn)述
寫(xiě)一個(gè)簡(jiǎn)單的flask文件下載接口。
依賴
flask、gevent
代碼
不廢話上代碼。
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Sat Oct 23 19:53:18 2021 @author: huyi """ from flask import Flask, request, make_response, send_from_directory from gevent.pywsgi import WSGIServer from gevent import monkey # 將python標(biāo)準(zhǔn)的io方法,都替換成gevent中的同名方法,遇到io阻塞gevent自動(dòng)進(jìn)行協(xié)程切換 monkey.patch_all() app = Flask(__name__) @app.route("/download", methods=['GET']) def download_file(): get_data = request.args.to_dict() file_path = get_data.get('fileName') response = make_response( send_from_directory('/Users/huyi/Movies/Videos',file_path,as_attachment=True)) response.headers["Content-Disposition"] = "attachment; filename={}".format( file_path.encode().decode('latin-1')) return response if __name__ == '__main__': WSGIServer(('0.0.0.0', 8080), app).serve_forever()
準(zhǔn)備數(shù)據(jù):
瀏覽器輸入:http://localhost:8080/download?fileName=test.mp4
下載完成。
總結(jié)
沒(méi)啥好總結(jié)的。
如果本文對(duì)你有幫助,請(qǐng)點(diǎn)個(gè)贊支持一下吧。
到此這篇關(guān)于python 詳解如何寫(xiě)flask文件下載接口的文章就介紹到這了,更多相關(guān)python flask下載內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。