Last active
August 29, 2015 14:00
-
-
Save 10sr/11039647 to your computer and use it in GitHub Desktop.
Small CGI Uploader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # post.py --- Small Uploader | |
| # datadir = "posted" | |
| datadir = "" | |
| FORM_HTML = """<form action="post.py" method="post" enctype="multipart/form-data" required> | |
| <p><input type="file" name="content" value="" /></p> | |
| <p>Name (optional) <input type="text" name="filename" /></p> | |
| <p><input type="submit" name="submit" value="submit" /></p> | |
| </form> | |
| """ | |
| import sys | |
| import os | |
| import datetime | |
| import cgi | |
| def main(): | |
| try: | |
| met = os.environ["REQUEST_METHOD"].upper() | |
| except KeyError: | |
| met = None | |
| # print contents | |
| print("Status: %s %s" % (200, "OK")) | |
| print("Content-Type: text/html") | |
| print("") | |
| if met == "POST": | |
| form = cgi.FieldStorage() | |
| # print(form.type) | |
| assert(form.type.startswith("multipart/")) | |
| f = form['content'] | |
| try: | |
| filename_orig = form['filename'].value or f.filename | |
| except KeyError: | |
| filename_orig = f.filename | |
| content = f.value | |
| timestr = datetime.datetime.today().strftime("%y%m%d-%H%M%S") | |
| filename = os.path.join(datadir, "".join((timestr, "-", filename_orig))) | |
| print("<p>") | |
| print("Posted: " + filename) | |
| print("</p>") | |
| print(FORM_HTML) | |
| else: | |
| # met == "GET" | |
| print(FORM_HTML) | |
| return | |
| if __name__ == "__main__": | |
| try: | |
| main() | |
| except Exception as e: | |
| print("Status: %s %s" % (200, "OK")) | |
| print("Content-Type: text/html") | |
| print("") | |
| print(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment