Skip to content

Instantly share code, notes, and snippets.

@pythoninthegrass
Created September 25, 2024 22:06
Show Gist options
  • Save pythoninthegrass/7e1160e531c767ad4c67eb563ca97b8b to your computer and use it in GitHub Desktop.
Save pythoninthegrass/7e1160e531c767ad4c67eb563ca97b8b to your computer and use it in GitHub Desktop.
http.server implementation of serving unencrypted directory content (e.g., kickstart files) over port 8000
#!/usr/bin/env python
import http.server
import os
import socketserver
from pathlib import Path
PORT = 8000
if os.getenv("WORK_DIR") is not None:
work_dir = os.getenv("WORK_DIR")
else:
work_dir = str(Path.cwd())
class CustomHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=work_dir, **kwargs)
try:
with socketserver.TCPServer(("", PORT), CustomHTTPRequestHandler) as httpd:
print("Serving at port", PORT)
httpd.serve_forever()
except KeyboardInterrupt:
print("\nGoodbye!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment