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)
相关推荐
骚戴13 小时前
AI架构指南:大型语言模型 (LLM) API 的通用集成与企业级配置(2025年)
人工智能·大模型·llm·gateway·api
yangmf204013 小时前
INFINI Gateway 助力联想集团 ES 迁移升级
大数据·数据库·elasticsearch·搜索引擎·gateway·全文检索
时光803.1 天前
快速搭建青龙面板Docker教程
windows·ubuntu·bash·httpx
梵得儿SHI2 天前
SpringCloud 核心组件精讲:Spring Cloud Gateway 网关实战-路由配置 + 过滤器开发 + 限流鉴权(附场景配置模板)
java·spring·spring cloud·gateway·搭建基础网关·现静态/动态路由配置·全局/局部过滤器
JH30734 天前
Gateway 中能写 Servlet Filter 吗?
servlet·gateway
黄俊懿4 天前
【深入理解SpringCloud微服务】Gateway源码解析
java·后端·spring·spring cloud·微服务·gateway·架构师
东东的脑洞5 天前
【面试突击】Spring Security + OAuth2 密码模式实战:Gateway 作为网关与资源服务器,Auth 作为认证服务器(完整认证链路解析)
spring·面试·gateway
清水白石0085 天前
《requests vs httpx:Python 网络请求库的全面对比与实战指南》
网络·python·httpx
JavaBoy_XJ5 天前
spring-gateway配置详解
spring·bootstrap·gateway
黄俊懿7 天前
【深入理解SpringCloud微服务】Gateway简介与模拟Gateway手写一个微服务网关
spring boot·后端·spring·spring cloud·微服务·gateway·架构师