On this page
http
Prev: html | Up: stdlib_module | Next: idlelib
Rel: email, json, mailbox, mimetypes, base64
Ref: docs.python.org โ http
Tags: #public #python3 #stdlib
Import
import http
Overview
Package holding Python's HTTP protocol primitives: http.HTTPStatus and http.HTTPMethod enums, http.client (low-level persistent HTTP/1.1 connections), http.server (development/local HTTP servers), and http.cookies. Reach for HTTPStatus / HTTPMethod everywhere you deal with status codes or verbs; use http.client when you need explicit connection reuse over raw urllib.request.
Quick Start
from http import HTTPMethod, HTTPStatus
assert HTTPStatus.OK == 200
assert HTTPStatus.NOT_FOUND.phrase == "Not Found"
assert HTTPStatus.CREATED.is_success
assert HTTPMethod.GET == "GET"
Implementation Walkthrough
1. Reusing a Persistent HTTPS Connection with http.client
Unlike urllib.request.urlopen(), HTTPSConnection keeps the TCP/TLS socket open across requests to the same host:
import http.client
import ssl
ctx = ssl.create_default_context()
conn = http.client.HTTPSConnection("example.com", timeout=10, context=ctx)
try:
conn.request("GET", "/", headers={"Host": "example.com"})
resp = conn.getresponse()
body = resp.read(65_536)
status = resp.status
finally:
conn.close()
2. Minimal Local JSON Server with http.server (Dev / Testing Only)
from http import HTTPStatus
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
import json
class HealthHandler(BaseHTTPRequestHandler):
def do_GET(self) -> None:
payload = json.dumps({"status": "ok"}).encode("utf-8")
self.send_response(HTTPStatus.OK)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(payload)))
self.end_headers()
self.wfile.write(payload)
# server = ThreadingHTTPServer(("127.0.0.1", 8000), HealthHandler)
Gotchas & Boundaries
http.serveris NOT for production: It lacks hardening, slowloris protection, and TLS termination; use it only for local tools, fixtures, or development.- Always read the full response on
http.clientbefore sending the next request: Leaving unread bytes on a persistent connection raisesResponseNotReady. - Use
HTTPStatusenums instead of bare magic numbers:resp.status == HTTPStatus.NO_CONTENTis self-documenting and compares equal to204.
Classes & Objects
- http.HTTPMethod โ HTTP methods and descriptions Methods from the following RFCs are all observed: * RFC 9110: HTTP Semantics, obsoletes 7231, which obsoleted 2616 (Methods:
.capitalize(),.casefold(),.center(),.count(),.encode(),.endswith()) - http.HTTPStatus โ HTTP status codes and reason phrases Status codes from the following RFCs are all observed: * RFC 9110: HTTP Semantics, obsoletes 7231, which obsoleted 2616 (Methods:
.as_integer_ratio(),.bit_count(),.bit_length(),.conjugate(),.from_bytes(),.is_integer())
Linked from
- tuts/python3
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/html
- tuts/python3/stdlib_module/http/HTTPMethod
- tuts/python3/stdlib_module/http/HTTPStatus
- tuts/python3/stdlib_module/idlelib
- tuts/python3/stdlib_module/socket
- tuts/python3/stdlib_module/socketserver
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/urllib
- tuts/python3/tutorials/popular_application_domains
- tuts/python3/tutorials/popular_application_domains/web_development_and_apis
Related notes
- tuts/python3/stdlib_module/html Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/http/HTTPMethod Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/http/HTTPStatus Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/idlelib Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/base64 Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/email Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/json Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/mailbox Linked here; shared tags: #stdlib