httpx.AsyncClient报错ProxyError: 504 Gateway Time-out
## 场景:
同一个URL,用requests.get能正常获取网页内容,用httpx.get和httpx.AsyncClient.get就不行,要么就报超时,要么就报ProxyError: 504 Gateway Time-out
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即可
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)