urllib.error — Exception classes raised by urllib.request

Source code:Lib/urllib/error.py


Theurllib.error module defines the exception classes for exceptionsraised byurllib.request. The base exception class isURLError.

The following exceptions are raised byurllib.error as appropriate:

exceptionurllib.error.URLError

The handlers raise this exception (or derived exceptions) when they run intoa problem. It is a subclass ofOSError.

reason

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

Changed in version 3.3:URLError used to be a subtype ofIOError, which is now analias ofOSError.

exceptionurllib.error.HTTPError

Though being an exception (a subclass ofURLError), anHTTPError can also function as a non-exceptional file-like returnvalue (the same thing thaturlopen() returns). Thisis useful when handling exotic HTTP errors, such as requests forauthentication.

code

An HTTP status code as defined inRFC 2616. This numeric value correspondsto a value found in the dictionary of codes as found inhttp.server.BaseHTTPRequestHandler.responses.

reason

This is usually a string explaining the reason for this error.

headers

The HTTP response headers for the HTTP request that caused theHTTPError.

New in version 3.4.

exceptionurllib.error.ContentTooShortError(msg,content)

This exception is raised when theurlretrieve()function detects thatthe amount of the downloaded data is less than the expected amount (given bytheContent-Length header).

content

The downloaded (and supposedly truncated) data.