Last active
February 25, 2023 01:26
-
-
Save tsprlng/0c1850109ae3a520b7f72e66dfc471fc to your computer and use it in GitHub Desktop.
basic http server for local dev, but not too basic (i.e. supports index.html but also /dir)
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/bin/env python | |
import os.path as ospath | |
import socket | |
from http.server import * | |
class Server(HTTPServer): | |
address_family = socket.AF_INET6 | |
pass | |
class Handler(SimpleHTTPRequestHandler): | |
def do_GET(self): | |
path = self.translate_path(self.path) | |
if path.endswith('/dir'): | |
f = super().list_directory(path[0:-4]) | |
self.copyfile(f, self.wfile) | |
f.close() | |
else: | |
if not ospath.exists(path): | |
self.path = self.path + '.html' | |
super().do_GET() | |
pass | |
server_address = ('::1', 8000) | |
httpd = Server(server_address, Handler) | |
httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment