python - 使用 Traversal 制作子应用程序的最佳方式

标签 python pyramid traversal

好吧,我有我的应用程序,它接受来自根 / 的请求,几乎所有东西都使用遍历。

但我想在该网站之上制作一个休息 API。

所以我有两个选择。我要么将其分离在两个不同的应用程序中,然后将其余应用程序放置到:rest.site.com,或者我可以将其移动到 site.com/rest/*traversal

如果我正在执行“/rest/*traversal”,我想我必须添加一条名为 rest_traversal 的路线,其中遍历路径将为 *traversal与路线/rest/*traversal。我为管理页面做过一次。

我想知道是否有最干净的方法来做到这一点。我尝试使用 virtual_root,但据我了解 virtual_root 实际上已添加到路径中进行遍历。

就像拥有 virtual_root =/cms 并请求 /fun 将创建以下路径 /cms/fun

另一方面,我希望将 /cms/fun 变成 /fun

最佳答案

我知道这个问题已经得到了回答,但是如果有人来到这里寻找另一种可能的方式来制作“子应用程序”并在 Pyramid 中使用它们,我想指出一些有趣的事情可以用 pyramid.wsgi 来完成。

"""
example of wsgiapp decorator usage
http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/api/wsgi.html
"""

from pyramid.wsgi import wsgiapp2, wsgiapp
from pyramid.config import Configurator
from webob import Request, Response
import pprint

# define some apps


def wsgi_echo(environ, start_response):
    """pretty print out the environ"""
    response = Response(body=pprint.pformat({k: v for k, v in environ.items()
                                             if k not in ["wsgi.errors",
                                                          "wsgi.input",
                                                          "SCRIPT_NAME"]}))
    return response(environ, start_response)


print Request.blank("/someurl").send(wsgi_echo).body


# convert wsgi app to a pyramid view callable
pyramid_echo = wsgiapp(wsgi_echo)
pyramid_echo_2 = wsgiapp2(wsgi_echo)

# wire up a pyramid application

config = Configurator()
config.add_view(pyramid_echo, name="foo")  # /foo
config.add_view(pyramid_echo, name="bar")  # /bar
config.add_view(pyramid_echo_2, name="foo_2")  # /foo
config.add_view(pyramid_echo_2, name="bar_2")  # /bar
pyramid_app = config.make_wsgi_app()

#call some urls
foo_body = Request.blank("/foo").send(pyramid_app).body
bar_body = Request.blank("/bar").send(pyramid_app).body
foo_body_2 = Request.blank("/foo_2").send(pyramid_app).body
bar_body_2 = Request.blank("/bar_2").send(pyramid_app).body

# both should be different because we arrived at 2 different urls
assert foo_body != bar_body, "bodies should not be equal"

# should be equal because wsgiapp2 fixes stuff before calling
# application in fact there's an additional SCRIPT_NAME in the
# environment that we are filtering out
assert foo_body_2 == bar_body_2, "bodies should be equal"

# so how to pass the path along? like /foo/fuuuu should come back
# /fuuuu does it
foo_body = Request.blank("/foo_2/fuuuu").send(pyramid_app).body
assert "'/fuuuu'," in foo_body, "path didn't get passed along"


# tldr: a wsgi app that is decorated with wsgiapp2 will recieve data
# as if it was mounted at "/", any url generation it has to do should
# take into account the SCRIPT_NAME variable that may arrive in the
# environ when it is called

关于python - 使用 Traversal 制作子应用程序的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11601296/

相关文章:

python - Pandas 获得具有多个索引的时间序列的前一行

PHP - 解析 ini 文件并访问单个值

python - 从雅虎财经论坛提取回复

python - 使用 Pyramid 中的 http header 进行身份验证

python - 如何找到女服务员发出 "task queue depth"警告的原因?

c - 在 C 中使用 fts_children() 的意外结果

c# - 如何在TreeView中选择某个子节点,C#

python - 使用 boto3 对 dynamoDb 进行完整扫描

python - 我应该使用 Pylons 还是 Pyramid?

recursion - Gremlin 查询以查找特定节点以任何方式连接到的整个子图