Parsing multipart HTTP response

Gilles Lenfant gilles.lenfant at gmail.com
Mon Aug 3 13:15:26 EDT 2015


Le lundi 3 août 2015 17:39:57 UTC+2, Mark Lawrence a écrit :
> On 03/08/2015 16:01, Gilles Lenfant wrote:
> > Hi,
> >
> > I searched without succeeding a Python resource that is capable of parsing an HTTP multipart/mixed response stream, preferably as a generator that yields headers + content for each part.
> >
> > Google and friends didn't find or I didn't use the appropriate term.
> >
> > Any hint will be appreciated.
> > --
> > Gilles Lenfant
> >
> 
> Hardly my field but how about:-
> 
> https://pypi.python.org/pypi/requests-toolbelt/0.3.1
> https://pypi.python.org/pypi/multipart/
> 
> -- 
> My fellow Pythonistas, ask not what our language can do for you, ask
> what you can do for our language.
> 
> Mark Lawrence

Thanks Mark,

"multipart" is close to what I need and will give me some inpiration. Unfortunately, it works for multipart/form-data server-side handling, when I'm searching for a multipart/mixed client side handling of a response. In addition, it does not yield the headers specific to each data chunk from the response, and I need those headers.

To be more specific, I'm buiding with the excellent "requests" lib and Python 2.7 (sorry, I can't go to Python 3.x at the moment) a client library for the documents database MarkLogic 8 that provides some APIs with multipart/mixed responses like this one:

http://docs.marklogic.com/REST/POST/v1/eval

In other words I need something that may be used like this :

response = requests.post(some_uri, params=some_params, stream=True)
if response.headers['content-type'].startswith('multipart/mixed'):
    boundary = parse_boundary(response.headers['content-type'])

    generator = iter_multipart_response(response, boundary)
    for part_headers, part_body in generator:
        # I'm consuming parsed headers and bodies from response
        # In my application code

Cheers
-- 
Gilles Lenfant



More information about the Python-list mailing list