Python SimpleHTTPServer

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

    No mod_rewrite in Python servers I am afraid, unless you run python scripts behind an Apache server, a resource-costly solution.

    Try Cherrypy (http://www.cherrypy.org/), which allows you to manage your page handlers, and very simply makes clean URLs.

    0 讨论(0)
  • 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()
    
    0 讨论(0)
  • 2020-12-10 18:14

    SimpleHTTPServer does not support apache modules and does not respect .htaccess, because it isn't apache. it won't work with php either.

    0 讨论(0)
  • 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()
    
    0 讨论(0)
提交回复
热议问题