u.twoha.cc/server.py
2024-09-13 19:49:18 -05:00

32 lines
834 B
Python

import http.server
import socketserver
import sys
from pathlib import Path
dir = sys.argv[1]
class RequestHandler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=dir, **kwargs)
def do_GET(self):
if Path(f'{dir}/{self.path}').is_dir():
self.path += '/index.html'
elif self.path[-1] != '/' and Path(self.path).suffix == '':
self.path += '.html'
super().do_GET()
port = 8080
server = socketserver.TCPServer(('localhost', port), RequestHandler, False)
server.allow_reuse_port = True
server.allow_reuse_address = True
server.server_bind()
server.server_activate()
try:
print(f'http://localhost:{port}')
server.serve_forever()
except KeyboardInterrupt:
server.shutdown()
server.server_close()