简体   繁体   English

Python3.8.5 使用 http 请求将文件从一个 docker 容器发送到另一个容器的问题

[英]Python3.8.5 problem to send file from one docker container to another using http request

I have a problem with my python(python3.8.5) project.我的 python(python3.8.5) 项目有问题。 I have two docker containers.我有两个 docker 容器。 One is used for the frontend(container2) using flask.一个用于使用 flask 的前端(容器 2)。 There I have a website where I can send requests(package requests) to the second container(backend).在那里我有一个网站,我可以将请求(包请求)发送到第二个容器(后端)。 In the backend(container1) I have to create a zip file containing multiple file.在后端(container1)中,我必须创建一个包含多个文件的 zip 文件。 That part is done.那部分已经完成。 Now the problem is how can I get this file to the frontend(container2) and from there I need to download it via the website.现在的问题是我怎样才能把这个文件放到前端(container2),然后我需要从那里通过网站下载它。

So I need a solution to send a http request (now I have requests.get(URL to backend)) from the frontend to the backend.所以我需要一个解决方案来从前端向后端发送 http 请求(现在我有 requests.get(URL to backend))。 In the response I need the zip file which I can download then from the website.在响应中,我需要 zip 文件,然后我可以从网站下载该文件。

I googled already many hours but I cannot find a good solution.我已经用谷歌搜索了好几个小时,但我找不到一个好的解决方案。 I thing it would be good if I don't have to store the zip file on the frontend and on the backend.我认为如果我不必在前端和后端存储 zip 文件会很好。 If possible I would like to have it stored just on the backend and send it to the frontend and directly download it.如果可能的话,我希望将它存储在后端并将其发送到前端并直接下载。

I hope you understand my problem and can help me.我希望你能理解我的问题并能帮助我。 Br

A probably incomplete solution, that does what you describe.一个可能不完整的解决方案,这就是你所描述的。 Depending on other concerns (like file size) this might not be what you really want, but it works.根据其他问题(如文件大小),这可能不是您真正想要的,但它确实有效。

As an example I have 2 flask servers: flask1 (backend) and flask2 (frontend):例如,我有 2 个 flask 服务器:flask1(后端)和 flask2(前端):

flask1:烧瓶1:

from flask import Flask, send_file, request

app = Flask(__name__)

@app.route('/get_file')
def get_image():
    return send_file(request.args.get('name'))

flask2:烧瓶2:

from flask import Flask, send_file, request

import io
import requests

app = Flask(__name__)

@app.route('/get_something')
def get_image():
    r = requests.get("http://localhost:5000/get_file?name="+request.args.get('name'))
    return  send_file(io.BytesIO(r.content), mimetype=r.headers['Content-Type'],
                      as_attachment=True, attachment_filename="file")

To test you can run them on different ports, like:要进行测试,您可以在不同的端口上运行它们,例如:

FLASK_APP=flask1.py flask run --port=5000
FLASK_APP=flask2.py flask run --port=8080

Then if you go to the browser and access the frontend with an URL like:然后,如果您使用 go 访问浏览器并使用 URL 访问前端,例如:

http://localhost:8080/get_something?name=download.png http://localhost:8080/get_something?name=download.png

the frontend will "pass" the request to the backend, then re-send what it receives, hence returning the file.前端会将请求“传递”到后端,然后重新发送它收到的内容,从而返回文件。

Things that code above does not address: file naming and errors.上面的代码没有解决的事情:文件命名和错误。

You could proxy the request from frontend.您可以代理来自前端的请求。 I assume you use something like react.我假设您使用诸如反应之类的东西。 So if you add a line like:因此,如果您添加如下行:

"proxy": "http://backend-container:8080"

to your package.json, frontend should proxy to the backend.到您的 package.json,前端应该代理到后端。 Now you can send requests like http://front-end/api/hello and it will hit the backend.现在你可以发送像 http://front-end/api/hello 这样的请求,它会到达后端。 Not sure if it is the best solution but might work.不确定这是否是最好的解决方案,但可能会奏效。

examples:例子:

https://create-react-app.dev/docs/proxying-api-requests-in-development/ https://create-react-app.dev/docs/proxying-api-requests-in-development/
https://medium.com/bb-tutorials-and-thoughts/react-how-to-proxy-to-backend-server-5588a9e0347 https://medium.com/bb-tutorials-and-thoughts/react-how-to-proxy-to-backend-server-5588a9e0347
https://www.twilio.com/blog/react-app-with-node-js-server-proxy https://www.twilio.com/blog/react-app-with-node-js-server-proxy

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在 docker 容器上将 python 从 3.5.3 更新到 3.8.5 - Unable to update python from 3.5.3 to 3.8.5 on docker container 如何将 python 文件发送到 docker 容器(使用 python 映像)并获取 Z78E6221F6393D1356681 到本地的 DB398DZF - how to send python file to docker container(using python image) and get the output back to local 如何从文件发送整个http请求? - How to send entire http request from file? 从 python 3.8.5 降级到 python 3.6 - downgrade from python 3.8.5 to python 3.6 如何在 python docker 容器中监听 HTTP ping? - How to listen for HTTP pings in python docker container? 如何从运行在 docker 组合中不同端口上的另一个 flask 容器调用一个容器的端点? - How to call an endpoint of one container from another flask container running on different ports in docker compose? 在 python 中将文本数据从一个 txt 文件复制到另一个文件时出现问题 - Having problem while copying text data from one txt file to another in python 将数据从一个 Python 程序发送到另一个 Python 程序(使用套接字) - Send data from one Python program to another Python program (using sockets) 使用从一个 python 文件到另一个 python 文件的全局变量 - Using global variable from one python file to another 如何使用 docker-compose 设置一个容器以允许它的整个卷访问另一个容器 - how to set one container to allow it's entire volume access to another container using docker-compose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM