-
-
Save jessta/326fdf4279c3d559131ef9563e3c18bd to your computer and use it in GitHub Desktop.
SimpleHTTPServer with history API fallback
This file contains hidden or 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 os | |
import sys | |
import urllib | |
import http.server | |
class Handler(http.server.SimpleHTTPRequestHandler): | |
def do_GET(self): | |
urlparts = urllib.parse.urlparse(self.path) | |
request_file_path = urlparts.path.strip('/') | |
if not os.path.exists(request_file_path): | |
self.path = 'index.html' | |
return http.server.SimpleHTTPRequestHandler.do_GET(self) | |
host = '0.0.0.0' | |
try: | |
port = int(sys.argv[1]) | |
except IndexError: | |
port = 8000 | |
httpd = http.server.HTTPServer((host, port), Handler) | |
print('Serving HTTP on %s port %d ...' % (host, port)) | |
httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment