Python SimpleHTTPServer

6

有没有一种方法可以让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/aboutlocalhost: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,那将是首选。

感谢您的答复。

4个回答

9
通过修改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()

1
Python3 的版本是多少? - AwokeKnowing

7

SimpleHTTPServer 不支持 apache 模块,也不遵守 .htaccess 规则,因为它不是 apache。它也不能与 php 一起使用。


1
事实上,“SimpleHTTPServer”没有比提供静态文件和HTML更多的功能。 - mvanveen
是的,我也这么认为。不管怎样,有没有推荐的简单易用且至少支持mod_rewrite的网页服务器? - Henson
1
如果支持所有这些东西,它就不会是一个“简单”的Web服务器。 - LtWorf
它可以自由扩展。你可以使其支持几乎任何东西。 - XTL
我成功地使用SimpleHTTPServer来服务于一个包含链接JavaScript库的HTML文件,但前提是这个库文件和HTML文件都存储在同一个目录下。 - noobninja

7

如果您知道需要重定向的情况,可以子类化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()

2
很抱歉,在Python服务器中没有mod_rewrite,除非您在Apache服务器后运行Python脚本,这是一个资源成本高昂的解决方案。
尝试使用Cherrypy(http://www.cherrypy.org/),它允许您管理页面处理程序,并且可以轻松地创建干净的URL。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接