What Python code for replacing cgi.FieldStorage for reading contents of uploaded files?

130 Views Asked by At

Since the python cgi module will soon be removed, I need a way to capture file contents uploaded through an HTML form that looks like this:

<form id="uploadfile" method="post" action="cgi-bin/getfile.py">
<p>
Filename:
<input type="file" device="files" name="srcfile" 
size="55" value="" />
</p>
...
<input type="submit" value="Submit">
</form>

This currently works for me:

import cgi

form = cgi.FieldStorage()
fileitem = form['srcfile']
contents = fileitem.file.read()

Using urllib, I can get a dictionary of form fields with:

from urllib.parse import parse_qs

form = parse_qs(qs, encoding="utf-8")
filename = form['srcfile'][0]

But I can't see from the urllib documentation how to read the contents of the uploaded file.

I've also looked at multipart.multipart.FormParser(), but I can't find any examples to clarify its usage.

0

There are 0 best solutions below