我在 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')))
最佳答案
对于遇到此问题的任何人,这是一个 docker 网络问题。 127.0.0.1
在 docker 容器中引用内部 docker 网络,而不是您本地环境的网络。使用 172.17.0.1
指定您的本地环境。
关于python - Django 中带有代理的外部 API 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63100450/