从BaseHTTPServer解析Python HTML POST数据 [英] Parsing Python HTML POST data from BaseHTTPServer
问题描述
我正在从HTML表单发送几个文件到基于BaseHTTPServer的服务器。
I'm sending a couple of files from an HTML form to my server which is based on BaseHTTPServer.
在我的do_POST中我从rfile获取一个字符串.read(length),看起来像某种多部分MIME字符串。谷歌对如何将其解码为可用的东西没有帮助。
Within my do_POST I'm getting a string from rfile.read(length) which looks like some sort of multipart MIME string. Google is not being helpful on how I can decode this into something usable.
输出如下:
-----------------------------122422713313797828591978698502
Content-Disposition: form-data; name="MAX_FILE_SIZE"
1000000
-----------------------------122422713313797828591978698502
Content-Disposition: form-data; name="and_title_input"
依此类推。
我尝试过email.parser
I've tried email.parser
from email.parser import Parser
p=Parser()
msg=p.parsestr(s)
但是msg没有似乎让我更接近我的目标 - 它不是多部分并且不包含有效载荷。
but msg doesn't seem to get me any nearer to my goal - it's not multipart and contains no payload.
我自己也只是解析数据 - 这肯定不是Pythonic的做事方式!
I'm reduced to parsing the data myself - which is surely not the Pythonic way of doing things!
我错过了一些明显的东西吗?谷歌让我失望了吗? Stack Stack Overflow可以节省一天吗?
Have I missed something obvious? Has Google let me down? Can Stack Overflow save the day?
推荐答案
cgi.parse_multipart 满足您的需求?另请参阅有关 comp.lang.python的相关讨论。
Would cgi.parse_multipart meet your need? Also see a relevant discussion on comp.lang.python.
这篇关于从BaseHTTPServer解析Python HTML POST数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!