Skip to content

Instantly share code, notes, and snippets.

@jessta
Forked from martijnvermaat/server.py
Last active October 3, 2022 03:26
Show Gist options
  • Save jessta/326fdf4279c3d559131ef9563e3c18bd to your computer and use it in GitHub Desktop.
Save jessta/326fdf4279c3d559131ef9563e3c18bd to your computer and use it in GitHub Desktop.
SimpleHTTPServer with history API fallback
#!/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