Skip to main content
Python Software Foundation 20th Year Anniversary Fundraiser  Donate today!

Multipart/* reader extracted from awsome `aiohttp` project, cf.:

Project description

We are under CI!!

Permits to read a multipart content like, mixed, related, etc.

Thanks to the aiohttp project for the implementation, cf.: Unfortunately lot of people have not yet moved to asyncio, or do not want the full aiohttp stack to read the multipart content.

Here we tried to keep all the aiohttp logic and coverage but the coroutines mechanism.

What it does

It reads the same way multpart/x contents. Lets say we have the following multipart/related content:

>>> import json

>>> from email.mime.multipart import MIMEMultipart
>>> from email.mime.base import MIMEBase

>>> multipart = MIMEMultipart('related')

>>> part = MIMEBase('application', 'json')
>>> part.set_payload(json.dumps({'foo': 'bar'}))
>>> multipart.attach(part)

>>> part = MIMEBase('application', 'octet-stream')
>>> part.set_payload(b"Python will save the world. I don't know how. But it will.")
>>> part.add_header('Content-Disposition', 'attachment', filename='python-save-the-world.txt')
>>> multipart.attach(part)

Here is how we can read it:

>>> import io

>>> from multipart_reader import MultipartReader

>>> content = multipart.as_string().split('\n\n', 1)[1]
>>> headers = dict(multipart.items())

>>> stream = io.BytesIO()
>>> stream.write(content)

>>> reader = MultipartReader(headers, stream)

>>> json_part =
>>> json_part.json()
{'foo': 'bar'}

>>> file_part =
"Python will save the world. I don't know how. But it will."

>>> file_part.filename

That’s it …

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for multipart-reader, version 0.2
Filename, size File type Python version Upload date Hashes
Filename, size multipart_reader-0.2-py2-none-any.whl (13.6 kB) File type Wheel Python version 2.7 Upload date Hashes View
Filename, size multipart_reader-0.2-py3-none-any.whl (13.6 kB) File type Wheel Python version 3.6 Upload date Hashes View
Filename, size multipart-reader-0.2.tar.gz (15.3 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page