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
- id: C1 claim: Handlers raise URLError or a derived class when a request fails. source: S1 evidence: "The handlers raise this exception (or derived exceptions)"
- id: C2 claim: HTTPError can be used as a file-like return value, not only as an exception. source: S2 evidence: "useful when handling exotic HTTP errors, such as"
- id: C3 claim: urlretrieve raises ContentTooShortError when the download is shorter than promised. source: S2 evidence: "This exception is raised when the"
Sources
- [S1] urllib.error — Exception classes raised by urllib.request. Python Software Foundation. https://docs.python.org/3/library/urllib.error.html . License: PSF License Version 2. Accessed 2026-11-27.
- [S2] urllib.error documentation (source). CPython. https://raw.githubusercontent.com/python/cpython/main/Doc/library/urllib.error.rst . License: PSF License Version 2. Accessed 2026-11-27.
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
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.},
}
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.
- python-urllib-error.schema.json (schema; CC-BY-4.0)
- python-urllib-error.table.json (table; CC-BY-4.0)
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.