【问题标题】:External API Request with Proxy within Django在 Django 中使用代理的外部 API 请求
【发布时间】:2020-07-26 12:56:30
【问题描述】:

我在127.0.0.1:8080 有一个本地代理服务

我可以用它进行 api 调用,但不能在 django 中,由于某种原因,它可以在常规 python 代码(即python test.py)中工作,但不能在 django 中。

import requests

proxies = {
    'http': 'http://127.0.0.1:8080',
    'https': 'https://127.0.0.1:8080',
}

response = requests.get('https://api.ipify.org?format=json', proxies=proxies)

django v3.0.8 和请求 v2.24.0

我假设 WSGI 以某种方式覆盖了我的代理设置

错误

HTTPSConnectionPool(host='api.ipify.org', port=443): Max retries exceeded with url: /?format=json (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7fa922093340>: Failed to establish a new connection: [Errno 111] Connection refused')))

【问题讨论】:

    标签: python django python-requests wsgi


    【解决方案1】:

    对于遇到此问题的任何人,这是一个 docker 网络问题。 docker 容器中的127.0.0.1 引用内部 docker 网络,而不是本地环境的网络。使用172.17.0.1 指定您的本地环境。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-04
      • 1970-01-01
      • 2020-07-27
      • 2019-06-20
      • 2022-11-07
      • 1970-01-01
      • 2021-04-08
      相关资源
      最近更新 更多