Python写flask文件下载接口的代码是什么
导读:这篇文章主要为大家详细介绍了Python写flask文件下载接口的代码是什么,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望对大家学习或工作能有帮助。 简述 写一个简单的flask文件下载接...
这篇文章主要为大家详细介绍了Python写flask文件下载接口的代码是什么,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望对大家学习或工作能有帮助。
简述
写一个简单的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标准的io方法,都替换成gevent中的同名方法,遇到io阻塞gevent自动进行协程切换 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()
准备数据:
浏览器输入:http://localhost:8080/download?fileName=test.mp4
下载完成。
总结
没啥好总结的。
如果本文对你有帮助,请点个赞支持一下吧。
关于“Python写flask文件下载接口的代码是什么”的内容就介绍到这,感谢各位的阅读,相信大家对Python写flask文件下载接口的代码是什么已经有了进一步的了解。大家如果还想学习更多知识,欢迎关注网络,小编将为大家输出更多高质量的实用文章!
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: Python写flask文件下载接口的代码是什么
本文地址: https://pptw.com/jishu/650385.html