Skip to content

Instantly share code, notes, and snippets.

@tmeissner
Created December 16, 2012 23:28
Show Gist options
  • Save tmeissner/4314180 to your computer and use it in GitHub Desktop.
Save tmeissner/4314180 to your computer and use it in GitHub Desktop.
Python file upload
#!/usr/bin/python
import yate
import cgi
import os
form = cgi.FieldStorage()
item = form["filename"]
message = 'File uploaded'
if item.file:
fout = file(str(os.getcwd()) + '/../data/drewag.pdf', 'wb')
filesize = 0
while 1:
if (filesize > (1024*1024)):
message = "File too big, upload aborted"
break
chunk = item.file.read(1024)
filesize = filesize + 1024
if not chunk:
break
fout.write(chunk)
print(yate.start_response())
print("<!DOCTYPE html>\n<html>")
print(yate.html_header())
print("<body>")
print(yate.para(message))
print(yate.include_footer({"Home": "/projects/drewag/drewag_upload.html"}))
print("</body>\n</html>")
fout.close()
if message != 'File uploaded':
os.remove(str(os.getcwd()) + '/../data/drewag.pdf')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment