Adapt path handling

main
Gianluca Olivito 2025-01-09 17:05:20 +01:00
parent f9e6f5e9e1
commit b394aea469
1 changed files with 14 additions and 8 deletions

View File

@ -1,19 +1,25 @@
# Import libraries
import http.server
import socketserver
import os
# Define port and path tho external drive directory
PORT = 8000
DIRECTORY = "/mnt/mydisk"
# Define port and path to external drive directory
port = 8000
directory = "/media/gianluca/Elements"
class MyHandler(http.server.SimpleHTTPRequestHandler):
def translatePath(self, path):
# Modifizieren des Standardverhaltens von SimpleHTTPRequestHandler
# Modify default behavior of SimpleHTTPRequestHandler
path = super().translate_path(path)
return path if path.startswith(DIRECTORY) else DIRECTORY + path
# Ensure correct path handling by joining directories
if not path.startswith(directory):
path = os.path.join(directory, path.lstrip("/"))
return path
handler = MyHandler
httpd = socketserver.TCPServer(("", PORT), handler)
httpd = socketserver.TCPServer(("", port), handler)
print(f"Serving on port {PORT}")
print(f"Serving on port {port}")
httpd.serve_forever()