Skip to main content

Tasty multipart form data parser built with cython.

Project description

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

Project details


Download files

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

Source Distribution

multifruits-0.1.4.tar.gz (58.8 kB view details)

Uploaded Source

File details

Details for the file multifruits-0.1.4.tar.gz.

File metadata

  • Download URL: multifruits-0.1.4.tar.gz
  • Upload date:
  • Size: 58.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6

File hashes

Hashes for multifruits-0.1.4.tar.gz
Algorithm Hash digest
SHA256 5c23c6db9b6c19c5c6d86fc2a1d987121c8d012d9b82cb7cab6222f19ce2497f
MD5 270b669fdf51f39a1fa5cd32a45ec906
BLAKE2b-256 b6bf5ee3b7d5f167408a22d912183b3230bbe87a4181553e906996eb5ef8b2ec

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page