Last active
November 15, 2023 13:42
-
-
Save countingpine/271627d9e4dc5812392dbac640e36009 to your computer and use it in GitHub Desktop.
SimpleHTTPUploadServer (from https://stackoverflow.com/a/58255859/446106 by smidgey)
This file contains 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
# https://stackoverflow.com/a/58255859/446106 | |
HOST=1.2.3.4 | |
FILE1=foo.txt | |
FILE2=bar.txt | |
# Running the server on your host: | |
python3 ./httpupload.py | |
# Download a file from your host: | |
curl -O http://$HOST:44444/$FILENAME | |
# Upload a file back to your host: | |
curl -F "file=@$FILE1" http://$HOST:44444/ | |
# Multiple file upload supported, just add more -F 'file=@<FILENAME>' | |
# parameters to the command line. | |
curl -F "file=@$FILE1" -F "file=@$FILE2" http://$HOST:44444/ |
This file contains 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/env python3 | |
import http.server | |
import socketserver | |
import io | |
import cgi | |
# Change this to serve on a different port | |
PORT = 44444 | |
class CustomHTTPRequestHandler(http.server.SimpleHTTPRequestHandler): | |
def do_POST(self): | |
r, info = self.deal_post_data() | |
print(r, info, "by: ", self.client_address) | |
f = io.BytesIO() | |
if r: | |
f.write(b"Success\n") | |
else: | |
f.write(b"Failed\n") | |
length = f.tell() | |
f.seek(0) | |
self.send_response(200) | |
self.send_header("Content-type", "text/plain") | |
self.send_header("Content-Length", str(length)) | |
self.end_headers() | |
if f: | |
self.copyfile(f, self.wfile) | |
f.close() | |
def deal_post_data(self): | |
ctype, pdict = cgi.parse_header(self.headers['Content-Type']) | |
pdict['boundary'] = bytes(pdict['boundary'], "utf-8") | |
pdict['CONTENT-LENGTH'] = int(self.headers['Content-Length']) | |
if ctype == 'multipart/form-data': | |
form = cgi.FieldStorage( fp=self.rfile, headers=self.headers, environ={'REQUEST_METHOD':'POST', 'CONTENT_TYPE':self.headers['Content-Type'], }) | |
print (type(form)) | |
try: | |
if isinstance(form["file"], list): | |
for record in form["file"]: | |
open("./%s"%record.filename, "wb").write(record.file.read()) | |
else: | |
open("./%s"%form["file"].filename, "wb").write(form["file"].file.read()) | |
except IOError: | |
return (False, "Can't create file to write, do you have permission to write?") | |
return (True, "Files uploaded") | |
Handler = CustomHTTPRequestHandler | |
with socketserver.TCPServer(("", PORT), Handler) as httpd: | |
print("serving at port", PORT) | |
httpd.serve_forever() |
This file contains 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
<!-- https://gist.github.com/yczheng0/e1bf7b0986dcc871a1f919e2138fb737/059aca1d510c615df3d9fedafabac4d538ebe352#file-simplehttpserverwithupload-py-L191 --> | |
<form ENCTYPE="multipart/form-data" action="/" method=post> | |
<input type=file name=file> | |
<input type=submit> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment