Python 简单 HTTP 服务器

标签 python ember.js simplehttpserver

有没有办法让 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 网络服务器只返回 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()

关于Python 简单 HTTP 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15401815/

相关文章:

python - 通过SimpleHTTPServer调用python脚本将数据插入MYSQL数据库

python - Pandas 滚动 : How to roll from left to right or from up to down?

python - 我应该如何安装和导入 simpletransformers?

ember.js - 使用 Ember 保存不成功时显示错误

mongodb - ember.js RestAdapter 如何定义ajaxoptions

Python SimpleHTTPServer 提供一个子目录

python - 在 Windows 上执行 pip installpsychopy 时出现错误消息

python - Django Model 类中的函数不接受 self 参数?

ember.js - 接受服务器更新时保存记录

python-3.x - 从 Python 的 http.server 提供文件 - 使用文件正确响应