Skip to content

Small python snippet to allow ppl serving archived myst htmls #7

@agahkarakuzu

Description

@agahkarakuzu
import http.server
import socketserver
import os

DIRECTORY= "/Users/agah/Downloads/LivingPreprint_10.55458_NeuroLibre_00031_33723c"
BASE_URL = "/10.55458/neurolibre.00031"

class CustomHandler(http.server.SimpleHTTPRequestHandler):
    def translate_path(self, path):
        # Remove the base URL prefix from the path
        if path.startswith(BASE_URL):
            path = path[len(BASE_URL):]
        # Serve files from the specified directory
        path = os.path.join(DIRECTORY, path.lstrip("/"))
        return path

    def do_GET(self):
        # Check if the requested file exists
        file_path = self.translate_path(self.path)
        if not os.path.exists(file_path):
            # If file doesn't exist, try appending `.html`
            file_path += ".html"
            if os.path.exists(file_path):
                # Update the path to point to the .html file
                self.path += ".html"
        # Call the parent class's GET handler
        super().do_GET()

# Set the port for the server
PORT = 8000

with socketserver.TCPServer(("", PORT), CustomHandler) as httpd:
    print(f"Serving at http://localhost:{PORT}{BASE_URL}")
    httpd.serve_forever()

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions