← All notes

Python urllib.error: URLError, HTTPError, and ContentTooShortError

Answer: urllib.error defines the exception types raised by urllib.request, with URLError as the base class.

URLError

Handlers raise URLError, or a subclass, when they hit a problem.

URLError is a subclass of OSError.

Its reason attribute is a message string or another exception instance.

In Python 3.3 it stopped being a subtype of IOError, which is now an alias of OSError.

Since Python 3.16, errno and strerror are set from reason when reason is an OSError, and on Windows winerror is set as well.

HTTPError and ContentTooShortError

HTTPError is a URLError, but it can also act as a file-like return value, like the object urlopen returns.

That file-like shape helps when handling exotic HTTP errors, such as requests for authentication.

url holds the request URL and is an alias for filename.

code is an HTTP status code as defined in RFC 2616, and it matches a value in http.server.BaseHTTPRequestHandler.responses.

reason is usually a string explaining the error and is an alias for msg.

headers holds the HTTP response headers and is an alias for hdrs, added in Python 3.4.

fp is a file-like object from which the HTTP error body can be read.

ContentTooShortError is raised by urlretrieve when downloaded data is less than the expected amount from the Content-Length header.

Its content attribute stores the truncated downloaded data.

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, urllib.error — Exception classes raised by urllib.request; CPython, urllib.error documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, urllib.error — Exception classes raised by urllib.request; CPython, urllib.error documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.

Cite this

Permanent ID: iil:note:bv072vqm

iinventedlight. 2026. "Python urllib.error: URLError, HTTPError, and ContentTooShortError." iinventedlight Library, iil:note:bv072vqm, version of 2026-11-27. https://iinventedlight.com/library/notes/python-urllib-error/. License: CC-BY-4.0.

@misc{iil_note_bv072vqm,
  author = {{iinventedlight}},
  title = {{Python urllib.error: URLError, HTTPError, and ContentTooShortError}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-27},
  url = {https://iinventedlight.com/library/notes/python-urllib-error/},
  note = {Permanent ID iil:note:bv072vqm. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-urllib-error.md.