在没有 CGI 模块的情况下使用 Python 作为 CGI 脚本?

发布于 2024-08-29 11:02:26 字数 232 浏览 1 评论 0原文

是否可以在不使用 CGI 模块的情况下使用 Python 作为 CGI,并且仍然可以访问所有浏览器信息和所有内容?

我试过了:

#!/usr/bin/python
import sys
print "Content-type: text/html"
print
data = sys.stdin.readlines()
print len(data)

但总是打印0。

Is it possible to use Python as CGI without using the CGI module and still get access to all of the browser information and everything?

I tried:

#!/usr/bin/python
import sys
print "Content-type: text/html"
print
data = sys.stdin.readlines()
print len(data)

but it always prints 0.

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

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

发布评论

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

评论(2

无悔心 2024-09-05 11:02:26

这确实是可能的,但是很多信息是作为环境变量传递的,而不是在标准输入上传递的。事实上,标准输入中传递的唯一内容是传入请求的正文,只有在 POST 表单时,该正文才会包含内容。

有关如何使用 CGI 的详细信息,请查看网站,例如 http://www.w3.org/ CGI/。 (在这里的答案中解释整个标准太多了)

It is indeed possible, but a lot of the information is passed in as environment variables, not on standard input. In fact, the only thing that is passed in on standard input is the body of the incoming request, which would only have contents if a form is being POSTed.

For more information on how to work with CGI, check a website such as http://www.w3.org/CGI/. (It's too much to explain the entire standard in an answer here)

辞慾 2024-09-05 11:02:26

当然。 cgi 是一个没有魔力的实用模块;通过读取和处理环境变量(特别是 QUERY_STRING)和 stdin(它在 POST 中发挥作用),它可以做任何您自己做不到的事情形成机构。 (但请记住读取 CONTENT_LENGTH 环境变量,并且只读取那么多字节,而不是使用 readlines(),否则您的脚本可能会挂起,等待更多输入永远不会来。)

事实上,对于表单提交处理,有完整的 cgi 替代方案,可以作为独立模块,也可以作为框架的一部分。

不管有没有 cgi 模块,我今天都不会编写一个纯粹基于 CGI 的 Web 应用程序。最好写入 WSGI 接口,并使用 wsgiref.handlers.CGIHandler< /code> 通过 CGI 部署 WSGI 应用程序。然后,您可以在需要时轻松迁移到更高效的 Web 服务器界面。如果您愿意,您可以使用 WSGI 应用程序内的 cgi 模块来读取表单提交,或者再次执行您自己的操作。

Sure. cgi is a utility module with no magic powers; it does nothing you can't do yourself by reading and processing environment variables (in particular QUERY_STRING) and stdin, which comes into play for POST form bodies. (But remember to read the CONTENT_LENGTH environment variable and only read that many bytes, rather than using readlines(), else you can make your script hang waiting for more input that will never come.)

Indeed, there are complete alternatives to cgi for form submission processing, either as standalone modules or as part of a framework.

cgi module or no, however, I wouldn't write a purely CGI-based webapp today. Much better to write to the WSGI interface, and use wsgiref.handlers.CGIHandler to deploy that WSGI app over CGI. Then you can easily migrate to a more efficient web server interface as and when you need to. You can use the cgi module inside a WSGI app to read form submissions if you like, or, again, do your own thing.

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