mod_python 发布者和漂亮的 URL

发布于 2024-07-19 08:58:29 字数 792 浏览 8 评论 0原文

我是 Python 新手(我正在退出 PHP,因为它越来越糟糕),并且我正在竞相移植我的旧代码。 一件事:

我有一个带有函数index()和bar()的文件/foo.py,因此,通过发布者我可以访问 http://domain/foo/barhttp://domain/foo 如文档所示。

我怎样才能拥有它,这样我就可以做到:

http://domain/foo/ bar/a1/a2/a3/an/...

这样发布者启动 bar() 然后我可以访问 URL 来获取 /a1/a2/... 我得到的只是 Forbidden :) (我不想在所有内容上使用 mod_rewrite)

哦,我在 2.5.2 提前致谢

更新:理想的解决方案是发布者在 URL 中启动最右边的解析,并简单地通过 apache 模块访问 a1/a2/a3..。 也许是 apache 指令和发布者的组合?

已解决(大约): 神奇的 call() 方法等的答案是多汁的! 尽管我想我会修改发布者或编写自己的发布者以使用最右匹配以类似的方式检查对象,然后允许最右使用 apache 模块访问 URL。 谢谢大家!

I am new to Python (I am getting out of PHP because of how increasingly broken it is), and I am racing through porting my old code. One thing:

I have a file /foo.py with functions index() and bar(), so, with the publisher I can access http://domain/foo/bar and http://domain/foo as the documentation suggests.

How can I have it such that I can do:

http://domain/foo/bar/a1/a2/a3/an/...

Such that the publisher launches bar() and then I can access the URL to obtain /a1/a2/...
All I get is Forbidden :) (I don't want to use mod_rewrite on everything)

Oh, im on 2.5.2
Thanks in advance

UPDATE: The ideal solution would be for the publisher to launch the furthest-right resolution in the URL it can, and simply make a1/a2/a3.. accessible through the apache module. Maybe a combination of an apache directive and publisher?

SOLVED (ish):
The answer of the magic call() method and so on is juicy! Although I think I will modify the publisher or write my own to inspect objects in a similar way using furthest-right matching, then allowing the furthest-right to access the URL using apache module. Thanks all!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三生一梦 2024-07-26 08:58:29

您必须在 foo.py 模块中定义一个对象 bar.a1.a2.a3.an。 基本上,发布者处理程序将 URL 中的斜杠替换为点,并尝试查找具有该名称的某个 Python 对象。

您可以尝试以下一些古怪的事情:在 foo.py 中:

class _barclass(object):
    def __init__(self, parent, name):
        if parent and name:
            self.path = parent.path + '/' + name
            setattr(parent, name, self)
        else:
            self.path = ''
    def __getattr__(self, name):
        return _barclass(self, name)
    def __call__(self):
        # do your processing here
        # url path is contained in self.path

bar = _barclass(None, None)

尽管这有点超出了发布者的意图 - 您最好从头开始编写自己的处理程序。 (或者使用 Django 之类的东西。)

You would have to have an object bar.a1.a2.a3.an defined within your foo.py module. Basically, the publisher handler replaces the slashes in the URL with dots, and tries to find some Python object with that name.

Here's something wacky you could try: in foo.py:

class _barclass(object):
    def __init__(self, parent, name):
        if parent and name:
            self.path = parent.path + '/' + name
            setattr(parent, name, self)
        else:
            self.path = ''
    def __getattr__(self, name):
        return _barclass(self, name)
    def __call__(self):
        # do your processing here
        # url path is contained in self.path

bar = _barclass(None, None)

Although this is kind of straining the bounds of what the publisher is meant to do - you might be better off writing your own handlers from scratch. (Or using something like Django.)

夏日浅笑〃 2024-07-26 08:58:29

据我所知,我相信这超出了发布算法的能力。 (文档当然没有不用提。)您可以编写自己的 mod_python 处理程序(示例)扩展了发布算法来做到这一点。

更好的解决方案是研究 mod_wsgi 并将您的网络应用程序构建为 WSGI 应用程序。 您将从 WSGI 中间件的架子和架子中受益,但特别是您可以使用路由软件,例如 Routes< /a>,它们是专门为处理对象发布不够强大的情况而设计的。 但我不知道你的截止日期,所以这可能可行,也可能不可行。

I believe this is beyond the capabilities of the publishing algorithm, as far as I know. (The documentation certainly doesn't mention it.) You could write your own mod_python handler (example here) that extends the publishing algorithm to do so, however.

A better solution would be to look into mod_wsgi and build your web application as an WSGI application instead. You would benefit from the shelves and shelves of WSGI middleware, but in particular you'd be able to use routing software like Routes, which are specifically designed to handle these cases where object publishing isn't strong enough. But I don't know your deadlines, so this may or may not be feasible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文