Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Parsing MIME-encoded data in an HTTP request (http://www.velocityreviews.com/forums/t623691-parsing-mime-encoded-data-in-an-http-request.html)

Ron Garret 07-03-2008 08:59 PM

Parsing MIME-encoded data in an HTTP request
 
I'm writing a little HTTP server and need to parse request content that
is mime-encoded. All the MIME routines in the Python standard library
seem to have been subsumed into the email package, which makes this
operation a little awkward. It seems I have to do the following:

1. Extract the content-length header from the HTTP request and use that
to read the payload.

2. Stick some artificial-looking headers onto the beginning of this
payload to make it look like an email message (including the
content-type and content-transfer-encoding headers)

3. Parse the resulting string into a email message

That works, but it feels way too hackish for my tastes. Surely there
must be a better/more standard way of doing this?

Thanks,
rg

s0suk3@gmail.com 07-04-2008 03:50 AM

Re: Parsing MIME-encoded data in an HTTP request
 
On Jul 3, 3:59 pm, Ron Garret <rNOSPA...@flownet.com> wrote:
> I'm writing a little HTTP server and need to parse request content that
> is mime-encoded. All the MIME routines in the Python standard library
> seem to have been subsumed into the email package, which makes this
> operation a little awkward.


To deal with messages of that kind, I've seen modules such as
'rfc822', and 'mimetools' (which apparently builds itself from
'rfc822', so it might be more complete). There's also 'mimetypes', in
case you need to deal with file extensions and their corresponding
MIME media type.

> It seems I have to do the following:
>
> 1. Extract the content-length header from the HTTP request and use that
> to read the payload.
>
> 2. Stick some artificial-looking headers onto the beginning of this
> payload to make it look like an email message (including the
> content-type and content-transfer-encoding headers)
>
> 3. Parse the resulting string into a email message
>


Email? Why does an HTTP server need to build an email message?

I remember doing things like that some time ago when building an HTTP
server myself (http://code.google.com/p/sws-d/). Incidentally, I
resisted the urge to use much of the Python's library facilities (most
things are done manually; am I a knucklehead or what!? :). You might
wanna take a look to get some ideas.

Sebastian


Ron Garret 07-04-2008 07:17 AM

Re: Parsing MIME-encoded data in an HTTP request
 
In article
<4834df8f-2cf8-434c-8c95-ff53024766c8@p25g2000hsf.googlegroups.com>,
s0suk3@gmail.com wrote:

> On Jul 3, 3:59 pm, Ron Garret <rNOSPA...@flownet.com> wrote:
> > I'm writing a little HTTP server and need to parse request content that
> > is mime-encoded. All the MIME routines in the Python standard library
> > seem to have been subsumed into the email package, which makes this
> > operation a little awkward.

>
> To deal with messages of that kind, I've seen modules such as
> 'rfc822', and 'mimetools' (which apparently builds itself from
> 'rfc822', so it might be more complete). There's also 'mimetypes', in
> case you need to deal with file extensions and their corresponding
> MIME media type.


From the mimetools docs:

"Deprecated since release 2.3. The email package should be used in
preference to the module. This module is present only to maintain
backward compatibility."

>
> > It seems I have to do the following:
> >
> > 1. Extract the content-length header from the HTTP request and use that
> > to read the payload.
> >
> > 2. Stick some artificial-looking headers onto the beginning of this
> > payload to make it look like an email message (including the
> > content-type and content-transfer-encoding headers)
> >
> > 3. Parse the resulting string into a email message
> >

>
> Email? Why does an HTTP server need to build an email message?


It shouldn't. That's my whole point. But see the docs excerpt above.

> I remember doing things like that some time ago when building an HTTP
> server myself (http://code.google.com/p/sws-d/). Incidentally, I
> resisted the urge to use much of the Python's library facilities (most
> things are done manually; am I a knucklehead or what!? :). You might
> wanna take a look to get some ideas.


I'd much prefer not to reinvent this particular wheel.

rg

Michael Ströder 07-04-2008 07:49 AM

Re: Parsing MIME-encoded data in an HTTP request
 
Ron Garret wrote:
> I'm writing a little HTTP server and need to parse request content that
> is mime-encoded. All the MIME routines in the Python standard library
> seem to have been subsumed into the email package, which makes this
> operation a little awkward.


How about using cgi.parse_multipart()?

Ciao, Michael.

Ron Garret 07-04-2008 10:30 PM

Re: Parsing MIME-encoded data in an HTTP request
 
In article <3a11k5-al7.ln1@nb2.stroeder.com>,
Michael Ströder <michael@stroeder.com> wrote:

> Ron Garret wrote:
> > I'm writing a little HTTP server and need to parse request content that
> > is mime-encoded. All the MIME routines in the Python standard library
> > seem to have been subsumed into the email package, which makes this
> > operation a little awkward.

>
> How about using cgi.parse_multipart()?
>
> Ciao, Michael.


Unfortunately cgi.parse_multipart doesn't handle nested multiparts,
which the requests I'm getting have. You have to use a FieldStorage
object to do that, and that only works if you're actually in a cgi
environment, which I am not. The server responds to these requests
directly.

Anyway, thanks for the idea.

rg

Ron Garret 07-04-2008 10:37 PM

Re: Parsing MIME-encoded data in an HTTP request
 
In article <rNOSPAMon-BDA88C.15302904072008@news.gha.chartermi.net>,
Ron Garret <rNOSPAMon@flownet.com> wrote:

> In article <3a11k5-al7.ln1@nb2.stroeder.com>,
> Michael Ströder <michael@stroeder.com> wrote:
>
> > Ron Garret wrote:
> > > I'm writing a little HTTP server and need to parse request content that
> > > is mime-encoded. All the MIME routines in the Python standard library
> > > seem to have been subsumed into the email package, which makes this
> > > operation a little awkward.

> >
> > How about using cgi.parse_multipart()?
> >
> > Ciao, Michael.

>
> Unfortunately cgi.parse_multipart doesn't handle nested multiparts,
> which the requests I'm getting have. You have to use a FieldStorage
> object to do that, and that only works if you're actually in a cgi
> environment, which I am not. The server responds to these requests
> directly.
>
> Anyway, thanks for the idea.
>
> rg


Hm, it actually seems to work if I manually pass in the outerboundary
parameter and environ={'REQUEST_METHOD':'POST'} That seems like the
Right Answer.

Woohoo!

Thanks Michael!

rg

Michael Ströder 07-05-2008 09:11 AM

Re: Parsing MIME-encoded data in an HTTP request
 
Ron Garret wrote:
> In article <rNOSPAMon-BDA88C.15302904072008@news.gha.chartermi.net>,
> Ron Garret <rNOSPAMon@flownet.com> wrote:
>
>> In article <3a11k5-al7.ln1@nb2.stroeder.com>,
>> Michael Ströder <michael@stroeder.com> wrote:
>>
>>> Ron Garret wrote:
>>>> I'm writing a little HTTP server and need to parse request content that
>>>> is mime-encoded. All the MIME routines in the Python standard library
>>>> seem to have been subsumed into the email package, which makes this
>>>> operation a little awkward.
>>> How about using cgi.parse_multipart()?
>>>

>> Unfortunately cgi.parse_multipart doesn't handle nested multiparts,
>> which the requests I'm getting have. You have to use a FieldStorage
>> object to do that, and that only works if you're actually in a cgi
>> environment, which I am not. The server responds to these requests
>> directly.
>>
>> Anyway, thanks for the idea.

>
> Hm, it actually seems to work if I manually pass in the outerboundary
> parameter and environ={'REQUEST_METHOD':'POST'} That seems like the
> Right Answer.


I'm also using it to parse form parameters in a message body received by
POST.

CIao, Michael.

Ron Garret 07-06-2008 07:47 AM

Re: Parsing MIME-encoded data in an HTTP request
 
In article <5fq3k5-eva.ln1@nb2.stroeder.com>,
Michael Ströder <michael@stroeder.com> wrote:

> Ron Garret wrote:
> > In article <rNOSPAMon-BDA88C.15302904072008@news.gha.chartermi.net>,
> > Ron Garret <rNOSPAMon@flownet.com> wrote:
> >
> >> In article <3a11k5-al7.ln1@nb2.stroeder.com>,
> >> Michael Ströder <michael@stroeder.com> wrote:
> >>
> >>> Ron Garret wrote:
> >>>> I'm writing a little HTTP server and need to parse request content that
> >>>> is mime-encoded. All the MIME routines in the Python standard library
> >>>> seem to have been subsumed into the email package, which makes this
> >>>> operation a little awkward.
> >>> How about using cgi.parse_multipart()?
> >>>
> >> Unfortunately cgi.parse_multipart doesn't handle nested multiparts,
> >> which the requests I'm getting have. You have to use a FieldStorage
> >> object to do that, and that only works if you're actually in a cgi
> >> environment, which I am not. The server responds to these requests
> >> directly.
> >>
> >> Anyway, thanks for the idea.

> >
> > Hm, it actually seems to work if I manually pass in the outerboundary
> > parameter and environ={'REQUEST_METHOD':'POST'} That seems like the
> > Right Answer.

>
> I'm also using it to parse form parameters in a message body received by
> POST.
>
> CIao, Michael.


Just for the record, here's the incantation I ended up with:


class post_handler(BaseHTTPRequestHandler):
def do_POST(self):
form = cgi.FieldStorage(fp=self.rfile, headers=self.headers,
environ={'REQUEST_METHOD':'POST'})
...


works like a charm.

rg


All times are GMT. The time now is 05:11 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57