polite-http
A courteous, dependency-free HTTP client for Python. It plays nice with rate-limited APIs out of the box:
- Per-host rate limiting — cross-process, via a shared file lock.
- Automatic retries on transient errors (HTTP 429, 5xx) and transient network errors (e.g. timeouts and connection resets).
- Exponential backoff with optional jitter.
Retry-Aftersupport (server-directed backoff, both seconds and HTTP-date forms).X-Throttling-Controlproactive backpressure (as used by PubChem / NCBI).- Streaming helpers for large line-oriented and binary responses.
- Zero third-party dependencies — built entirely on the standard library
(
urllib.request).
Installation
pip install polite-http
Requires Python 3.9+. Cross-process rate limiting works on Linux, macOS, and
Windows — it uses fcntl on POSIX and msvcrt on Windows for the shared file
lock (both standard library). On the rare platform that provides neither, it
falls back to a best-effort in-process timer.
Quick start
from polite_http import HttpClient
# Scope a client to a base URL and a steady-state rate of 3 requests/second.
client = HttpClient("https://eutils.ncbi.nlm.nih.gov/entrez/eutils/", qps=3)
# GET + parse JSON (relative paths are resolved against the base URL).
data = client.fetch_json("esummary.fcgi?db=pubmed&id=123456")
# POST a JSON body.
result = client.fetch_json(
"esearch.fcgi",
method="POST",
json_body={"db": "pubmed", "term": "cancer"},
)
# Download raw bytes with a custom timeout.
pdf = client.fetch_bytes(
"efetch.fcgi?db=pubmed&id=123456&rettype=abstract",
timeout=60,
)
Streaming
# Stream a large response line-by-line without buffering it all in memory.
for line in client.stream_lines("large-export.tsv"):
process(line)
# Stream binary content in chunks (e.g. to a file).
with open("paper.pdf", "wb") as f:
for chunk in client.stream_bytes("paper.pdf"):
f.write(chunk)
Configuration
HttpClient accepts the following keyword arguments:
| Argument | Default | Description |
|---|---|---|
qps |
required | Maximum queries per second (steady state). |
default_headers |
None |
Headers added to every request. |
max_retries |
7 |
Retry attempts for transient errors (total attempts = max_retries + 1). |
timeout |
60.0 |
Per-request timeout in seconds. |
backoff_base |
3.0 |
Base delay for exponential backoff. |
backoff_max |
180.0 |
Cap on backoff delay. |
jitter |
0.5 |
Max uniform random jitter added to each backoff. |
user_agent |
env / "" |
User-Agent header; falls back to POLITE_HTTP_USER_AGENT. |
retryable_status_codes |
{429, 500, 502, 503, 504} |
Status codes that trigger a retry. |
referer |
None |
Optional Referer header sent with every request. |
Environment variables
POLITE_HTTP_USER_AGENT— defaultUser-Agentwhen one isn't passed explicitly. Many APIs (e.g. NCBI) reject requests without a descriptive User-Agent, so setting this is recommended.POLITE_HTTP_LOCK_DIR— directory for the cross-process rate-limit lock files (defaults to the system temp directory).
Error handling
Failed requests raise HttpError, which carries the status_code, raw body
bytes, and url. The raw body may be gzip-compressed; text and json()
decompress and decode it for you:
from polite_http import HttpClient, HttpError
client = HttpClient("https://api.example.com/", qps=5)
try:
data = client.fetch_json("widgets/42")
except HttpError as exc:
print(exc.status_code, exc.url, exc.text)
if exc.body:
print(exc.json()) # parse the error body as JSON, if applicable
Acknowledgements
The HTTP client at the heart of this package is derived from the
science-skills project by
Google DeepMind, used under the Apache License 2.0. See NOTICE for
details of the changes.
License
Metadata
Release files for polite-http 0.1.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| polite_http-0.1.4.tar.gz | 23.8 kB | With provenance | Details for polite_http-0.1.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| polite_http-0.1.4-py3-none-any.whl | Python 3 | none | any | Details for polite_http-0.1.4-py3-none-any.whl |
Total release size: 44.4 kB
Release files / polite_http-0.1.4.tar.gz
| Download URL | polite_http-0.1.4.tar.gz |
|---|---|
| Size | 23.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0dcf315a99458c73def06f30cc2f162aaac49f2b40cdd942a0c6a3cc3ecda2be
|
|
BLAKE2b-256 checksum How to use checksums |
c2e5d1cda822e54032da60ceb23251406a0f0e1dbd53af1db2eae891ccd00b7f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 11, 2026.
Transparency logRelease files / polite_http-0.1.4-py3-none-any.whl
| Download URL | polite_http-0.1.4-py3-none-any.whl |
|---|---|
| Size | 20.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d692167fb8d88f0df28f0cfded29424234c0a5ef6d8365296418469cbb50c153
|
|
BLAKE2b-256 checksum How to use checksums |
1a7b012b3937d496dcee8ab9bb985593b28ac3e899ece6184559a930aea43a26
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 11, 2026.
Transparency logSecurity for polite-http 0.1.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for polite_http-0.1.4.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 11, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.