Python flask实现文件接收保存
使用curl post上传文件:
touch abc.txt curl -F "file=@abc.txt" http://192.168.1.10:5000/upload
使用python flask接收文件:
from flask import Flask, request app = Flask(__name__) @app.route("/upload", methods=["POST"]) def save_file(): data = request.files print("start...") print(type(data)) print(data) file = data['file'] print(file.filename) print(request.headers) # 文件写入磁盘 file.save(file.filename) print("end...") return "已接收保存\n" if __name__ == '__main__': app.run(host="0.0.0.0", port=5000, debug=True)
注意:
1. debug=True仅在调试时使用
2. 后端data['file']里的file和curl命令中"file=@abc.txt"里的file相同即可在后端获取。
参考:
1. https://www.letianbiji.com/python-flask/py-flask-upload-file.html