http
The Python http package is a collection of modules for working with HTTP. The module for making HTTP requests and handling responses is http.client, a low-level client that you normally use indirectly through higher-level interfaces like urllib.request. Other modules include http.server for building HTTP servers, http.cookies for handling cookies, and http.cookiejar for persisting cookies across requests.
Here’s a quick example:
>>> from http.client import HTTPConnection
>>> conn = HTTPConnection("www.example.com")
>>> conn.request("GET", "/")
>>> response = conn.getresponse()
>>> response.status
200
>>> conn.close()
Key Features
- Sends HTTP requests
- Receives HTTP responses
- Handles HTTP headers and status codes
- Supports HTTP/1.1 protocol
- Supports building HTTP servers
- Provides tools for parsing and generating cookies
- Provides the
HTTPStatusandHTTPMethodenums for status codes and request methods
Frequently Used Classes and Functions
| Object | Type | Description |
|---|---|---|
HTTPConnection |
Class | Handles HTTP connections |
HTTPSConnection |
Class | Handles HTTPS connections |
HTTPResponse |
Class | Represents the response from an HTTP request |
responses |
Dictionary | Maps HTTP status codes to status messages |
HTTPStatus |
Enum | Maps HTTP status codes to names, reason phrases, and descriptions |
HTTPMethod |
Enum | Names the standard HTTP methods, such as GET and POST |
Examples
Sending a simple HTTP GET request:
>>> from http.client import HTTPConnection
>>> conn = HTTPConnection("www.example.com")
>>> conn.request("GET", "/")
>>> response = conn.getresponse()
>>> response.status
200
>>> response.read()
b'<!doctype html>...'
Handling HTTP headers:
>>> from http.client import HTTPConnection
>>> conn = HTTPConnection("www.example.com")
>>> conn.request("GET", "/")
>>> response = conn.getresponse()
>>> response.getheaders()
[('Date', '...'), ('Content-Type', 'text/html'), ...]
Common Use Cases
- Making HTTP requests to web servers
- Retrieving and processing HTTP responses
- Managing HTTP headers and status codes
- Implementing HTTP clients and servers
- Handle cookies
Real-World Example
Suppose you need to download the HTML content of a web page and save it to a file. You can achieve this using the http package:
>>> from http.client import HTTPConnection
>>> conn = HTTPConnection("www.example.com")
>>> conn.request("GET", "/")
>>> response = conn.getresponse()
>>> with open("example.html", "wb") as file:
... file.write(response.read())
...
559
>>> conn.close()
In this example, the http package is used to send an HTTP GET request to a server, retrieve the HTML content, and save it to a local file, demonstrating how to perform basic HTTP client tasks.
Related Resources
Course
HTTP Requests With Python's urllib.request
In this video course, you'll explore how to make HTTP requests using Python's handy built-in module, urllib.request. You'll try out examples and go over common errors, all while learning more about HTTP requests and Python in general.
For additional information on related topics, take a look at the following resources:
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Oct. 11, 2026