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)
相关推荐
曼彻斯特的海边17 小时前
RequestRateLimiterGatewayFilterFactory
spring cloud·gateway·限流
·云扬·17 小时前
【PmHub面试篇】Gateway全局过滤器统计接口调用耗时面试要点解析
面试·职场和发展·gateway
devil_mf17 小时前
gateway 网关 路由新增 (已亲测)
gateway
wmd1316430671217 小时前
Gateway 搭建
gateway
KubeSphere 云原生3 天前
云原生周刊:探索 Gateway API v1.3.0
云原生·gateway
曼彻斯特的海边4 天前
spring-cloud-alibaba-sentinel-gateway
gateway·sentinel·springcloud
xujinwei_gingko6 天前
网关Gateway
微服务·gateway
徐子童6 天前
《Spring Cloud Gateway 快速入门:从路由到自定义 Filter 的完整教程》
java·开发语言·spring cloud·nacos·gateway
亚林瓜子6 天前
AWS API Gateway 配置WAF(中国区)
云计算·gateway·aws·waf
枫super10 天前
Spring Cloud 详解:2025 最新技术与最佳实践
后端·spring·spring cloud·eureka·nacos·gateway·openfeign