Python SimpleHTTPServer

前端 未结 4 593
说谎
说谎 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:28

    By modifying pd40's answer, I came up with this which doesn't redirect, it does your traditional "send index.html instead of 404". Not at all optimized, but it works for testing and development which is all I needed.

    import SimpleHTTPServer, SocketServer
    import urlparse, os
    
    PORT = 3456
    
    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:
              # send index.hmtl
              self.send_response(200)
              self.send_header('Content-Type', 'text/html')
              self.end_headers()
              with open('index.html', 'r') as fin:
                self.copyfile(fin, self.wfile)
    
    Handler = MyHandler
    
    httpd = SocketServer.TCPServer(("", PORT), Handler)
    
    print "serving at port", PORT
    httpd.serve_forever()
    

提交回复
热议问题