Skip to content

Instantly share code, notes, and snippets.

@parhammmm
Created August 6, 2014 22:00
Show Gist options
  • Save parhammmm/6ee6c23942a7f4e778e8 to your computer and use it in GitHub Desktop.
Save parhammmm/6ee6c23942a7f4e778e8 to your computer and use it in GitHub Desktop.
A simple Python HTTP server, with support for pushState, for use with frontend frameworks such as Backbone.js
#!/usr/bin/env python
import os
import urlparse
import SimpleHTTPServer
import SocketServer
from mimetypes import guess_type
# Directory containing the static files
ROOT_DIR = 'debug/'
HOST = ('0.0.0.0', 3000)
class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
urlparts = urlparse.urlparse(self.path)
urlpath = urlparts.path.strip("/")
mime_type = 'text/html'
response_file_path = '%sindex.html' % ROOT_DIR
request_file_path = '%s%s' % (ROOT_DIR, urlpath)
if urlpath and os.path.exists(request_file_path):
mime_type, encoding = guess_type(self.path)
response_file_path = request_file_path
self.send_response(200)
self.send_header('Content-type', mime_type)
self.end_headers()
self.wfile.write(open(response_file_path).read())
httpd = SocketServer.TCPServer(HOST, Handler)
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment