Skip to content

故障排查

本页面列出了一些在使用 HTTPX 开发过程中可能遇到的常见问题及其解决方法。

代理


使用代理进行 HTTPS 请求时出现 "The handshake operation timed out" 错误

描述:当你使用代理并发起 HTTPS 请求时,可能会看到如下异常:

httpx.ProxyError: _ssl.c:1091: The handshake operation timed out

相关问题encode/httpx#1412, encode/httpx#1433

解决方法:你很可能像这样设置了代理……

mounts = {
  "http://": httpx.HTTPTransport(proxy="http://myproxy.org"),
  "https://": httpx.HTTPTransport(proxy="https://myproxy.org"),
}

使用这种设置,你告诉 HTTPX 对于 HTTP 请求使用 HTTP 代理,对于 HTTPS 请求使用 HTTPS 代理。

但如果你遇到上述错误,很可能是你的代理并不支持通过 HTTPS 方式连接。别担心,这是一个常见的陷阱

请将你的 HTTPS 代理的协议更改为 http://...,而不是 https://...

mounts = {
  "http://": httpx.HTTPTransport(proxy="http://myproxy.org"),
  "https://": httpx.HTTPTransport(proxy="http://myproxy.org"),
}

这可以简化为:

proxy = "http://myproxy.org"
with httpx.Client(proxy=proxy) as client:
  ...

更多信息请参见 代理:FORWARD vs TUNNEL


向 HTTPS 代理发起请求时报错

描述:你的代理 确实 支持通过 HTTPS 方式连接,但你遇到了如下错误……

httpx.ProxyError: [SSL: PRE_MAC_LENGTH_TOO_LONG] invalid alert (_ssl.c:1091)

相关问题encode/httpx#1424

解决方法:HTTPX 目前尚未完全支持 HTTPS 代理。如果你对此有需求,请参见 encode/httpx#1434,欢迎参与贡献。