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)
相关推荐
飞火流星0202710 小时前
SkyWalking异步采集spring gateway日志
gateway·skywalking·日志监控·gateway链路监控
要开心吖ZSH10 小时前
【Spring Cloud Gateway 实战系列】高级篇:服务网格集成、安全增强与全链路压测
spring cloud·微服务·gateway·istio
德育处主任Pro10 小时前
解放生产力:Amazon API Gateway 与 Amazon Lambda 的优雅组合
gateway·aws·亚马逊
java叶新东老师10 小时前
spring gateway 配置http和websocket路由转发规则
spring·http·gateway
java叶新东老师11 小时前
五、搭建springCloudAlibaba2021.1版本分布式微服务-gateway网关
分布式·微服务·gateway
云和数据.ChenGuang2 天前
`neutron router-gateway-set` 操作失败的可能原因及解决方案
运维·gateway·运维技术总结·运维技术
要开心吖ZSH3 天前
【Spring Cloud Gateway 实战系列】终极篇:演进方向与未来架构
spring cloud·gateway
@小了白了兔4 天前
统一服务入口——Spring Cloud Gateway
java·spring cloud·gateway
要开心吖ZSH4 天前
【Spring Cloud Gateway 实战系列】进阶篇:过滤器高级用法、动态路由配置与性能优化
spring cloud·性能优化·gateway·负载均衡
亲爱的非洲野猪5 天前
Spring Cloud Gateway 电商系统实战指南:架构设计与深度优化
java·spring cloud·gateway