Tiny utilities for parsing HTTP Range headers and formatting Content-Range values.
http-range-kit is a small Python library for applications that need to support partial content responses (206 Partial Content) without pulling in a full web framework helper.
If you build:
- file download endpoints,
- media streaming APIs,
- proxy/middleware layers,
- storage adapters,
- custom ASGI/WSGI handlers,
...you often need two boring but error-prone pieces of logic:
- parse
Range: bytes=... - resolve the requested ranges against a known resource size
This package keeps that logic tiny, explicit, and framework-agnostic.
- Parse
bytesrange headers - Support closed ranges (
0-99) - Support open-ended ranges (
500-) - Support suffix ranges (
-500) - Resolve ranges against a resource size
- Build
Content-Rangeheader values - No runtime dependencies
pip install http-range-kitfrom http_range_kit import resolve_byte_ranges
ranges = resolve_byte_ranges("bytes=100-199, -20", size=1000)
for item in ranges:
print(item.start, item.end, item.length, item.content_range)
# 100 199 100 bytes 100-199/1000
# 980 999 20 bytes 980-999/1000Parses the raw HTTP Range header value.
from http_range_kit import parse_range_header
specs = parse_range_header("bytes=0-99, 200-, -50")Returned ByteRangeSpec values:
start: inclusive start offset, orNoneend: inclusive end offset, orNonesuffix_length: set for suffix ranges likebytes=-50
Resolves the header against a known resource size.
- clamps ranges to the resource size when appropriate
- skips individual unsatisfiable members
- raises
UnsatisfiableRangeif nothing can be served
from http_range_kit import resolve_byte_ranges
resolved = resolve_byte_ranges("bytes=900-1200", size=1000)
print(resolved[0].start, resolved[0].end)
# 900 999Builds a valid Content-Range header value.
from http_range_kit import build_content_range
header_value = build_content_range(0, 99, 1000)
# bytes 0-99/1000This library intentionally supports a focused MVP:
- only the
bytesrange unit - parsing and resolution helpers
- no multipart/byteranges response body generation
- no direct framework integration
For multi-range inputs, individual ranges that cannot be satisfied are skipped if at least one valid range remains.
Example:
resolve_byte_ranges("bytes=999-1200, 5000-6000, -10", size=1000)This resolves to the satisfiable parts only.
python -m pip install -e . pytest
pytestGitHub Actions runs tests on Python 3.9-3.13.
MIT