简体   繁体   English

如何使用 python flask web 服务从服务器发送文件以及如何从客户端获取这些文件?

[英]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.

相关问题 如何使用Flask在Python中将数据从服务器发送到客户端? - How to send data from server to client in Python using Flask? 如何将信息从客户端发送到服务器(Flask-python) - How to send information from client to server (Flask - python) 如何使用python脚本将文件从客户端传输到服务器计算机? - How to Transfer Files from Client to Server Computer by using python script? 如何使用UDP python套接字从服务器向客户端发送大文件(2MB)多个文件 - How to send big image (2MB) multiple files from server to client with UDP python socket 如何将数据从 python 客户端发送到 Django web 服务器? - How to send data from python client to Django web server? 使用python flask从客户端向服务器发送和接收XML文件 - Send and recieve an XML file from client to server using python flask 如何使用 Flask 从服务器中删除文件 - How to delete files from the server with Flask Flask:如何从服务器中删除文件 - Flask: How to delete files from the server 如何将文件从 React/Next.js 客户端 UI 发送到 Node 服务器和 Flask 服务旁边 - How to send file from React/Next.js client UI to Node server and next to Flask service 如何使用 Python 从 Salesforce 获取文件 - How to get files from Salesforce using Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM