httpx.AsyncClient报错ProxyError: 504 Gateway Time-out

场景:

同一个URL,用requests.get能正常获取网页内容,用httpx.get和httpx.AsyncClient.get就不行,要么就报超时,要么就报ProxyError: 504 Gateway Time-out

python 复制代码
import anyio, httpx, requests

url = 'https://my-domain.com/xxx/'

r = requests.get(url, verify=False) # 这个ok
r = httpx.get(url, verify=False) # 这个报错了

async def fetch(url):
    async with httpx.AsyncClient(verify=False) as client:
        return await client.get(url)

r = anyio.run(fetch, url) # 这个也报错了

原因:httpx在Windows下,默认读取系统的代理设置,然后它却不能读到跳过代理的白名单,于是导致不该走代理的网址走了代理,从而引发代理错误。

解决:加上trust_env=False即可

python 复制代码
import anyio, httpx, requests

url = 'https://my-domain.com/xxx/'

r = httpx.get(url, verify=False, trust_env=False)

async def fetch(url):
    async with httpx.AsyncClient(verify=False, trust_env=False) as client:
        return await client.get(url)

r = anyio.run(fetch, url)
相关推荐
曲幽3 小时前
从卡顿到丝滑:FastAPI 调用外部 API 的正确姿势(httpx 实战)
python·fastapi·web·async·httpx·client·await·requests·aiohttp
搬砖柯2 天前
系列12-接口压测怎么做成平台能力?自研 httpx 引擎、分布式 Worker 与 Locust 选型对照
分布式·测试工具·性能优化·开源·自动化·httpx
Sean‘4 天前
GitLab 升级后 502:Puma 反复重启问题处理记录
docker·gateway·gitlab
Hadoop_Liang22 天前
使用Kubernetes Gateway API实现域名访问应用
容器·kubernetes·gateway
worilb22 天前
Spring Cloud 学习与实践(9):Gateway + JWT 统一鉴权
学习·spring cloud·gateway
Dontla22 天前
Kong Gateway(OSS)(Open Source Software)与 Kong Gateway(Enterprise)区别
gateway·kong
matrixmind825 天前
HTTPX:Python 下一代 HTTP 客户端
python·其他·http·httpx
yxl8746464625 天前
磐创PCTG-9013 Modbus转ProfibusDP工业协议转换器
网络·科技·物联网·gateway·信息与通信
__log25 天前
401鉴权问题完全指南:从“门卫拦人“到“畅通无阻“
gateway
暗夜猎手-大魔王1 个月前
hermes源码学习8--Gateway 内部机制
人工智能·gateway