Guest User

Untitled

a guest
Sep 11th, 2019
104
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sample file upload form:
  2.  
  3. <html>
  4. <body>
  5. <form enctype = "multipart/form-data"
  6. action = "/bin/up.py" method = "post">
  7. <p>File: <input type = "file" name = "filename" /></p>
  8. <p><input type = "submit" value = "Upload" /></p>
  9. </form>
  10. </body>
  11. </html>
  12.  
  13. Python 3 upload script:
  14.  
  15. #!/usr/bin/python3
  16. import cgi, os
  17. import cgitb; cgitb.enable()
  18. form = cgi.FieldStorage()
  19. fileitem = form['filename']
  20. if fileitem.filename:
  21. fn = os.path.basename(fileitem.filename)
  22. open('/tmp/' + fn, 'wb').write(fileitem.file.read())
  23. message = 'The file "' + fn + '" was uploaded successfully'
  24.  
  25. else:
  26. message = 'No file was uploaded'
  27.  
  28. print('Content-Type: text/plain\n')
  29. print('Ciao, ' + message)
  30.  
  31. Fails with SOME unicode characters in file name!
RAW Paste Data