Python SimpleHTTPServer

Is there a way to make Python SimpleHTTPServer support mod_rewrite?

I am trying to use Ember.js using the API History API as a location API and for it to work I have to:

1) add some vhosts config in WAMP (not simple), or
2) run python -m simpleHTTPServer (very simple)

So, when I opened it in the browser, localhost:3000and clicked the navigation (for example, about users and users), it worked well. URLs changed with Ember.js to localhost:3000/aboutand localhost:3000/usersrespectively.

But when I tried to open localhost:3000/aboutdirectly on a new tab, the python web server just returns 404.

I had my .htaccess redirecting everything to index.html, but I suspect that the simple python web server does not really read the htaccess file (am I doing this right?)

I tried downloading PHP 5.4.12 and starting the embedded web server, url and htaccess mod_rewrite work well. But I'm still reluctant to upgrade from a stable value of 5.3 to (probably still unstable) 5.4.12, so if there is a way to support mod_rewrite in a simple python web server, this would be preferable.

Thanks for the answer.

+5
source share
4 answers

SimpleHTTPServer does not support apache modules and does not respect .htaccess because it is not apache. it will also not work with php.

+8
source

, , SimpleHTTPRequestHandler . /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()
+7

pd40, , , "send index.html 404". , , , .

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()
+7

mod_rewrite Python. , python Apache, .

Cherrypy (http://www.cherrypy.org/), URL-.

+2

All Articles