故障排除
¥Troubleshooting
本页列出了使用 HTTPX 开发时可能遇到的一些常见问题以及可能的解决方案。
¥This page lists some common problems or issues you could encounter while developing with HTTPX, as well as possible solutions.
代理
¥Proxies
“The handshake operation timed out“使用代理时对 HTTPS 请求
¥"The handshake operation timed out" on HTTPS requests when using a proxy
描述:当使用代理并发出 HTTPS 请求时,您会看到如下异常:
¥Description: When using a proxy and making an HTTPS request, you see an exception looking like this:
httpx.ProxyError: _ssl.c:1091: The handshake operation timed out
类似问题:编码/httpx#1412,编码/httpx#1433
¥Similar issues: encode/httpx#1412, encode/httpx#1433
解决:您很可能已经像这样设置了代理……
¥Resolution: it is likely that you've set up your proxies like this...
mounts = {
"http://": httpx.HTTPTransport(proxy="http://myproxy.org"),
"https://": httpx.HTTPTransport(proxy="https://myproxy.org"),
}
使用此设置,您告诉 HTTPX 使用 HTTP 连接到代理以进行 HTTP 请求,并使用 HTTPS 连接到 HTTPS 请求。
¥Using this setup, you're telling HTTPX to connect to the proxy using HTTP for HTTP requests, and using HTTPS for HTTPS requests.
但如果您遇到上述错误,则可能是您的代理不支持通过 HTTPS 连接。别担心:这是常见问题。
¥But if you get the error above, it is likely that your proxy doesn't support connecting via HTTPS. Don't worry: that's a common gotcha.
将 HTTPS 代理的方案更改为http://...而不是https://...:
¥Change the scheme of your HTTPS proxy to http://... instead of https://...:
mounts = {
"http://": httpx.HTTPTransport(proxy="http://myproxy.org"),
"https://": httpx.HTTPTransport(proxy="http://myproxy.org"),
}
这可以简化为:
¥This can be simplified to:
proxy = "http://myproxy.org"
with httpx.Client(proxy=proxy) as client:
...
有关详细信息,请参阅代理:FORWARD 与 TUNNEL。
¥For more information, see Proxies: FORWARD vs TUNNEL.
向 HTTPS 代理发出请求时出错
¥Error when making requests to an HTTPS proxy
描述:你的代理做支持通过 HTTPS 连接,但您会看到以下错误...
¥Description: your proxy does support connecting via HTTPS, but you are seeing errors along the lines of...
httpx.ProxyError: [SSL: PRE_MAC_LENGTH_TOO_LONG] invalid alert (_ssl.c:1091)
类似问题:编码/httpx#1424。
¥Similar issues: encode/httpx#1424.
解决:HTTPX 目前无法正确支持 HTTPS 代理。如果您对此感兴趣,请参阅编码/httpx#1434并考虑伸出援手。
¥Resolution: HTTPX does not properly support HTTPS proxies at this time. If that's something you're interested in having, please see encode/httpx#1434 and consider lending a hand there.