首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python SimpleHTTPServer

Python SimpleHTTPServer
EN

Stack Overflow用户
提问于 2013-03-14 13:24:17
回答 4查看 6K关注 0票数 6

有没有办法让Python SimpleHTTPServer支持mod_rewrite?

我正在尝试使用Ember.js,并利用History API作为位置API,为了让它工作,我必须:

代码语言:javascript
复制
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 simple web服务器并没有真正读取htaccess文件(我说的对吗?)

我已经尝试下载PHP 5.4.12并运行内置的web服务器,url和htaccess mod_rewrite运行得很好。但是我仍然不愿意从稳定的5.3升级到(可能仍然不稳定) 5.4.12,所以如果有一种方法可以在python simple web服务器中支持mod_rewrite,那将是最好的。

谢谢你的回答。

EN

回答 4

Stack Overflow用户

发布于 2014-06-20 11:01:56

通过修改PD40的答案,我想出了这个不会重定向的方法,它做的是传统的“发送index.html而不是404”。不是所有的优化,但它工作的测试和开发,这是我所需要的。

代码语言:javascript
复制
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()
票数 9
EN

Stack Overflow用户

发布于 2013-03-14 13:33:08

SimpleHTTPServer不支持apache模块,也不尊重.htaccess,因为它不是apache。它也不能与php一起工作。

票数 7
EN

Stack Overflow用户

发布于 2013-03-15 09:25:05

如果您知道需要重定向的情况,您可以子类SimpleHTTPRequestHandler并执行重定向。这会将任何缺少的文件请求重定向到/index.html

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15401815

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档