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

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.

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)

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.0.tar.gz (50.9 kB view hashes)

Uploaded Source

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