import argparse import html import http.server import mimetypes import socketserver import zipfile # wait until zipfile is fixed... https://bugs.python.org/issue40564 import zipp from os.path import basename, dirname from urllib.parse import urlparse, unquote parser = argparse.ArgumentParser() parser.add_argument("archive") parser.add_argument("port", type=int, default=8888, nargs='?') parser.add_argument("address", default="127.0.0.1", nargs='?') parser.add_argument("root", default='', nargs='?') args = parser.parse_args() archive = zipfile.ZipFile(args.archive) class ZipHTTPRequestHandler(http.server.BaseHTTPRequestHandler): def do_GET(s): suffix = unquote(urlparse(s.path).path.strip('/')) path = zipp.Path(archive, args.root) / suffix if path.is_dir(): dir_path = path path = path / 'index.html' else: dir_path = None if path.is_file(): s.send_response(200) s.send_header("Content-type", mimetypes.guess_type(path.name)[0]) s.end_headers() s.wfile.write(path.read_bytes()) elif dir_path is not None: s.send_response(200) s.send_header("Content-type", "text/html") s.end_headers() html_dirlist = ''.join(f'