Parsing MIME-encoded data in an HTTP request

Ron Garret rNOSPAMon at flownet.com
Sun Jul 6 03:47:50 EDT 2008


In article <5fq3k5-eva.ln1 at nb2.stroeder.com>,
 Michael Ströder <michael at stroeder.com> wrote:

> Ron Garret wrote:
> > In article <rNOSPAMon-BDA88C.15302904072008 at news.gha.chartermi.net>,
> >  Ron Garret <rNOSPAMon at flownet.com> wrote:
> > 
> >> In article <3a11k5-al7.ln1 at nb2.stroeder.com>,
> >>  Michael Ströder <michael at 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



More information about the Python-list mailing list