Last active
July 21, 2022 03:39
-
-
Save ohtomi/74c49a4f6b460d9097f7 to your computer and use it in GitHub Desktop.
File Upload CGI Script written in Python
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
#!/bin/bash | |
mkdir ./cgi-bin/ | |
cp upload.cgi ./cgi-bin/ | |
chmod +x ./cgi-bin/upload.cgi | |
mkdir ./upload/ | |
python -m CGIHTTPServer 8080 |
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/python | |
# -*- coding: utf-8 -*- | |
import cgi, cgitb, os, sys | |
UPLOAD_DIR = './upload' | |
def save_uploaded_file(): | |
print 'Content-Type: text/html; charset=UTF-8' | |
print ''' | |
<html> | |
<head> | |
<title>Upload File</title> | |
</head> | |
<body> | |
''' | |
form = cgi.FieldStorage() | |
if not form.has_key('file'): | |
print '<h1>Not found parameter: file</h1>' | |
return | |
form_file = form['file'] | |
if not form_file.file: | |
print '<h1>Not found parameter: file</h1>' | |
return | |
if not form_file.filename: | |
print '<h1>Not found parameter: file</h1>' | |
return | |
uploaded_file_path = os.path.join(UPLOAD_DIR, os.path.basename(form_file.filename)) | |
with file(uploaded_file_path, 'wb') as fout: | |
while True: | |
chunk = form_file.file.read(100000) | |
if not chunk: | |
break | |
fout.write (chunk) | |
print '<h1>Completed file upload</h1>' | |
print ''' | |
<hr> | |
<a href="../upload.html">Back to upload page</a> | |
</body> | |
</html> | |
''' | |
cgitb.enable() | |
save_uploaded_file() |
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
<html> | |
<head> | |
<title>Upload File</title> | |
</head> | |
<body> | |
<h1>Upload File</h1> | |
<form action="cgi-bin/upload.cgi" method="POST" enctype="multipart/form-data"> | |
File: <input name="file" type="file"> | |
<input name="submit" type="submit"> | |
</form> | |
</body> | |
</html> |
Hi, i have uploaded file successfully to remote server from windows but its not going back to Upload.html file after clicking back.
My file is on my local windows machine.
replace href="../upload.html"
with href="./upload.html"
if it's in the same folder as upload.cgi
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, i have uploaded file successfully to remote server from windows but its not going back to Upload.html file after clicking back.
My file is on my local windows machine.