从 python cgi 检测 http 请求类型(GET、HEAD 等)

发布于 2024-08-04 12:53:05 字数 69 浏览 2 评论 0原文

我怎样才能找到我的python cgi收到的http请求?我需要 HEAD 和 GET 的不同行为。

谢谢!

How can I find out the http request my python cgi received? I need different behaviors for HEAD and GET.

Thanks!

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

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

发布评论

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

评论(3

一绘本一梦想 2024-08-11 12:53:05
import os

if os.environ['REQUEST_METHOD'] == 'GET':
    # blah
import os

if os.environ['REQUEST_METHOD'] == 'GET':
    # blah
韶华倾负 2024-08-11 12:53:05

为什么需要区分GET和HEAD呢?

通常您不应该区分 HEAD 请求,而应该像对待 GET 一样对待 HEAD 请求。这是因为 HEAD 请求返回与 GET 完全相同的标头。唯一的区别是不会有回复内容。仅仅因为没有响应内容并不意味着您不再需要返回有效的 Content-Length 标头或其他标头,这些标头取决于响应内容。

在mod_wsgi中,很多人都指着你,它实际上会在某些情况下故意将请求方法从HEAD更改为GET,以防止人们错误地区别对待HEAD。完成此操作的具体情况是注册 Apache 输出过滤器。在这种情况下这样做的原因是因为输出过滤器可能期望看到响应内容并从中生成额外的响应标头。如果您决定不为 HEAD 请求生成任何响应内容,您将剥夺输出过滤器的内容,并且它们添加的标头可能与 GET 请求返回的内容不一致。这样做的最终结果是你可以塞满缓存和浏览器的操作。

这同样适用于 Apache 背后的 CGI 脚本,因为在这种情况下仍然可以添加输出过滤器。对于 CGI 脚本来说,没有任何措施可以防止用户愚蠢地针对 HEAD 请求做不同的事情。

Why do you need to distinguish between GET and HEAD?

Normally you shouldn't distinguish and should treat a HEAD request just like a GET. This is because a HEAD request is meant to return the exact same headers as a GET. The only difference is there will be no response content. Just because there is no response content though doesn't mean you no longer have to return a valid Content-Length header, or other headers, which are dependent on the response content.

In mod_wsgi, which various people are pointing you at, it will actually deliberately change the request method from HEAD to GET in certain cases to guard against people who wrongly treat HEAD differently. The specific case where this is done is where an Apache output filter is registered. The reason that it is done in this case is because the output filter may expect to see the response content and from that generate additional response headers. If you were to decide not to bother to generate any response content for a HEAD request, you will deprive the output filter of the content and the headers they add may then not agree with what would be returned from a GET request. The end result of this is that you can stuff up caches and the operation of the browser.

The same can apply equally for CGI scripts behind Apache as output filters can still be added in that case as well. For CGI scripts there is nothing in place though to protect against users being stupid and doing things differently for a HEAD request.

残花月 2024-08-11 12:53:05

这不是对你的问题的直接回答。但你的问题源于做事方式错误。

不要编写 Python CGI 脚本。

编写一个 mod_wsgi 应用程序。更好的是,使用 Python Web 框架。有几十个。选择一个,例如 Werkzeug

WSGI 标准(在 PEP 333 中描述)使它变得非常非常容易在网络请求中查找内容。

mod_wsgi 实现比 CGI 更快、更安全。

Web 框架也比编写自己的 CGI 脚本或 mod_wsgi 应用程序更简单。

This is not a direct answer to your question. But your question stems from doing things the wrong way.

Do not write Python CGI scripts.

Write a mod_wsgi application. Better still, use a Python web framework. There are dozens. Choose one like Werkzeug.

The WSGI standard (described in PEP 333) makes it much, much easier to find things in the web request.

The mod_wsgi implementation is faster and more secure than a CGI.

A web framework is also simpler than writing your own CGI script or mod_wsgi application.

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