python 詳解如何寫flask文件下載接口
發(fā)布日期:2021-12-18 04:39 | 文章來源:源碼中國
簡述
寫一個簡單的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自動進行協(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é)
沒啥好總結(jié)的。
如果本文對你有幫助,請點個贊支持一下吧。

到此這篇關(guān)于python 詳解如何寫flask文件下載接口的文章就介紹到這了,更多相關(guān)python flask下載內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
相關(guān)文章
關(guān)注官方微信