Skip to content

Instantly share code, notes, and snippets.

@parhammmm
Created August 6, 2014 22:00
Show Gist options
  • Select an option

  • Save parhammmm/6ee6c23942a7f4e778e8 to your computer and use it in GitHub Desktop.

Select an option

Save parhammmm/6ee6c23942a7f4e778e8 to your computer and use it in GitHub Desktop.

Revisions

  1. parhammmm created this gist Aug 6, 2014.
    33 changes: 33 additions & 0 deletions pushstate-server.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    #!/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()