Skip to content

Instantly share code, notes, and snippets.

@tsprlng
Last active February 25, 2023 01:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsprlng/0c1850109ae3a520b7f72e66dfc471fc to your computer and use it in GitHub Desktop.
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)
#!/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