Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Python SimpleHTTPServer for Static Serving (React / Angular / Ember) in HTML5 mode (a la mod_rewrite)
Taken from:
import SimpleHTTPServer, SocketServer
import urlparse, os
PORT = 3000
INDEXFILE = 'index.html'
class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
# Parse query data to find out what was requested
parsedParams = urlparse.urlparse(self.path)
# See if the file requested exists
if os.access('.' + os.sep + parsedParams.path, os.R_OK):
# File exists, serve it up
# send index.html, but don't redirect
self.send_header('Content-Type', 'text/html')
with open(INDEXFILE, 'r') as fin:
self.copyfile(fin, self.wfile)
Handler = MyHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "serving at port", PORT
Copy link

mauronr commented Sep 5, 2018

Very handy. Thank you for sharing.

Copy link

phuongtailtranminh commented May 3, 2019

Very helpful for quick testing

Copy link

alexislefebvre commented May 29, 2019

Thanks, I added #!/usr/bin/env python on the first line to make it executable (it also requires chmod +x

Copy link

faelp22 commented Dec 30, 2019

Thanks, will you have a version for Python 3?

Copy link

faelp22 commented Dec 30, 2019

I made an adjustment for Python 3, I don't know if it's very good

Copy link

chrisbolin commented Jan 7, 2020

Glad you found it useful! Thanks for making the python3 version :)

Copy link

samkamin commented Mar 29, 2020

Just wanted to thank chrisbolin (and faelp22) for this. Gonna really help me with testing.

Copy link

chrisbolin commented Mar 29, 2020

@samkamin you are very welcome!

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