[英]how to send files using python flask web service from server and how to get those files from client?
I need to send files from a server using flask web service in python and clients should be to download those files whenever they want by calling that service.我需要使用 python 中的 flask web 服务从服务器发送文件,客户端应该通过调用该服务随时下载这些文件。
I am trying the below code to send files.我正在尝试以下代码来发送文件。
from flask import Flask, send_from_directory
import os
app = Flask(__name__)
@app.route('/', methods=['POST'])
def index():
response = send_from_directory(directory=os.getcwd(), filename='utils.py')
response.headers['my-custom-header'] = 'my-custom-status-0'
return response
if __name__ == '__main__':
check, file_object = app.run("0.0.0.0", port=5000, threaded=True)
To download file from Flask server you can do the following要从 Flask 服务器下载文件,您可以执行以下操作
from flask import send_from_directory
import os
@app.route("/getfile", methods=["GET"])
def getfile():
# WORKDIR = "" if localhost, otherwise you server root path
try:
return send_from_directory(
WORKDIR + os.path.join(os.getcwd(), "upload_folder_name"), "your_filename", as_attachment=True)
except Exception as ex:
logging.warning("Wasn't able to provide get file: {}".format(ex))
return redirect("401.html", 301)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.