Skip to content

例外

¥Exceptions

此页面列出了使用 HTTPX 时可能引发的异常。

¥This page lists exceptions that may be raised when using HTTPX.

有关如何处理 HTTPX 异常的概述,请参阅异常(快速入门)

¥For an overview of how to work with HTTPX exceptions, see Exceptions (Quickstart).

异常层次

¥The exception hierarchy

  • HTTP错误

    ¥HTTPError

    • 请求错误

      ¥RequestError

      • 运输错误

        ¥TransportError

        • 超时异常

          ¥TimeoutException

          • 连接超时

            ¥ConnectTimeout

          • 读取超时

            ¥ReadTimeout

          • 写入超时

            ¥WriteTimeout

          • 池超时

            ¥PoolTimeout

        • 网络错误

          ¥NetworkError

          • 连接错误

            ¥ConnectError

          • 读取错误

            ¥ReadError

          • 写入错误

            ¥WriteError

          • 关闭错误

            ¥CloseError

        • 协议错误

          ¥ProtocolError

          • 本地协议错误

            ¥LocalProtocolError

          • 远程协议错误

            ¥RemoteProtocolError

        • 代理错误

          ¥ProxyError

        • 不支持的协议

          ¥UnsupportedProtocol

      • 解码错误

        ¥DecodingError

      • 重定向次数过多

        ¥TooManyRedirects

    • HTTP状态错误

      ¥HTTPStatusError

  • 无效URL

    ¥InvalidURL

  • Cookie冲突

    ¥CookieConflict

  • 流错误

    ¥StreamError

    • 流消耗

      ¥StreamConsumed

    • 未读回复

      ¥ResponseNotRead

    • 请求未读

      ¥RequestNotRead

    • StreamClosed

      ¥StreamClosed


异常类

¥Exception classes

class httpx.HTTPError(message)

基类RequestErrorHTTPStatusError

¥Base class for RequestError and HTTPStatusError.

适用于try...except发出请求时阻塞,然后调用.raise_for_status()

¥Useful for try...except blocks when issuing a request, and then calling .raise_for_status().

例如:

¥For example:

try:
    response = httpx.get("https://www.example.com")
    response.raise_for_status()
except httpx.HTTPError as exc:
    print(f"HTTP Exception for {exc.request.url} - {exc}")
class httpx.RequestError(message, *, request=None)

发出.request()

¥Base class for all exceptions that may occur when issuing a .request().

class httpx.TransportError(message, *, request=None)

在传输 API 级别发生的所有异常的基类。

¥Base class for all exceptions that occur at the level of the Transport API.

class httpx.TimeoutException(message, *, request=None)

超时错误的基类。

¥The base class for timeout errors.

操作已超时。

¥An operation has timed out.

class httpx.ConnectTimeout(message, *, request=None)

连接主机时超时。

¥Timed out while connecting to the host.

class httpx.ReadTimeout(message, *, request=None)

从主机接收数据超时。

¥Timed out while receiving data from the host.

class httpx.WriteTimeout(message, *, request=None)

向主机发送数据超时。

¥Timed out while sending data to the host.

class httpx.PoolTimeout(message, *, request=None)

等待从池中获取连接超时。

¥Timed out waiting to acquire a connection from the pool.

class httpx.NetworkError(message, *, request=None)

网络相关错误的基类。

¥The base class for network-related errors.

与网络交互时发生错误。

¥An error occurred while interacting with the network.

class httpx.ConnectError(message, *, request=None)

无法建立连接。

¥Failed to establish a connection.

class httpx.ReadError(message, *, request=None)

无法从网络接收数据。

¥Failed to receive data from the network.

class httpx.WriteError(message, *, request=None)

无法通过网络发送数据。

¥Failed to send data through the network.

class httpx.CloseError(message, *, request=None)

无法关闭连接。

¥Failed to close a connection.

class httpx.ProtocolError(message, *, request=None)

协议被违反了。

¥The protocol was violated.

class httpx.LocalProtocolError(message, *, request=None)

客户端违反了协议。

¥A protocol was violated by the client.

例如,如果用户实例化了一个Request实例明确,未能包括强制性Host:标头,然后直接使用client.send()

¥For example if the user instantiated a Request instance explicitly, failed to include the mandatory Host: header, and then issued it directly using client.send().

class httpx.RemoteProtocolError(message, *, request=None)

服务器违反了协议。

¥The protocol was violated by the server.

例如返回格式错误的 HTTP。

¥For example, returning malformed HTTP.

class httpx.ProxyError(message, *, request=None)

建立代理连接时发生错误。

¥An error occurred while establishing a proxy connection.

class httpx.UnsupportedProtocol(message, *, request=None)

尝试向不受支持的协议发出请求。

¥Attempted to make a request to an unsupported protocol.

例如向ftp://www.example.com

¥For example issuing a request to ftp://www.example.com.

class httpx.DecodingError(message, *, request=None)

由于编码格式错误,响应解码失败。

¥Decoding of the response failed, due to a malformed encoding.

class httpx.TooManyRedirects(message, *, request=None)

重定向过多。

¥Too many redirects.

class httpx.HTTPStatusError(message, *, request, response)

响应具有错误 HTTP 状态 4xx 或 5xx。

¥The response had an error HTTP status of 4xx or 5xx.

打电话时可能会提出response.raise_for_status()

¥May be raised when calling response.raise_for_status()

class httpx.InvalidURL(message)

URL 格式不正确或无法解析。

¥URL is improperly formed or cannot be parsed.

class httpx.CookieConflict(message)

尝试按名称查找 cookie,但存在多个 cookie。

¥Attempted to lookup a cookie by name, but multiple cookies existed.

调用时可能会发生response.cookies.get(...)

¥Can occur when calling response.cookies.get(...).

class httpx.StreamError(message)

流异常的基类。

¥The base class for stream exceptions.

开发人员以无效的方式访问请求流时出错。

¥The developer made an error in accessing the request stream in an invalid way.

class httpx.StreamConsumed()

尝试读取或流式传输内容,但内容已被流式传输。

¥Attempted to read or stream content, but the content has already been streamed.

class httpx.StreamClosed()

尝试读取或流式传输响应内容,但请求已被关闭。

¥Attempted to read or stream response content, but the request has been closed.

class httpx.ResponseNotRead()

尝试访问流式响应内容,而无需调用read()

¥Attempted to access streaming response content, without having called read().

class httpx.RequestNotRead()

尝试访问流式请求内容,但未调用read()

¥Attempted to access streaming request content, without having called read().