urllib.error — Exception classes raised by urllib.request

Source code: Lib/urllib/error.py


The urllib.error module defines the exception classes for exceptions raised by urllib.request. The base exception class is URLError.

The following exceptions are raised by urllib.error as appropriate:

exception urllib.error.URLError
exception urllib.error.URLError(
reason: BaseException | str,
filename: str | None = None,
) URLError

(typeshed)

The handlers raise this exception (or derived exceptions) when they run into a problem. It is a subclass of OSError.

reason: BaseException | str

(typeshed)

The reason for this error. It can be a message string or another exception instance.

Changed in version 3.3: URLError used to be a subtype of IOError, which is now an alias of OSError.

exception urllib.error.HTTPError(url, code, msg, hdrs, fp)
exception urllib.error.HTTPError(
url: str,
code: int,
msg: str,
hdrs: Message[str, str],
fp: IO[bytes] | None,
) HTTPError

(typeshed)

Though being an exception (a subclass of URLError), an HTTPError can also function as a non-exceptional file-like return value (the same thing that urlopen() returns). This is useful when handling exotic HTTP errors, such as requests for authentication.

url: str

(typeshed)

Contains the request URL. An alias for filename attribute.

code: int

(typeshed)

An HTTP status code as defined in RFC 2616. This numeric value corresponds to a value found in the dictionary of codes as found in http.server.BaseHTTPRequestHandler.responses.

reason: reason(self: urllib.error.HTTPError) -> str

(typeshed)

This is usually a string explaining the reason for this error. An alias for msg attribute.

headers: headers(self: urllib.error.HTTPError, headers: email.message.Message[str, str]) -> None

(typeshed)

The HTTP response headers for the HTTP request that caused the HTTPError. An alias for hdrs attribute.

Added in version 3.4.

fp: IO[bytes]

(typeshed)

A file-like object where the HTTP error body can be read from.

exception urllib.error.ContentTooShortError(msg, content)
exception urllib.error.ContentTooShortError(
message: str,
content: tuple[str, Message[str, str]],
) ContentTooShortError

(typeshed)

This exception is raised when the urlretrieve() function detects that the amount of the downloaded data is less than the expected amount (given by the Content-Length header).

content: tuple[str, Message[str, str]]

(typeshed)

The downloaded (and supposedly truncated) data.