http.client.HTTPException
PythonERRORNotableNetwork
Low-level HTTP protocol error
Quick Answer
Catch http.client.HTTPException for low-level HTTP protocol errors; switch to the requests library for higher-level error handling in production code.
Production Risk
Medium.
What this means
Base class for all exceptions raised by http.client (the low-level HTTP library). Covers malformed responses, incomplete reads, and invalid headers — situations where the remote server violated the HTTP protocol.
Why it happens
- 1Server closed the connection before sending a complete response
- 2Response contains malformed headers
- 3Chunked transfer encoding is corrupted
Fix
Catch HTTPException and retry
Catch HTTPException and retry
import http.client
import time
def fetch(host, path, retries=3):
for attempt in range(retries):
try:
conn = http.client.HTTPSConnection(host, timeout=10)
conn.request('GET', path)
return conn.getresponse().read()
except http.client.HTTPException as e:
if attempt == retries - 1:
raise
time.sleep(2 ** attempt)Why this works
Exponential back-off handles transient connection-reset and incomplete-read errors from overloaded servers.
Code examples
Common subclassespython
# http.client exception hierarchy: # HTTPException # NotConnected # InvalidURL # UnknownProtocol # UnknownTransferEncoding # UnimplementedFileMode # IncompleteRead # ImproperConnectionState # BadStatusLine # LineTooLong
Same error in other languages
Sources
Official documentation ↗
Python Docs
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev