473,387 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
Jul 3 '08 #1
7 2922
On Jul 3, 3:59 pm, Ron Garret <rNOSPA...@flownet.comwrote:
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

Jul 4 '08 #2
In article
<48**********************************@p25g2000hsf. googlegroups.com>,
s0****@gmail.com wrote:
On Jul 3, 3:59 pm, Ron Garret <rNOSPA...@flownet.comwrote:
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
Jul 4 '08 #3
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.
Jul 4 '08 #4
In article <3a************@nb2.stroeder.com>,
Michael Ströder <mi*****@stroeder.comwrote:
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
Jul 4 '08 #5
In article <rN*****************************@news.gha.charterm i.net>,
Ron Garret <rN*******@flownet.comwrote:
In article <3a************@nb2.stroeder.com>,
Michael Ströder <mi*****@stroeder.comwrote:
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
Jul 4 '08 #6
Ron Garret wrote:
In article <rN*****************************@news.gha.charterm i.net>,
Ron Garret <rN*******@flownet.comwrote:
>In article <3a************@nb2.stroeder.com>,
Michael Ströder <mi*****@stroeder.comwrote:
>>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.
Jul 5 '08 #7
In article <5f************@nb2.stroeder.com>,
Michael Ströder <mi*****@stroeder.comwrote:
Ron Garret wrote:
In article <rN*****************************@news.gha.charterm i.net>,
Ron Garret <rN*******@flownet.comwrote:
In article <3a************@nb2.stroeder.com>,
Michael Ströder <mi*****@stroeder.comwrote:

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
Jul 6 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Xaver Biton | last post by:
Hi, my internet provvider doesn' have support for pear and I tried the manual Installation as in the installation manual but when I call the script <?php ini_set("include_path",...
4
by: VK | last post by:
Dear All: I have an issue trying to parse response from xml document, for that matter I don't receive back response. I am trying to integrate UPS e-commerce online tool into our web site,...
15
by: John Smith | last post by:
I would like to parse a string into an array. I found on the net the following codes which parse a string and print it. The result is exactly what I want: char * pch; pch = strtok (buffer," ");...
2
by: ohb | last post by:
Hello, Is it possible to parse a attach file in a email ? ohb
3
by: Vijay | last post by:
Hi, In my application i have to parse through a multipart response. In my case the first part is a xml portion and second part is byte stream portion. How can i parse through the various...
3
by: ashulko | last post by:
Hi, I need a little help here as Im new to xml .The problem is in my web App I get response as xml and I try to parse it using DocumentBuilderFactory. All seems to work fine I dont get any errors...
1
by: comp.lang.php | last post by:
<pre> if (!function_exists('mime_content_type_fileinfo')) { /** * Will use {@link http://us2.php.net/fileinfo FileInfo} functions provided within {@link http://pecl.php.net PECL} bundle to return...
3
by: Steven Allport | last post by:
I am working on processing eml email message using the email module (python 2.5), on files exported from an Outlook PST file, to extract the composite parts of the email. In most instances this...
3
by: amit | last post by:
Hello group, I'm uploading some image files such as jpeg, png and gif. However, I must convert time to jpeg type. I have few questions: 1) Can I convert time in using gd library? 2)...
13
by: Phillip B Oldham | last post by:
Is there a standard library for parsing emails that can cope with the different way email clients quote?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.