简体   繁体   English

python:解析HTTP POST请求w /文件上传和其他参数

[英]python: parse HTTP POST request w/file upload and additional params

The task is simple: on the server side (python) accept an HTTP POST which contains an uploaded file and more form parameters. 任务很简单:在服务器端(python)接受HTTP POST,其中包含上传的文件和更多的表单参数。

I am trying to implement upload progress indicator, and therefore I need to be able to read the file content chunk-by-chunk. 我正在尝试实现上传进度指示器,因此我需要能够逐块读取文件内容。

All methods I found are based on cgi.FieldStorage, which somehow only allows me to obtain the file in its entirety (in memory, which is a disaster in itself). 我发现的所有方法都基于cgi.FieldStorage,它以某种方式只允许我完整地获取文件(在内存中,这本身就是一个灾难)。 Some advise to redefine the FieldStorage.make_file method(), which seems to break down the cgi implementation (weird...). 有人建议重新定义FieldStorage.make_file方法(),它似乎打破了cgi实现(很奇怪......)。

I am currently able to read the entire wsgi input, chunk by chunk, to the filesystem, resulting in the following data: 我目前能够通过chunk读取整个wsgi输入到文件系统,从而产生以下数据:

-----------------------------9514143097616
Content-Disposition: form-data; name="myfile"; filename="inbound_marketing_cartoon_ebook.pdf"
Content-Type: application/pdf

... 1.5 MB of PDF data

-----------------------------9514143097616
Content-Disposition: form-data; name="tid"

194
-----------------------------9514143097616--

Does anyone know if there are any Python libraries that could reliably parse this thing? 有谁知道是否有任何Python库可以可靠地解析这个东西? Or should I do this manually? 或者我应该手动执行此操作? (Python 2.5 that is) (Python 2.5即)

Thanks. 谢谢。

As you suggested, I would (and have done before) override the make_file method of a FieldStorage object. 正如你的建议,我会(以前做过)覆盖FieldStorage对象的make_file方法。 Just return an object which has a write method that both accepts the data (into a file or memory or what-have-you) and tracks how much has been received for your progress indicator. 只需返回一个具有write方法的对象,该方法既接受数据又接受数据(进入文件或内存或者有什么),并跟踪接收进度指示器的数量。

Doing it this way you also get access to the length of the file (as supplied by the client), file name, and the key that it is posted under. 通过这种方式,您还可以访问文件的长度(由客户端提供),文件名以及在其下发布的密钥。

Why does this seem to break down the CGI implementation for you? 为什么这似乎打破了CGI实现?

Another option is to do the progress tracking in the browser with a flash uploader ( YUI Uploader and SWFUpload come to mind) and skip tracking it on the server entirely. 另一种选择是在浏览器中使用闪存上传器( YUI UploaderSWFUpload )进行进度跟踪,并完全跳过在服务器上的跟踪。 Then you don't have to have a series of AJAX requests to get the progress. 然后,您无需获得一系列AJAX请求即可获得进度。

It seems counter-intuitive (and I feel that the module is poorly-named), but email will likely do what you want. 这似乎是违反直觉的(我觉得该模块命名不佳),但email可能会做你想要的。 I've never used it, but a coworker has in an e-mail processing system; 我从未使用它,但是同事在电子邮件处理系统中使用过; since these messages are simply RFC 2822 in nature, email will probably parse them. 由于这些消息本质上只是RFC 2822,因此email可能会解析它们。

The documentation for email is quite thorough, at first glance. 乍一看email的文档非常详尽。

My gut feeling would be to say that you're likely going to end up with the file in memory, however, which you did express chagrin at. 我的直觉是说你可能会在内存中结束这个文件,然而,你确实对此表示懊恼。

You might want to take a look at what Django has done. 你可能想看看Django做了什么。 They have a really nice implementation of custom file upload handlers, which allows you to subclass them to enable things like progress bars etc. See the documentation and the relevant code - even if you don't want to use Django, it's bound to give you some ideas. 他们有一个非常好的自定义文件上传处理程序实现,它允许你子类化它们以启用进度条等等。查看文档相关代码 - 即使你不想使用Django,它一定会给你一些想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM