Streaming parser for multipart/form-data
Project description
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 production usage is not recommended.
Usage
>>> from streaming_form_data.parser import StreamingFormDataParser
>>> from streaming_form_data.part import 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Hashes for streaming_form_data-0.3.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3972f880e1159bd2b8ccdcdcc3e9cd1cba4453699facb5ce9b51cb660757c349 |
|
MD5 | 8b33ac118c664b9d0483d14ccefbc101 |
|
BLAKE2b-256 | a5d1a682d80a6dda65ed24ffe0f46412eb77ec050e18454055c8c65c13045fe7 |