RaspberryPi4b/SourceCode-NAS-Olivito_Gian...

26 lines
693 B
Python
Raw Permalink Normal View History

2024-12-20 13:28:04 +01:00
# Import libraries
import http.server
import socketserver
2025-01-09 17:05:20 +01:00
import os
2024-12-20 13:28:04 +01:00
2025-01-09 17:05:20 +01:00
# Define port and path to external drive directory
port = 8000
directory = "/media/gianluca/Elements"
2024-12-20 13:28:04 +01:00
class MyHandler(http.server.SimpleHTTPRequestHandler):
def translatePath(self, path):
2025-01-09 17:05:20 +01:00
# Modify default behavior of SimpleHTTPRequestHandler
2024-12-20 13:28:04 +01:00
path = super().translate_path(path)
2025-01-09 17:05:20 +01:00
# Ensure correct path handling by joining directories
if not path.startswith(directory):
path = os.path.join(directory, path.lstrip("/"))
return path
2024-12-20 13:28:04 +01:00
handler = MyHandler
2025-01-09 17:05:20 +01:00
httpd = socketserver.TCPServer(("", port), handler)
2024-12-20 13:28:04 +01:00
2025-01-09 17:05:20 +01:00
print(f"Serving on port {port}")
httpd.serve_forever()