调用子需求的金字塔

2024-06-10 11:58:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试在金字塔中实现批处理请求方法。我在文件里看到了

subrequest = Request.blank('/relative/url')
request.invoke_subrequest(subrequest)

我只是想知道你怎么传递标题和饼干?是不是已经为你做了

^{pr2}$

不同方法的参数呢?文档只有一个POST关键字参数。在

我觉得文档有点模糊,或者我找不到正确的文档来说明如何做到这一点。谢谢


Tags: 文件方法文档url标题参数requestpost
3条回答

下面的代码对我有用。它复制所有(头、cookies、查询字符串、post参数等):

def subrequest(request, path):
    subreq = request.copy()
    subreq.path_info = path
    response = request.invoke_subrequest(subreq)
    return response

有点晚,但基于以上两个答案,我是如何做到这一点的。我不太喜欢above answer只复制所有内容。查看blank() method的文档,有一个kw参数,它说

All necessary keys will be added to the environ, but the values you pass in will take precedence. If you pass in base_url then wsgi.url_scheme, HTTP_HOST, and SCRIPT_NAME will be filled in from that value.

假设视图的请求包含子请求所需的正确标头信息和Cookie,则可以使用以下代码:

@view_config( ... )
def something(request):
    ...
    kwargs = { "cookies": request.cookies,                                  
               "host"   : request.host,                                     
             }                                                              
    req = Request.blank("/relative/url", **kwargs)
    resp = request.invoke_subrequest(req)                                   

其他头信息(例如acceptaccept_encoding等)是pyramid.request对象的属性,可以像上面的代码片段一样添加到kwargs字典中。在

invoke_subrequest()返回的对象是一个记录了here的响应对象。在

我只是想知道你怎么传递标题和饼干?

来自http://docs.pylonsproject.org/projects/pyramid/en/1.5-branch/narr/subrequest.html#subrequest-chapter

The pyramid.request.Request.invoke_subrequest() API accepts two arguments: a positional argument request that must be provided, and use_tweens keyword argument that is optional; it defaults to False.

这告诉我们构造函数只需要一个Request对象,以及可选的use_tweens的值,所以不,这个

request.invoke_subrequest(subrequest, cookies=request.cookies, headers=request.headers)

不起作用。在

然后,从http://docs.pylonsproject.org/projects/pyramid/en/1.5-branch/narr/subrequest.html

It's a poor idea to use the original request object as an argument to invoke_subrequest(). You should construct a new request instead as demonstrated in the above example, using pyramid.request.Request.blank(). Once you've constructed a request object, you'll need to massage it to match the view callable you'd like to be executed during the subrequest. This can be done by adjusting the subrequest's URL, its headers, its request method, and other attributes. The documentation for pyramid.request.Request exposes the methods you should call and attributes you should set on the request you create to massage it into something that will actually match the view you'd like to call via a subrequest.

因此,基本上,您需要在将请求传递给invoke_subrequest()之前对其进行配置。在

幸运的是有an entire page that documents the Request class。在那里我们可以找到很多配置它的选项,等等


不同方法的参数如何?文档只有一个POST关键字arg。

在Request class documentation页面上也有

method

Gets and sets the REQUEST_METHOD key in the environment.

http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/viewconfig.html上我发现

request_method This value can be a string (typically "GET", "POST", "PUT", "DELETE", or "HEAD") representing an HTTP REQUEST_METHOD

我必须同意你的观点,文件有些模糊,但我想你可以这样使用它

^{pr2}$

摘要

你会想这样做的

subrequest = Request.blank('/relative/url')
# Configure the subrequest object
# set headers and other options you need.
request.invoke_subrequest(subrequest)

注意

我知道这不是一个100%完整的答案,有些代码,你可以复制粘贴,它将只是工作(特别是关于配置请求对象),但我认为这个答案包含一些信息,至少,会让你在正确的轨道上,我希望它会对你有所帮助。在

相关问题 更多 >