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)
相关推荐
数据知道2 天前
如何使用 httpx + SQLAlchemy 异步高效写入上亿级图片链接与MD5到 PostgreSQL
数据库·postgresql·httpx
daladongba2 天前
Spring Cloud Gateway
java·spring cloud·gateway
潞哥的博客5 天前
Ingress nginx退役,该怎么换,gateway api 上线
运维·gateway·k8s
Gold Steps.7 天前
K8s Gateway-API 标准化流量治理
容器·kubernetes·gateway
曲幽7 天前
重构FastAPI生产部署:用异步网关与无服务器计算应对高并发
python·serverless·fastapi·web·async·httpx·await·asyncio
J_liaty7 天前
Spring Cloud Gateway与LoadBalancer深度整合实战:从基础到进阶
spring·spring cloud·gateway·loadbalancer
L***d6708 天前
SpringColoud GateWay 核心组件
gateway
悟空码字8 天前
Spring Cloud Gateway实战,从零搭建API网关,构建高性能微服务统一入口
java·gateway·springcloud·编程技术·后端开发
i***13248 天前
SpringCloud实战十三:Gateway之 Spring Cloud Gateway 动态路由
java·spring cloud·gateway
9***g6878 天前
SpringCloud Gateway 集成 Sentinel 详解 及实现动态监听Nacos规则配置实时更新流控规则
spring cloud·gateway·sentinel