Registry / http-networking / python-multipart

python-multipart

JSON →
library0.0.22pypypi✓ verified 35d ago

python-multipart is an Apache2-licensed streaming multipart parser for Python. It is designed for handling `multipart/form-data` POST requests, typically used in web servers for file uploads and complex form data. The library is actively maintained with frequent releases, currently at version 0.0.22, and supports modern Python versions (>=3.10).

http-networkingserialization
Install & Compatibility
Where this runs
tested against v0.0.32 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.925 runs
installs and imports cleanly · install 0.0s · import 0.074s · 18MB
glibc
py 3.103.925 runs
installs and imports cleanly · install 1.5s · import 0.068s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

The package to install is `python-multipart`, but the module to import is `python_multipart`. The old import path `import multipart` was changed in version 0.0.13 and conflicts with a separate, different PyPI package named `multipart`.

from python_multipart import MultipartParser

This quickstart demonstrates how to parse a `multipart/form-data` request body using `MultipartParser`. It simulates an incoming HTTP request with form fields and a file, processing them using event-like iteration.

import io from python_multipart import MultipartParser # Simulate an HTTP request body with multipart/form-data boundary = b"----WebKitFormBoundary7MA4YWxkTrZu0gW" body_data = ( b"--" + boundary + b"\r\n" b'Content-Disposition: form-data; name="username"\r\n' b'\r\n' b'testuser\r\n' b"--" + boundary + b"\r\n" b'Content-Disposition: form-data; name="upload_file"; filename="hello.txt"\r\n' b'Content-Type: text/plain\r\n' b'\r\n' b'Hello, World!\nThis is a test file.\r\n' b"--" + boundary + b"--\r\n" ) # Simulate HTTP headers headers = { "Content-Type": f"multipart/form-data; boundary={boundary.decode()}", "Content-Length": str(len(body_data)), } # Use BytesIO to simulate a file-like object for the body stream body_stream = io.BytesIO(body_data) # Create a parser instance parser = MultipartParser(headers) # Iterate through parts and process them print("Parsing multipart data:") for part in parser.parse(body_stream): if hasattr(part, 'field_name'): # It's a form field print(f" Field: name={part.field_name.decode()}, value={part.value.decode()}") elif hasattr(part, 'file_name'): # It's an uploaded file print(f" File: name={part.field_name.decode()}, filename={part.file_name.decode()}, content_type={part.content_type.decode()}") file_content = part.value # The file content is available as bytes print(f" File content length: {len(file_content)} bytes") # In a real application, you would typically save or process file_content print("\nParsing complete.")
Debug
Known footguns
breakingThe import name was changed from `multipart` to `python_multipart` in version 0.0.13. Direct imports using `import multipart` will no longer work and may lead to `ModuleNotFoundError` or unexpected behavior if another `multipart` package is installed. Versions 0.0.13 and 0.0.14 were temporarily yanked due to breakage related to this change.
breakingSupport for Python 3.8 and 3.9 was dropped in version 0.0.21. The library now requires Python 3.10 or newer.
gotchaA separate, unrelated package on PyPI is also named `multipart`. Installing `pip install multipart` instead of `pip install python-multipart` will install the wrong library, leading to `ModuleNotFoundError` if `from python_multipart import ...` is used, or unexpected behavior if `import multipart` is still in your code.
gotchaIn version 0.0.22, the `File` object produced by the parser will no longer include directory paths in its `filename` attribute. It will only contain the base filename.
gotchaVersion 0.0.18 introduced a 'hard break' if data is found after the last boundary in `MultipartParser`. This means the parser will now strictly enforce the end of the multipart body, potentially raising errors for malformed requests that previously might have been partially processed.
deprecatedIn version 0.0.15, `FutureWarning` messages were replaced with `PendingDeprecationWarning`. While not a breaking change, it indicates that certain behaviors or features are slated for deprecation or removal in future major releases.
gotchaThe `TypeError: can't concat dict to bytes` when initializing `MultipartParser` indicates that a dictionary of HTTP headers (or any dict) was passed as the `content_type` argument. The `MultipartParser` constructor expects the raw value of the `Content-Type` header (a string or bytes) from which it can extract the boundary, or directly the boundary string/bytes, not the entire headers dictionary.
Upgrade
Version history

Breaking-change detection hasn't run for this library yet.

Audit
Security & dependencies

CVE tracking and dependency tree are planned for a later release.

Agent activity
28 hits · last 30 days
claudebot
4
ahrefsbot
3
amazonbot
3
bytedance
2
googlebot
2
chatgpt-user
1
Resources