-
-
Save Morreski/c7e02f3c63672f54674e to your computer and use it in GitHub Desktop.
Python SimpleHTTPServer for Static Serving (React / Angular / Ember) in HTML5 mode (a la mod_rewrite)
This file contains 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
''' | |
Taken from: | |
http://stackoverflow.com/users/1074592/fakerainbrigand | |
http://stackoverflow.com/questions/15401815/python-simplehttpserver | |
''' | |
import sys | |
import os | |
if sys.version_info < (3,): | |
import SimpleHTTPServer as server | |
import SocketServer as socketserver | |
import urlparse as parse | |
else: | |
from http import server | |
from urllib import parse | |
import socketserver | |
PORT = 8000 | |
INDEXFILE = 'index.html' | |
class MyHandler(server.SimpleHTTPRequestHandler): | |
def do_GET(self): | |
# Parse query data to find out what was requested | |
parsedParams = parse.urlparse(self.path) | |
# See if the file requested exists | |
if os.access('.' + os.sep + parsedParams.path, os.R_OK): | |
# File exists, serve it up | |
server.SimpleHTTPRequestHandler.do_GET(self) | |
else: | |
# send index.html, but don't redirect | |
self.send_response(200) | |
self.send_header('Content-Type', 'text/html') | |
self.end_headers() | |
with open(INDEXFILE, 'rb') as fin: | |
self.copyfile(fin, self.wfile) | |
if __name__ == "__main__": | |
args = sys.argv[1:] | |
if len(args) > 0: | |
PORT = int(args[0]) | |
Handler = MyHandler | |
httpd = socketserver.TCPServer(("", PORT), Handler) | |
sys.stdout.write("serving at port: %s\n" % PORT) | |
httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Simple HttpServer compatible with js framework that use custom routes.
Forked to make it compatible with both python 2 & 3