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)
相关推荐
许杰小刀11 小时前
Python网络请求库,从 requests 到 httpx
开发语言·python·httpx
AI_Claude_code1 天前
ZLibrary访问困境方案三:Web代理与轻量级转发服务的搭建与优化
爬虫·python·web安全·搜索引擎·网络安全·web3·httpx
万粉变现经纪人1 天前
如何解决 import aiohttp ModuleNotFoundError: No module named ‘aiohttp’
python·scrapy·beautifulsoup·aigc·pillow·pip·httpx
爱淋雨的男人1 天前
网关gateway详解
gateway
不吃鱼的猫7482 天前
Janus WebRTC Gateway -- 从零搭建完整指南
gateway·webrtc
乔克19982 天前
代理连接失败的问题
python·httpx
zhojiew2 天前
关于AWS Direct Connect with Transit Gateway和Direct Connect Gateway
云计算·gateway·aws
亚马逊云开发者2 天前
GameLift Servers DDoS防护实战:Player Gateway + Ping Beacons延迟优化 + C++ SDK集成
c++·gateway·ddos
戮戮2 天前
Spring Cloud Gateway 零拷贝参数校验:一种高性能网关架构实践
java·网络·架构·gateway
记忆张量MemTensor2 天前
SkillsVote:从技能目录走向终极技能网关(Ultimate Skill Gateway)
gateway