容器与python请求的通信 [英] Containers communication with python requests

查看:66
本文介绍了容器与python请求的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的体系结构:

我有两个容器(A和B)在具有各自网络的同一主机上运行.

I have two containers (A and B) running on the same host with their own network.

docker-compose:

docker-compose :

version : '3'
services: 

  A: 
    build: ./docker_A
    ports: 
      - "8090:8090"
    networks:
      - my_network

  B:
    build: ./docker_B
    ports: 
      - "8070:8070"    
    networks:
      - my_network

networks: 
  my_network:
    driver : bridge

容器b正在运行瓶服务器:

@get('/')
def hello():
    return {"say":"Hello world"}

run(host='0.0.0.0', port=8070, debug=True)

docker inspect package_name_my_network 返回:

{
        "Name": "package_name_my_network",
        "Id": "...",
        "Created": "...",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.18.0.0/16",
                    "Gateway": "172.18.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "...": {
                "Name": "package_name_A",
                "EndpointID": "...",
                "MacAddress": "...",
                "IPv4Address": "172.18.0.3/16",
                "IPv6Address": ""
            },
            "...": {
                "Name": "package_name_B",
                "EndpointID": "...",
                "MacAddress": "...",
                "IPv4Address": "172.18.0.2/16",
                "IPv6Address": ""
            }

我正在尝试从A发出 GET 请求:

I'm trying to do a GET request from A :

ret = requests.get('http://172.18.0.2:8070/')

但是,每当我收到此响应时:< Response [503]>/网络错误(tcp_error)

But each time I get this response : <Response [503]> / Network Error (tcp_error)

我尝试了几件事:

将172.18.0.2更改为:

Changed 172.18.0.2 to :

  • B:KO
  • name_package_B:KO
  • 0.0.0.0:KO

从主机上:

curl -X GET http://172.18.0.2:8070/:{"say":"Hello world"}

从A: docker exec -t -i package_name_A/bin/bash :我可以ping:

From A : docker exec -t -i package_name_A /bin/bash : I can ping :

  • 172.18.0.2
  • B
  • package_name_B

如果有人有解决方案,那就太好了.

If someone has a solution, it would be wonderful.

感谢您的时间.

这是一个代理问题.如果我未设置 https_proxy unset http_proxy ,则可以通过A到达B.问题是,我无法访问主机上的服务器(不是容器).即使使用 EXPORT no_proxy = 172.0.0.0 ,有什么想法吗?

It's a proxy problem. If I do unset https_proxy and unset http_proxy, I'm able to reach B with A. The problem is, I can't reach the server on the host (not a container) anymore. Even with EXPORT no_proxy=172.0.0.0 Any Idea ?

推荐答案

已解决:这是一个代理问题.我正在使用没有env var的会话:

Solved : It was a proxy problem. I'm using session without env var :

session = requests.Session()
session.trust_env = False
ret = session.get(url, json=my_json)

这篇关于容器与python请求的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆