【发布时间】:2013-05-26 15:20:45
【问题描述】:
我目前正在尝试使用 Python Bottle 创建一个简单的独立应用程序。
我的整个项目都在pytest/ 下,其中我有dispatch.fcgi 和.htaccess。
dispatch.fcgi:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import bottle
import os
from bottle import route, run, view
@route('<foo:path>')
@view('index')
def pytest(foo = ''):
return dict(foo=foo)
APP_ROOT = os.path.abspath(os.path.dirname(__file__))
bottle.TEMPLATE_PATH.append(os.path.join(APP_ROOT, 'templates'))
app = bottle.default_app()
if __name__ == '__main__':
from flup.server.fcgi import WSGIServer
WSGIServer(app).run()
.htaccess:
DirectoryIndex dispatch.fcgi
以下网址给了我foo的对应值:
url.com/pytest/
> /pytest/
url.com/pytest/dispatch.fcgi
> /pytest/dispatch.fcgi
url.com/pytest/dispatch.fcgi/
> /
url.com/pytest/dispatch.fcgi/foo/bar
> /foo/bar
url.com/pytest/dispatch.fcgi/pytest/
> /pytest/
如何使 URL 统一?我应该使用 .htaccess 文件还是在 Python 代码中处理重新路由?什么会被认为是最 Python 或最佳实践?
我正在运行 Python 2.6.6、Bottle 0.11.6、Flup 1.0.2 和 Apache 2.2.24。我还想指出,我使用的是共享主机,而 mod_wsgi 是不可能的(如果这有影响的话)。
编辑
这是我希望看到的:
url.com/pytest/
> <redirect to url.com/pytest/dispatch.fcgi>
url.com/pytest/dispatch.fcgi
> <empty string>
url.com/pytest/dispatch.fcgi/
> /
url.com/pytest/dispatch.fcgi/foo/bar
> /foo/bar
url.com/pytest/dispatch.fcgi/pytest/
> /pytest/
如果有更有效的方法来解决这个问题,请告诉我。
【问题讨论】:
-
Phil,“使 URL 统一”是什么意思?您向我们展示了您当前代码的输出——很好——但也可以看到您/期望/看到的输出,以便更清楚您正在尝试做什么以及我们如何提供帮助。
-
@ron.rothman 已编辑以包含预期输出
标签: python apache .htaccess bottle fastcgi