Created
September 25, 2024 22:06
-
-
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
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 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