gpt4 book ai didi

python - Pyramid 调用子请求

转载 作者:太空狗 更新时间:2023-10-30 02:31:06 24 4
gpt4 key购买 nike

我正在尝试在 Pyramid 中实现批量请求方法。我在文档中看到它是用

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

我只是想知道您如何传递 header 和 cookie?是已经为你做好了还是已经做好了

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

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

我觉得文档有点含糊,或者我找不到关于如何执行此操作的正确文档。谢谢

最佳答案

我只是想知道您如何传递 header 和 cookie?

来自 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 关键字参数。

同样在请求类文档页面上,有这个

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

我必须同意你的看法,文档有些地方有点含糊,但我想你可以像这样使用它

request.method = 'POST'
# Or
request.method = 'GET'
# Etc.

总结

你会想这样做

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

注意

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

关于python - Pyramid 调用子请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23687653/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com