|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
import cgitb; cgitb.enable()
|
|
|
|
import cgi, os
|
|
|
|
|
|
|
|
|
|
|
|
env = os.environ
|
|
|
|
method = os.environ.get("REQUEST_METHOD")
|
|
|
|
|
|
|
|
print ("Content-type:text/html;charset=utf-8")
|
|
|
|
print ()
|
|
|
|
# print "Hello", method
|
|
|
|
# cgi.print_environ()
|
|
|
|
|
|
|
|
from settings import UPLOAD_DIR
|
|
|
|
|
|
|
|
def upload (inputname, upload_dir):
|
|
|
|
form = cgi.FieldStorage()
|
|
|
|
if not inputname in form: return
|
|
|
|
fileitem = form[inputname]
|
|
|
|
if not fileitem.file: return
|
|
|
|
fp = os.path.join(upload_dir, fileitem.filename)
|
|
|
|
with open(fp, 'wb') as fout:
|
|
|
|
bytes = 0
|
|
|
|
while 1:
|
|
|
|
chunk = fileitem.file.read(100000)
|
|
|
|
if not chunk: break
|
|
|
|
bytes += len(chunk)
|
|
|
|
fout.write(chunk)
|
|
|
|
return bytes, fileitem.filename
|
|
|
|
|
|
|
|
if method == "POST":
|
|
|
|
result = upload("thefile", UPLOAD_DIR)
|
|
|
|
if result:
|
|
|
|
bytes, filename = result
|
|
|
|
print ("{0} bytes written to {1}<br />".format(bytes, filename))
|
|
|
|
print ("<a href="">upload another</a>")
|
|
|
|
else:
|
|
|
|
print ("""<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<title>upload</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<form method="post" action="" enctype="multipart/form-data">
|
|
|
|
<input type="file" name="thefile" /><input type="submit" />
|
|
|
|
</form>
|
|
|
|
</body>
|
|
|
|
</html>""")
|