解析来自 BaseHTTPServer 的 Python HTML POST 数据

发布于 2024-09-09 10:10:04 字数 673 浏览 10 评论 0原文

我正在将几个文件从 HTML 表单发送到基于 BaseHTTPServer 的服务器。

在我的 do_POST 中,我从 rfile.read(length) 获取一个字符串,它看起来像某种多部分 MIME 字符串。谷歌并没有帮助我将其解码为可用的东西。

输出如下所示:

<预><代码>----------------------------122422713313797828591978698502 内容处置:表单数据;名称=“最大文件大小” 1000000 --------------------------------------122422713313797828591978698502 内容处置:表单数据;名称=“and_title_input”

等等。

我已经尝试过 email.parser

from email.parser import Parser 
p=Parser()
msg=p.parsestr(s)

但 msg 似乎并没有让我更接近我的目标 - 它不是多部分的并且不包含有效负载。

我只能自己解析数据——这肯定不是 Pythonic 的做事方式!

我错过了一些明显的事情吗?谷歌让我失望了吗? Stack Overflow 能拯救世界吗?

I'm sending a couple of files from an HTML form to my server which is based on BaseHTTPServer.

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.

The output looks like this :

-----------------------------122422713313797828591978698502

Content-Disposition: form-data; name="MAX_FILE_SIZE"



1000000

-----------------------------122422713313797828591978698502

Content-Disposition: form-data; name="and_title_input"

and so on.

I've tried email.parser

from email.parser import Parser 
p=Parser()
msg=p.parsestr(s)

but msg doesn't seem to get me any nearer to my goal - it's not multipart and contains no payload.

I'm reduced to parsing the data myself - which is surely not the Pythonic way of doing things!

Have I missed something obvious? Has Google let me down? Can Stack Overflow save the day?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余生再见 2024-09-16 10:10:04

cgi.parse_multipart 能满足您的需求吗?另请参阅 comp.lang.python 的相关讨论

Would cgi.parse_multipart meet your need? Also see a relevant discussion on comp.lang.python.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文