This example shows a Python CGI script and HTML page to allow people to submit files.
Try it: web link
Each submission is assigned its own sub-directory, with two files in it. The
file submission
will contain the file that was submitted. The file
info
will have the other information that the user provided.
FieldStorage
classThe preferred way to obtain form variables or the sections of a multipart file object is
to use an instance of the FieldStorage
class:
form = cgi.FieldStorage()
The top-level FieldStorage
object will have an attribute, list
,
that contains a list fo all the fields contained in the query. Each of these will be a
FieldStorage
object.
key
is assigned one or
more values in the query by calling
form.has_key[key]
form.keys()
key
, and get a FieldStorage
object associated with the key:
form[key]
FieldStorage
objectsFieldStorage
objects have these attributes:
name
- The name of the field, or equivalently the name of the query variable.
value
- The value of the field. For everything except a file input, this is
what you want.
filename
- If the field corresponds to a file input, filename
is
the name of the file on the browser's machine. See line 63 of the Python program below:
file
- If the field corresponds to a file input, the file
attribute is an open file object that can be used for reading the file. See lines 40-44
of the Python script below
George K. Thiruvathukal, Thomas W. Christopher, and John P. Shafaee, Web Programming, Techniques for Integrating Python, Linux, Apache, and MySQL, Prentice-Hall, 2002. p 348-354.
This book discusses some interesting Python software, but apparently the code is not available anymore, at least not from www.tools-of-computing.com, as stated in the book.
submitfile.py
01: #!/usr/bin/python 02: import cgi, cgitb, os, sys 03: import types, time, shutil 04: cgitb.enable(); # formats errors in HTML 05: 06: sys.stderr = sys.stdout 07: print "Content-type: text/html" 08: print 09: print '''<html> 10: <head> 11: <title>File upload</title> 12: </head> 13: <body>''' 14: 15: form = cgi.FieldStorage() 16: 17: okay = 1 18: if not form.getvalue('LASTNAME'): 19: if okay: '<h1>Problem</h1>' 20: print '<p>Your last name is required.</p>' 21: okay = 0 22: if not form.getvalue('EMAIL'): 23: if okay: '<h1>Problem</h1>' 24: print '<p>Your email address is required.</p>' 25: okay = 0 26: if not form.getvalue('FILE'): 27: if okay: '<h1>Problem</h1>' 28: print '<p>A file is required.</p>' 29: okay = 0 30: if not okay: 31: print '<p>Please try again.</p>' 32: print '</body></html>' 33: sys.exit() 34: 35: docId = 'ID' + hex(os.getpid())[2:]+'-' 36: docId = docId + hex(long(time.time()))[2:-1] 37: dir = docId 38: os.mkdir(dir) 39: os.chmod(dir,0775) 40: file = form["FILE"].file 41: dst = dir + '/submission' 42: dstfile=open(dst,'wb') 43: shutil.copyfileobj(file,dstfile,1024) 44: dstfile.close() 45: filesize = os.path.getsize(dst) 46: if filesize==0: 47: print '<h1>Problem</h1>' 48: print '<p>The file did not come through.' 49: print 'Make sure it exists and is not a directory and try again.</p>' 50: print '<p>Please try again</p>' 51: os.remove(dst) 52: os.rmdir(dir) 53: print '</body></html>' 54: sys.exit() 55: 56: os.chmod(dst,0664) 57: info=open(dir+'/info','w') 58: info.write('Last: '+form['LASTNAME'].value + os.linesep) 59: first = form.getvalue('FIRSTNAME') 60: if first: 61: info.write('First: '+ first + os.linesep) 62: info.write('Email: ' + form['EMAIL'].value+os.linesep) 63: info.write('Filename: ' + form['FILE'].filename+os.linesep) 64: info.write(os.linesep + form['COVER'].value+os.linesep) 65: info.close() 66: os.chmod(dir+'/info',0664) 67: print '<h1>Thank you for your submission</h1>' 68: print '<p>Your submission ID is: <b>', docId + '</b>.' 69: print '<p>Please refer to it in all communications.' 70: print '<p>You submitted file <b>', form['FILE'].filename + ' </b>' 71: print '<p>Filesize: <b>', filesize, 'bytes</b>' 72: print '</body></html>'
submitfile.html
01: <html> 02: <head><title>Submit a file</title></head> 03: <body> 04: <h1>Submit a file</h1> 05: <form action="https://johnloomis.org/python/submitfile.py" 06: enctype="multipart/form-data" method="POST"> 07: <h2>Your name and email</h2> 08: 09: <p><table> 10: <tr><td>Last name: <td> <input type="text" name="LASTNAME" size=30> 11: <tr><td>First name: <td> <input type="text" name="FIRSTNAME" size=30> 12: <tr><td>Email: <td> <input type="text" name="EMAIL" size=50> 13: </table> 14: 15: <h2>Document</h2> 16: 17: <p>The file: <input type="FILE" name="FILE" size=20> 18: 19: <h2>Cover note</h2> 20: 21: <p><textarea name="COVER" cols=40 rows=8 wrap="physical"></textarea> 22: 23: <p><input type="SUBMIT"> 24: </form> 25: </body><html>
Here is a sample response for a successful transmission:
Thank you for your submissionYour submission ID is: ID111c-47E6AC49. Please refer to it in all communications. You submitted file C:\classes\python\cgi-bin\submitfile.py Filesize: 2020 bytes |
Maintained by John Loomis, updated Sun Mar 23 15:17:09 2008