Skip to main content

Multifruits

Tasty multipart form data parser built with cython.

Install

pip install multifruits

Usage

multifruits has one Parser class and two helpers: extract_filename and parse_content_disposition.

Parser

Parser needs the Content-Type header value and a handler, which could define one or more of these methods:

on_body_begin()
on_part_begin()
on_header(name: bytes, value: bytes)
on_headers_complete()
on_data(data: bytes)
on_part_complete()
on_body_complete()

Example:

from multifruits import Parser

class MyHandler:

    def on_part_begin(self):
        self.part = MyPart()

    def on_header(self, name, value):
        self.part.headers[name] = value

    def on_data(self, data):
        self.part.write(data)

handler = MyHandler()
parser = Parser(handler, request.headers['Content-Type'])
parser.feed_data(request.body)  # You can pass chunks

Helpers

parse_content_disposition

Takes raw Content-Disposition header value and returns the disposition type (attachment, form-data, inline and so on) and the parameters parsed as a dictionary.

Example:

dtype, params = parse_content_disposition(b'inline; filename="foo.html"')
assert dtype == b'inline'
assert params == {b'filename': b'foo.html'}
extract_filename

Takes parameters from parse_content_disposition as a dict and tries to return the appropriated str filename (like filename*).

Example:

assert extract_filename({
    b'filename*': "UTF-8''foo-ä-€.html".encode()
}) == 'foo-ä-€.html'

Build from source

You need a virtualenv with cython installed, then:

git clone https://github.com/pyrates/multifruits
cd multifruits
make compile
python setup.py develop

Tests

To run tests:

make test

Acknowledgements

Release files for multifruits 0.1.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for multifruits 0.1.7
File Size Uploaded
multifruits-0.1.7.tar.gz 94.1 kB Details

Release files / multifruits-0.1.7.tar.gz

Download URL multifruits-0.1.7.tar.gz
Size 94.1 kB
Tags Source
SHA-256 checksum
How to use checksums
8985bb7b73001525f92cad2e0efa353c42a3ae67a7510d67f19143b09be41019
BLAKE2b-256 checksum
How to use checksums
8c815236fd520d50a5ae8fad51e063302e0a4a002b47fc5a9a015bc0047be931
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.7

Release history Release notifications | RSS feed

This release

0.1.7 This release

1 release file

0.1.6

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page