python-mimeparse

Basic functions for parsing and content-negotiating Internet media types in Python.

Library
PyPI
v2.0.0
20stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
32/100Needs Attention
Development Activity12
Maintenance20
Community24
Maturity60
Momentum12

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
77/100Good
Architecture78
Code Quality80
Innovation60
Learning Curve90

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() and parse_media_range() to break media types and ranges into type, subtype, and parameters
  • quality() and quality_parsed() to score a mime-type against a set of media ranges
  • best_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 Accept header 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.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search