Skip to content

Instantly share code, notes, and snippets.

@KSHMK
Created December 31, 2021 07:39
Show Gist options
  • Save KSHMK/9169054fc7319d1fe34b9c0094ff7e16 to your computer and use it in GitHub Desktop.
Save KSHMK/9169054fc7319d1fe34b9c0094ff7e16 to your computer and use it in GitHub Desktop.
upload file simple http server
import html
import http.client
import io
import os
import socket # For gethostbyaddr()
import sys
import urllib.parse
import contextlib
import re
import http.server
from functools import partial
from http import HTTPStatus
__version__ = "0.1"
class UploadHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
""" Upload HTTp request handler with GET and POST, HEAD commands.
This serves file from the current directory and any of its subdirectories.
The MIME type for file is determined by calling the .guess_type() method.
"""
def do_POST(self):
path = self.translate_path(self.path)
print(path)
data_length = 0
if "Content-Length" in self.headers:
data_length = int(self.headers["Content-Length"])
boundary = ""
if "Content-Type" in self.headers:
content_type = self.headers["Content-Type"]
idx = content_type.find("boundary=")
if idx != -1:
boundary = content_type[idx+9:].encode()
data_idx = 0
while data_idx < data_length:
buf = self.rfile.readline()
data_idx += len(buf)
if buf.find(boundary) != -1:
break
while data_idx < data_length:
filenameraw = self.rfile.readline()
data_idx += len(filenameraw)
filename = re.findall(".*filename=\"(.*)\"",filenameraw.decode())
if len(filename) == 0:
break
filepath = os.path.join(path,filename[0])
tmp = self.rfile.readline()
data_idx += len(tmp)
tmp = self.rfile.readline()
data_idx += len(tmp)
f = open(filepath,"wb")
prev_buf = self.rfile.readline()
data_idx += len(prev_buf)
while data_idx < data_length:
buf = self.rfile.readline()
data_idx += len(buf)
if buf.find(boundary) != -1:
f.write(prev_buf[:-2])
break
f.write(prev_buf)
prev_buf = buf
f.close()
if buf.find(boundary+b"--") != -1:
break
f = self.send_head()
if f:
try:
self.copyfile(f, self.wfile)
finally:
f.close()
def list_directory(self, path):
try:
list = os.listdir(path)
except OSError:
self.send_error(
HTTPStatus.NOT_FOUND,
"No permission to list directory")
return None
list.sort(key=lambda a: a.lower())
r = []
try:
displaypath = urllib.parse.unquote(self.path,
errors='surrogatepass')
except UnicodeDecodeError:
displaypath = urllib.parse.unquote(path)
displaypath = html.escape(displaypath, quote=False)
enc = sys.getfilesystemencoding()
title = 'Directory listing for %s' % displaypath
r.append('<!DOCTYPE html>')
r.append('<html>\n<head>')
r.append('<meta http-equiv="Content-Type" '
'content="text/html; charset=%s">' % enc)
r.append('<title>%s</title>\n</head>' % title)
r.append('<body id="body">\n<h1>%s</h1>' % title)
r.append('<hr>\n<ul>')
for name in list:
fullname = os.path.join(path, name)
displayname = linkname = name
# Append / for directories or @ for symbolic links
if os.path.isdir(fullname):
displayname = name + "/"
linkname = name + "/"
if os.path.islink(fullname):
displayname = name + "@"
# Note: a link to a directory displays with @ and links with /
r.append('<li><a href="%s">%s</a></li>'
% (urllib.parse.quote(linkname,
errors='surrogatepass'),
html.escape(displayname, quote=False)))
r.append('</ul>\n<hr>')
T = '''<form action="'''+displaypath+'''" method="post" enctype="multipart/form-data">
<input type="file" id="uploadfile" name="file">
<input type="submit">
</form>
<script>
const dropArea = document.getElementById("body")
dropArea.addEventListener('dragover', (event) => {
event.stopPropagation();
event.preventDefault();
// Style the drag-and-drop as a "copy file" operation.
event.dataTransfer.dropEffect = 'copy';
});
dropArea.addEventListener('drop', (event) => {
event.stopPropagation();
event.preventDefault();
const uploadfile = document.getElementById("uploadfile");
const fileList = event.dataTransfer.files;
uploadfile.files = fileList
console.log(fileList[0]);
});
</script>
'''
r.append(T)
r.append('</body>\n</html>\n')
encoded = '\n'.join(r).encode(enc, 'surrogateescape')
f = io.BytesIO()
f.write(encoded)
f.seek(0)
self.send_response(HTTPStatus.OK)
self.send_header("Content-type", "text/html; charset=%s" % enc)
self.send_header("Content-Length", str(len(encoded)))
self.end_headers()
return f
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--bind', '-b', metavar='ADDRESS',
help='Specify alternate bind address '
'[default: all interfaces]')
parser.add_argument('--directory', '-d', default=os.getcwd(),
help='Specify alternative directory '
'[default: current directory]')
parser.add_argument('port', action='store',
default=8000, type=int,
nargs='?',
help='Specify alternate port [default: 8000]')
args = parser.parse_args()
handler_class = partial(UploadHTTPRequestHandler,
directory=args.directory)
# ensure dual-stack is not disabled; ref #38907
class DualStackServer(http.server.ThreadingHTTPServer):
def server_bind(self):
# suppress exception when protocol is IPv4
with contextlib.suppress(Exception):
self.socket.setsockopt(
socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
return super().server_bind()
http.server.test(
HandlerClass=handler_class,
ServerClass=DualStackServer,
port=args.port,
bind=args.bind,
)
@KSHMK
Copy link
Author

KSHMK commented Dec 31, 2021

Todo

  • if upload file is large this will be not working so we need to split file upload

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment