PYTHON 99
Submit.py Guest on 10th June 2020 02:28:59 AM
  1. #!/usr/local/bin/python
  2. """This demonstrates a minimal http upload cgi.
  3. This allows a user to upload up to three files at once.
  4. It is trivial to change the number of files uploaded.
  5.  
  6. This script has security risks. A user could attempt to fill
  7. a disk partition with endless uploads.
  8. If you have a system open to the public you would obviously want
  9. to limit the size and number of files written to the disk.
  10. """
  11. import cgi
  12. import time
  13. import cgitb; cgitb.enable()
  14. import os, sys
  15. try: # Windows needs stdio set for binary mode.
  16.     import msvcrt
  17.     msvcrt.setmode (0, os.O_BINARY) # stdin  = 0
  18.     msvcrt.setmode (1, os.O_BINARY) # stdout = 1
  19. except ImportError:
  20.     pass
  21.  
  22. UPLOAD_DIR = "c:\\websites\\jibbering\\mapping\\gpx"
  23.  
  24. HTML_TEMPLATE = """
  25. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  26. <html>
  27.  <head>
  28.    <title>Convert GPX tracklog to javascript tracklog</title>
  29. <style type="text/css">
  30. body { background-color:white; color:black }
  31. h1 { color:#880000; }
  32. input { border:1px solid black; }
  33. error { border:1px solid black; color:#cc0000; background-color:white;padding:4px; }
  34. textarea {height:8em;width:42em;}
  35. </style>
  36. <script type="text/javascript" src="submit.js"></script>
  37.  </head>
  38.  
  39.  <body onload="checkProcess('%(FILENAME)s')">
  40.    <h1>Convert GPX tracklog to javascript tracklog</h1>
  41.                 <p>This page converts a GPX tracklog in XML format into JSON format suitable for faster rendering from <a href="/mapping/">GPS tracklog mapper</a>.</p>
  42. <form action="submit.py" onsubmit="return check(this);return false" method=post enctype="multipart/form-data">
  43. URL of GPX file: <input type="text" name=url value="exmouth.xml"> (preferred method, no data held on this server)<br><br>
  44. GPX file to upload: <input type="file" name=file><br> This data will be stored on the server, I don't really want it, <a href="http://www.openstreetmap.org/">these people do though</a>.<br><br>
  45. <input type="submit" value="Convert to JSON">
  46. </form>
  47. <hr>
  48. <div id="output"></div>
  49.  
  50.  </body>
  51. </html>
  52. """
  53.  
  54. def print_html_form (filename):
  55.     print "content-type: text/html; charset=utf-8\n"
  56.     print HTML_TEMPLATE % {"FILENAME": filename }
  57.  
  58. def save_uploaded_file (form_field, upload_dir):
  59.     """This saves a file uploaded by an HTML form.
  60.       The form_field is the name of the file input field from the form.
  61.       For example, the following form_field would be "file_1":
  62.           <input name="file_1" type="file">
  63.       The upload_dir is the directory where the file will be written.
  64.       If no file was uploaded or if the field does not exist then
  65.       this does nothing.
  66.    """
  67.     form = cgi.FieldStorage()
  68.     if not form.has_key(form_field): return
  69.     fileitem = form[form_field]
  70.     if not fileitem.file: return
  71.     fname = str(time.time()) + ".gpx"
  72.     fout = file (os.path.join(upload_dir, fname), 'wb')
  73.     while 1:
  74.         chunk = fileitem.file.read(100000)
  75.         if not chunk: break
  76.         fout.write (chunk)
  77.     fout.close()
  78.     return fname
  79.  
  80. filename = save_uploaded_file ("file", UPLOAD_DIR)
  81.  
  82. print_html_form (filename)

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.