Hen*_*son 6 python simplehttpserver ember.js
有没有办法让Python SimpleHTTPServer支持mod_rewrite?
我正在尝试使用历史API作为位置API来使用Ember.js,为了使其工作,我必须:
1) add some vhosts config in WAMP (not simple), or
2) run python -m simpleHTTPServer (very simple)
Run Code Online (Sandbox Code Playgroud)
因此,当我在浏览器中打开它localhost:3000并点击导航(例如关于和用户)时,它运行良好.URL由Ember.js 分别更改为localhost:3000/about和localhost:3000/users.
但是当我尝试localhost:3000/about直接在新选项卡中打开时,python Web服务器只返回404.
我让我的.htaccess将所有内容重定向到index.html,但我怀疑python简单的web服务器并没有真正读取htaccess文件(我对吗?)
我已经尝试下载PHP 5.4.12并运行内置的Web服务器,url和htaccess mod_rewrite运行良好.但是我仍然不愿意从稳定的5.3升级到(可能仍然不稳定)5.4.12,所以如果有一种方法可以在python简单的web服务器中支持mod_rewrite,那将是首选.
谢谢你的回答.
通过修改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()
Run Code Online (Sandbox Code Playgroud)
如果您知道需要重定向的情况,则可以继承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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4836 次 |
| 最近记录: |