Getting Started
Relevant source files
The following files were used as context for generating this wiki page:
python-multipart is an Apache2-licensed streaming multipart parser for Python designed to handle large file uploads efficiently by processing data in chunks rather than loading entire payloads into memory. It supports multiple content types including multipart/form-data, application/x-www-form-urlencoded, and application/octet-stream.
The library provides both high-level helper functions for quick integration and low-level parser classes for granular control over the parsing process through notification and data callbacks.
Sources: README.md:1-12, python_multipart/multipart.py:27-28, python_multipart/multipart.py:1010-1015
Installation and Environment
The project requires Python 3.10 or higher and uses hatchling as the build backend. Development and testing involve several scripts located in the scripts/ directory.
Sources: pyproject.toml:5-10, scripts/README.md:3-8
Development Setup
To set up a development environment, the following scripts are provided:
scripts/setup: Installs project dependencies.scripts/test: Executes the test suite usingpytest.scripts/lint: Runs code formatting viaruff.scripts/check: Executes linting in check mode and runs themypytype checker.
Sources: scripts/README.md:3-8, pyproject.toml:44-65
High-Level API Usage
For most use cases, the library offers simplified functions to parse incoming request streams directly into fields and files using callbacks.
The parse_form Function
The parse_form function is the most direct way to handle an input stream. It requires a dictionary of headers (containing at least Content-Type), an input stream with a read() method, and two callback functions.
def on_field(field):
print(f"Field: {field.field_name} = {field.value}")
def on_file(file):
print(f"File: {file.file_name} saved to {file.actual_file_name}")
parse_form(headers, input_stream, on_field, on_file)
Sources: python_multipart/multipart.py:1669-1721
High-Level Data Flow
The following diagram illustrates how parse_form orchestrates the parsing process:
The diagram shows the transition from raw input stream to processed field and file objects via the FormParser.
Sources: python_multipart/multipart.py:1688-1718
Core Components
The library is built around a hierarchy of parsers and data containers.
Parser Classes
The library includes specialized parsers for different content types, all inheriting from a BaseParser.
| Parser Class | Target Content Type | Description |
|---|---|---|
OctetStreamParser | application/octet-stream | Parses raw binary data streams. |
QuerystringParser | application/x-www-form-urlencoded | Parses URL-encoded form data. |
MultipartParser | multipart/form-data | Parses multi-part messages using a boundary string. |
FormParser | All supported | A wrapper that selects the appropriate parser based on Content-Type. |
Sources: python_multipart/multipart.py:821, python_multipart/multipart.py:873, python_multipart/multipart.py:1010, python_multipart/multipart.py:1344
Data Containers: Field and File
Parsed data is encapsulated in Field or File objects.
- Field: Represents standard form fields. It caches the field name and its value as bytes.
- File: Represents uploaded files. It manages data storage by keeping it in memory (using
BytesIO) or flushing to disk (usingtempfile) once a threshold is reached.
Sources: python_multipart/multipart.py:228-232, python_multipart/multipart.py:348-355, python_multipart/multipart.py:461-480
Configuration Options
The FormParser and File objects can be configured via a FormParserConfig typed dictionary.
| Option | Type | Default | Description |
|---|---|---|---|
MAX_BODY_SIZE | float | inf | Maximum total body size to parse. |
MAX_MEMORY_FILE_SIZE | int | 1048576 (1 MiB) | Threshold before flushing an in-memory file to disk. |
UPLOAD_DIR | str/bytes | None | Directory for stored files. |
UPLOAD_KEEP_FILENAME | bool | False | Whether to use the original uploaded filename. |
UPLOAD_KEEP_EXTENSIONS | bool | False | Whether to keep the original file extension. |
MAX_HEADER_COUNT | int | 8 | Max headers allowed per multipart part. |
Sources: python_multipart/multipart.py:65-80, python_multipart/multipart.py:1363-1374
Parser Lifecycle and States
The multipart parser operates as a state machine to handle the streaming nature of HTTP uploads.
This state diagram represents the internal transitions of the MultipartParser as it moves through boundaries, headers, and body data.
Sources: python_multipart/multipart.py:105-119, python_multipart/multipart.py:1125-1310
Conclusion
python-multipart provides a robust, state-based streaming architecture for handling Python web uploads. By utilizing the parse_form helper or configuring a FormParser directly, developers can efficiently process large datasets with minimal memory overhead while maintaining strict control over resource limits like header sizes and total body length.
Sources: python_multipart/multipart.py:1344-1358, python_multipart/multipart.py:1669-1675