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 list of expected_parts, and a dictionary of request headers. Each element in the expected_parts list represents a single part of the complete multipart input, and should be an instance of the Part class.

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 (determined by expected_parts), it passes on the input to Part.data_received, which in turn calls Target.data_received with the input, which then decides what to do with it. At any point of time, only one part of the expected_parts is active. 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.

This library is currently under development. There are quite a few tests in the tests/ directory that test for correctness. But the parser hasn’t been battle tested yet, hence user discretion is advised.

Usage

>>> from streaming_form_data import StreamingFormDataParser, Part
>>> from streaming_form_data.targets import ValueTarget, FileTarget
>>>
>>> name = Part('name', ValueTarget())
>>> file_ = Part('file', FileTarget('/tmp/file.txt'))
>>>
>>> headers = {'Content-Type': 'multipart/form-data; boundary=boundary'}
>>> expected_parts = (name, file_)
>>>
>>> parser = StreamingFormDataParser(expected_parts=expected_parts, headers=headers)
>>> 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.3.2.tar.gz (43.1 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