Skip to content

Instantly share code, notes, and snippets.

@calesce
Forked from martijnvermaat/server.py
Created October 23, 2017 23:04
Show Gist options
  • Save calesce/61804611b2c110a9fce0c2994ec2df3b to your computer and use it in GitHub Desktop.
Save calesce/61804611b2c110a9fce0c2994ec2df3b to your computer and use it in GitHub Desktop.
SimpleHTTPServer with history API fallback, works with python and python3
#!/usr/bin/env python
"""
Modification of `python -m SimpleHTTPServer` with a fallback to /index.html
on requests for non-existing files.
This is useful when serving a static single page application using the HTML5
history API.
"""
import os
import sys
try:
urlparser = __import__("urlparse")
except:
urlparser = __import__("urllib.parse")
globals()["urlparse"] = urlparser.parse
else:
globals()["urlparse"] = urlparser
try:
httpserver = __import__("SimpleHTTPServer")
base = __import__("BaseHTTPServer")
except:
httpserver = __import__("http.server")
globals()["SimpleHTTPServer"] = httpserver.server
globals()["BaseHTTPServer"] = httpserver.server
else:
globals()["SimpleHTTPServer"] = httpserver
globals()["BaseHTTPServer"] = base
class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
urlparts = urlparse.urlparse(self.path)
request_file_path = urlparts.path.strip('/')
if not os.path.exists(request_file_path):
self.path = 'index.html'
return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
host = '0.0.0.0'
try:
port = int(sys.argv[1])
except IndexError:
port = 8000
httpd = BaseHTTPServer.HTTPServer((host, port), Handler)
print ("Serving HTTP on port %d" % (port))
httpd.serve_forever()
@gogoprog
Copy link

Ah I knew someone would have added python3 support!
Thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment