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)
相关推荐
JH30731 天前
Gateway 中能写 Servlet Filter 吗?
servlet·gateway
黄俊懿2 天前
【深入理解SpringCloud微服务】Gateway源码解析
java·后端·spring·spring cloud·微服务·gateway·架构师
东东的脑洞2 天前
【面试突击】Spring Security + OAuth2 密码模式实战:Gateway 作为网关与资源服务器,Auth 作为认证服务器(完整认证链路解析)
spring·面试·gateway
清水白石0082 天前
《requests vs httpx:Python 网络请求库的全面对比与实战指南》
网络·python·httpx
JavaBoy_XJ3 天前
spring-gateway配置详解
spring·bootstrap·gateway
黄俊懿5 天前
【深入理解SpringCloud微服务】Gateway简介与模拟Gateway手写一个微服务网关
spring boot·后端·spring·spring cloud·微服务·gateway·架构师
Hello.Reader5 天前
Flink SQL 的 JOB 管理语句SHOW / DESCRIBE / STOP(SQL CLI & SQL Gateway 实战)
sql·flink·gateway
骚戴7 天前
n1n:从替代LiteLLM Proxy自建网关到企业级统一架构的进阶之路
人工智能·python·大模型·llm·gateway·api
骚戴7 天前
LLM API Gateway:LLM API 架构、AI 聚合与成本优化全解(2025深度指南)
人工智能·python·大模型·llm·gateway·api
serendipity_hky10 天前
【SpringCloud | 第4篇】Gateway网关统一入口
spring·spring cloud·微服务·gateway