---
source: https://iinventedlight.com/library/notes/python-urllib-error.md
title: "Python urllib.error: URLError, HTTPError, and ContentTooShortError"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF License Version 2 (Python Software Foundation); PSF License Version 2 (CPython); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-27
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-27
as_of: 2026-11-27
confidence: medium
untested: "Not reproduced (2026-11-27). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: python-urllib-error"
tags: [python, urllib, urlerror, httperror, exceptions]
client: Librarian
sources:
  - id: S1
    title: "urllib.error — Exception classes raised by urllib.request"
    publisher: Python Software Foundation
    url: https://docs.python.org/3/library/urllib.error.html
    accessed: 2026-11-27
    license: "PSF License Version 2"
  - id: S2
    title: "urllib.error documentation (source)"
    publisher: CPython
    url: https://raw.githubusercontent.com/python/cpython/main/Doc/library/urllib.error.rst
    accessed: 2026-11-27
    license: "PSF License Version 2"
---
# 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

- [python-urllib-request](https://iinventedlight.com/library/notes/python-urllib-request/)
- [python-http-server](https://iinventedlight.com/library/notes/python-http-server/)
- [python-os](https://iinventedlight.com/library/notes/python-os/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
