Skip to content

Instantly share code, notes, and snippets.

@chrisbolin
Last active November 19, 2024 10:11
Show Gist options
  • Save chrisbolin/2e90bc492270802d00a6 to your computer and use it in GitHub Desktop.
Save chrisbolin/2e90bc492270802d00a6 to your computer and use it in GitHub Desktop.

Revisions

  1. chrisbolin revised this gist Jan 22, 2016. No changes.
  2. chrisbolin revised this gist Oct 20, 2014. 1 changed file with 6 additions and 5 deletions.
    11 changes: 6 additions & 5 deletions serve.py
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,14 @@
    '''
    Taken from:
    http://stackoverflow.com/users/993133/pd40
    http://stackoverflow.com/users/1074592/fakerainbrigand
    http://stackoverflow.com/questions/15401815/python-simplehttpserver
    '''

    import SimpleHTTPServer, SocketServer
    import urlparse, os

    PORT = 3000
    INDEXFILE = '/index.html'
    INDEXFILE = 'index.html'

    class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def do_GET(self):
    @@ -21,11 +21,12 @@ def do_GET(self):
    # File exists, serve it up
    SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self);
    else:
    # redirect to index.html
    self.send_response(302)
    # send index.html, but don't redirect
    self.send_response(200)
    self.send_header('Content-Type', 'text/html')
    self.send_header('location', INDEXFILE)
    self.end_headers()
    with open(INDEXFILE, 'r') as fin:
    self.copyfile(fin, self.wfile)

    Handler = MyHandler

  3. chrisbolin renamed this gist Oct 19, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. chrisbolin created this gist Oct 19, 2014.
    35 changes: 35 additions & 0 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    '''
    Taken from:
    http://stackoverflow.com/users/993133/pd40
    http://stackoverflow.com/questions/15401815/python-simplehttpserver
    '''

    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
    SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self);
    else:
    # redirect to index.html
    self.send_response(302)
    self.send_header('Content-Type', 'text/html')
    self.send_header('location', INDEXFILE)
    self.end_headers()

    Handler = MyHandler

    httpd = SocketServer.TCPServer(("", PORT), Handler)

    print "serving at port", PORT
    httpd.serve_forever()