Python-MimeParse

2.0.0 · active · verified Sun Apr 12

Python-MimeParse is a stable and mature project that provides basic functions for parsing MIME type names and matching them against a list of media ranges, adhering to HTTP specifications. The current version is 2.0.0, which includes support for CPython 3.13 and drops support for Python 3.7. The project aims for stability with infrequent, but significant, updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `best_match` to select the most suitable MIME type based on an `Accept` header and available types, and `parse_mime_type` to break down a MIME type into its components.

from mimeparse import best_match

supported_types = ['application/json', 'text/html', 'image/jpeg', 'text/plain']
accept_header = 'text/*;q=0.5, application/json;q=1.0, */*;q=0.8'

match = best_match(supported_types, accept_header)
print(f"Best match for '{accept_header}' from supported types: {match}")

# Example with parsing a specific mime type
from mimeparse import parse_mime_type
mime_type = 'application/json; charset=utf-8'
parsed_type = parse_mime_type(mime_type)
print(f"Parsed '{mime_type}': {parsed_type}")

view raw JSON →