Skip to content

Instantly share code, notes, and snippets.

@ckunte
Last active March 12, 2024 05:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ckunte/c7f54d485b79730ddfe4c4dce1c451aa to your computer and use it in GitHub Desktop.
Save ckunte/c7f54d485b79730ddfe4c4dce1c451aa to your computer and use it in GitHub Desktop.
SimpleHTTPServer script with clean URLs
#!/usr/bin/env python
# http.server script for clean URLs
# Credits: R. Scott-Adams ( https://stackoverflow.com/a/28467686 )
import os
import http.server
class SuffixHandler(http.server.SimpleHTTPRequestHandler):
"""
Overrides the default request handler to assume a suffixless
resource is actually an html page of the same name. Thus,
http://localhost:8000/foo would find foo.html.
"""
def do_GET(self):
path = self.translate_path(self.path)
# If the path doesn't exist, then assume it's a resource
# suffixed with '.html':
if not os.path.exists(path):
self.path = self.path + '.html'
# Call the superclass methods to actually serve the page:
http.server.SimpleHTTPRequestHandler.do_GET(self)
http.server.test(SuffixHandler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment