You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.2 KiB
Plaintext

5 years ago
#!/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()
5 years ago
from settings import UPLOAD_DIR
5 years ago
def upload (inputname, upload_dir):
form = cgi.FieldStorage()
5 years ago
if not inputname in form: return
5 years ago
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":
5 years ago
result = upload("thefile", UPLOAD_DIR)
5 years ago
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>
5 years ago
<meta charset="utf-8">
<title>upload</title>
5 years ago
</head>
<body>
<form method="post" action="" enctype="multipart/form-data">
<input type="file" name="thefile" /><input type="submit" />
</form>
</body>
5 years ago
</html>)