gpt4 book ai didi

python - Django 中带有代理的外部 API 请求

转载 作者:行者123 更新时间:2023-12-03 20:50:35 24 4
gpt4 key购买 nike

我在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com