python-mimeparse
Basic functions for parsing and content-negotiating Internet media types in Python.
Repository Health
Technical Analysis
python-mimeparse is a small, dependency-free module for handling MIME types (Internet media types) in Python. It parses mime-type strings into their component parts, understands media ranges with wildcards and quality (q) parameters, and can score and select the best matching content type from a list of candidates.
It implements the content-negotiation semantics described in section 12.5.1 of RFC 9110 (HTTP Semantics), making it a natural building block for HTTP servers and clients that need to honor Accept headers. The library is tiny, well tested against a shared JSON test-data fixture, and supports Python 3.8+ and PyPy.
What You Get
parse_mime_type()andparse_media_range()to break media types and ranges into type, subtype, and parametersquality()andquality_parsed()to score a mime-type against a set of media rangesbest_match()to select the highest-quality candidate content type for content negotiation- A dependency-free, PyPy-compatible module supporting Python 3.8 and newer
Common Use Cases
- Implementing HTTP content negotiation from an
Acceptheader in a web framework - Choosing the best response format (JSON, XML, etc.) an API client will accept
- Validating and normalizing media-type strings in request or configuration handling
Under The Hood
Architecture — The entire public API lives in the mimeparse package as a handful of pure functions (parse_mime_type, parse_media_range, quality, quality_parsed, best_match). Parsing splits a media type into type/subtype and a parameter dict, and quality scoring compares a parsed type against each media range to find the most specific, highest-q match.
Tech Stack — Pure Python with no runtime dependencies, packaged via pyproject.toml and tested across environments with tox. It targets Python 3.8+ and PyPy.
Code Quality — Tests are driven by mimeparse_test.py against a shared testdata.json fixture (the same cross-language test corpus used by sibling mimeparse implementations), giving broad coverage of edge cases in parsing and negotiation despite the library’s small size.
API Design — The API is deliberately minimal and side-effect-free: each function takes strings (or pre-parsed tuples) and returns plain data or a score, so it composes easily and is trivial to reason about. Function names map directly to the RFC concepts they implement.