有没有办法让Python SimpleHTTPServer支持mod_rewrite?
我正在尝试使用Ember.js,并利用History API作为位置API,为了让它工作,我必须:
1) add some vhosts config in WAMP (not simple), or
2) run python -m simpleHTTPServer (very simple)因此,当我在浏览器、localhost:3000中打开它并单击导航(例如关于和用户)时,它工作得很好。Ember.js将URL分别更改为localhost:3000/about和localhost:3000/users。
但是,当我尝试在新选项卡中直接打开localhost:3000/about时,python web服务器只返回404。
我让我的.htaccess将所有内容重定向到index.html,但我怀疑python simple web服务器并没有真正读取htaccess文件(我说的对吗?)
我已经尝试下载PHP 5.4.12并运行内置的web服务器,url和htaccess mod_rewrite运行得很好。但是我仍然不愿意从稳定的5.3升级到(可能仍然不稳定) 5.4.12,所以如果有一种方法可以在python simple web服务器中支持mod_rewrite,那将是最好的。
谢谢你的回答。
发布于 2014-06-20 11:01:56
通过修改PD40的答案,我想出了这个不会重定向的方法,它做的是传统的“发送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()发布于 2013-03-14 13:33:08
SimpleHTTPServer不支持apache模块,也不尊重.htaccess,因为它不是apache。它也不能与php一起工作。
发布于 2013-03-15 09:25:05
如果您知道需要重定向的情况,您可以子类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()https://stackoverflow.com/questions/15401815
复制相似问题