Python SimpleHTTPServer

前端 未结 4 599
说谎
说谎 2020-12-10 17:31

Is there a way to make Python SimpleHTTPServer supports mod_rewrite?

I\'m trying things with Ember.js with leveraging History API as the location API, and to make it

4条回答
  •  死守一世寂寞
    2020-12-10 18:12

    If you know the cases you need to redirect you can subclass SimpleHTTPRequestHandler and do a redirect. This redirects any missing file requests to /index.html

    import SimpleHTTPServer, SocketServer
    import urlparse, os
    
    PORT = 3000
    
    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', '/index.html')  
              self.end_headers()
    
    Handler = MyHandler
    
    httpd = SocketServer.TCPServer(("", PORT), Handler)
    
    print "serving at port", PORT
    httpd.serve_forever()
    

提交回复
热议问题