Skip to main content

Streaming parser for multipart/form-data

Project description

https://travis-ci.org/siddhantgoel/streaming-form-data.svg?branch=master https://badge.fury.io/py/streaming-form-data.svg https://readthedocs.org/projects/streaming-form-data/badge/?version=latest

streaming_form_data provides a Python parser for parsing multipart/form-data input chunks. Chunk size is determined by the API user, but currently there are no restrictions on what the size should be, since the parser works byte-by-byte. Although, this also means that passing the entire input as a single chunk should also work.

The main entry point is the StreamingFormDataParser class, which expects a dictionary of request headers.

The parser is fed chunks of (byte) input, and takes action depending on what the current byte is. In case it notices input that’s expected, it passes on the input to the configured Target, which then decides what to do with it. In case there’s a part that we don’t need, this input is simply discarded using a NullTarget object.

Currently three targets are included with this library - ValueTarget, FileTarget, and SHA256Target. ValueTarget stores the input in memory, and FileTarget pipes the input to a file on disk. Any new targets should inherit streaming_form_data.targets.BaseTarget and define a data_received function.

Please note, that this library has only been tested with Python 3 (specifically, versions 3.3, 3.4, 3.5, and 3.6). Python 2.7 is not supported yet, but pull requests are always welcome!

Usage

>>> from streaming_form_data import StreamingFormDataParser
>>> from streaming_form_data.targets import ValueTarget, FileTarget
>>>
>>> headers = {'Content-Type': 'multipart/form-data; boundary=boundary'}
>>>
>>> parser = StreamingFormDataParser(headers=headers)
>>>
>>> parser.register('name', ValueTarget())
>>> parser.register('file', FileTarget('/tmp/file.txt'))
>>>
>>> parser.data_received(chunk)

Development

To work on this package, please make sure you have a working Python 3 installation on your system.

  1. Create a virtualenv - python -m venv venv && source venv/bin/activate.

  2. Git clone the repository - git clone https://github.com/siddhantgoel/streaming-form-data

  3. Install the packages required for development - pip install -r requirements.dev.txt

  4. Install this package - pip install ..

  5. You should now be able to run the test suite - py.test tests/.

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

streaming_form_data-0.4.1.tar.gz (51.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

streaming_form_data-0.4.1-py3.6-linux-x86_64.egg (151.4 kB view details)

Uploaded Egg

File details

Details for the file streaming_form_data-0.4.1.tar.gz.

File metadata

File hashes

Hashes for streaming_form_data-0.4.1.tar.gz
Algorithm Hash digest
SHA256 896967e5446046d80c5bef05589fee9acd6c89c78b00c01dcc56db81bb54ef74
MD5 59ab48e5ae584d8c5a7493493a6cefbd
BLAKE2b-256 989f142627d29878a375499df7a0462ea703160ac535e5a381bb5ff4488c7320

See more details on using hashes here.

File details

Details for the file streaming_form_data-0.4.1-py3.6-linux-x86_64.egg.

File metadata

File hashes

Hashes for streaming_form_data-0.4.1-py3.6-linux-x86_64.egg
Algorithm Hash digest
SHA256 46ecc392ba98f41766b02734591a7be9500cb6e811bca9170381c2b8822bc2f8
MD5 1238274aab911210fb12c47c93d1734a
BLAKE2b-256 e76e705574881c556ea059f7c6ffdc8790f80ed192c2802d13f06d0b51eb175d

See more details on using hashes here.

Supported by

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