Created
October 11, 2024 16:17
-
-
Save xyb/c40b621c749bc251fee872b41456a9e2 to your computer and use it in GitHub Desktop.
http server for offline website retrieved by wget --mirror
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
# serve your offline website static files retrieved by: | |
# wget --mirror --convert-links --adjust-extension --page-requisites --no-parent | |
# usage: | |
# python3 wgetserver.py [-p 8080] | |
import http.server | |
import os | |
import argparse | |
class CustomHTTPRequestHandler(http.server.SimpleHTTPRequestHandler): | |
def translate_path(self, path): | |
path = super().translate_path(path) | |
folder_index = path + 'index.html' | |
file_index = path[:-1] + '.html' | |
html_file = path + '.html' | |
if self.path.endswith('/'): | |
if os.path.exists(folder_index): | |
path = folder_index | |
elif os.path.exists(file_index): | |
path = file_index | |
elif os.path.exists(html_file): | |
path = html_file | |
return path | |
def run(server_class=http.server.HTTPServer, handler_class=CustomHTTPRequestHandler, port=8000): | |
server_address = ('', port) | |
httpd = server_class(server_address, handler_class) | |
print(f'Serving HTTP on port {port}...') | |
httpd.serve_forever() | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='Simple HTTP Server for serving static files by wget --mirror.') | |
parser.add_argument('-p', '--port', type=int, default=8000, help='Port to start the server on') | |
args = parser.parse_args() | |
run(port=args.port) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment