Chris Dutrow
Chris Dutrow

Reputation: 50362

Handle file from WSGI request

Question

What is a good way to handle a file that has been uploaded through a WSGI POST request?

More info

So far, I'm able to read the raw POST data from environ[wsgi.input]. At this point the issue I am having is that the information associated with the file and the file itself are jammned together into one string:

'------WebKitFormBoundarymzmB1wyHKjyqZrDm
Content-Disposition: form-data; name="file"; filename="new file.wav"
Content-Type: audio/wav

THIS IS THE CONTENT
THIS IS THE CONTENT
THIS IS THE CONTENT
THIS IS THE CONTENT
THIS IS THE CONTENT

------WebKitFormBoundarymzmB1wyHKjyqZrDm--
'

Is there a library in python I should be using to handle information more cleanly? Ultimately, I'd like to take the file contents and then turn around and upload to Amazon S3.

Upvotes: 5

Views: 1479

Answers (2)

jwalker
jwalker

Reputation: 2009

Usually you want much more abstraction than raw WSGI. Consider frameworks that run on WSGI.

Upvotes: -1

falsetru
falsetru

Reputation: 369054

You can use cgi.FieldStorage.

import cgi
form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ)
f = form['file'].file
# You can use `f` as a file object: f.read(...)

Upvotes: 3

Related Questions