在没有 CGI 模块的情况下使用 Python 作为 CGI 脚本?
是否可以在不使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这确实是可能的,但是很多信息是作为环境变量传递的,而不是在标准输入上传递的。事实上,标准输入中传递的唯一内容是传入请求的正文,只有在 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)
当然。
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 particularQUERY_STRING
) andstdin
, which comes into play forPOST
form bodies. (But remember to read theCONTENT_LENGTH
environment variable and only read that many bytes, rather than usingreadlines()
, 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 usewsgiref.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 thecgi
module inside a WSGI app to read form submissions if you like, or, again, do your own thing.