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

python, upload, wsgi

Solution

As you suggested, I would (and have done before) override the `make_file` method of a `FieldStorage` object. 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.

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?

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. Then you don't have to have a series of AJAX requests to get the progress.

Problem

The task is simple: on the server side (python) accept an HTTP POST which contains an uploaded file and more form parameters. 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). Some advise to redefine the FieldStorage.make_file method(), which seems to break down the cgi implementation (weird...). I am currently able to read the entire wsgi input, chunk by chunk, to the filesystem, resulting in the following data: ``` -----------------------------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? Or should I do this manually? (Python 2.5 that is) Thanks.

Original source