A streaming multipart parser for Python
Actively maintained. Last release 13 days ago, commits in 13 of the last 26 weeks. A safe choice.
pip install python-multipartPython-Multipart
Python-Multipart reads the data that a web browser sends when someone submits a form or uploads a file.
pip install python-multipart
It powers file uploads in Starlette and FastAPI.
What problem does it solve?
A form sends data
When you fill in a form on a website and click "Submit", your browser sends the data to a server. The server is a program, maybe written in Python, that has to read that data and make sense of it.
Files need a special format
A form with only text, like a username, is sent as one short line: username=alice.
A form with a file is different. A file can contain anything, even text that looks like username=alice.
So the browser uses a format called multipart/form-data. It splits the request into parts, one per form field, separated by a boundary: a marker string the browser picks.
This is what the server receives when you send a username and a file called cat.txt:
--boundary
Content-Disposition: form-data; name="username"
alice
--boundary
Content-Disposition: form-data; name="avatar"; filename="cat.txt"
Content-Type: text/plain
meow
--boundary--
Each part has some headers (the field name, the file name) and then the content.
Every line ends with \r\n (a carriage return and a newline), which is why the examples below write \r\n explicitly.
A parser is the code that turns this text back into fields and files. Python-Multipart is that parser.