从Python CGI脚本中读取客户端的头文件? [英] Reading a client's header from Python CGI script?

查看:156
本文介绍了从Python CGI脚本中读取客户端的头文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个非常简单的Web服务,用Python编写并在Apache服务器上作为CGI运行。

I'm writing a very simple web service, written in Python and run as CGI on an Apache server.

根据Python文档(某处...我忘了哪里),我可以使用sys.stdin来读取随机客户端发布的数据,这一切都运行正常。但是,我希望能够读取HTTP头信息 - 传入IP,用户代理等。我现在也想通过仅使用Python库(因此没有mod-python)来保持它非常简单。我该怎么做?

According to Python docs (somewhere... I forgot where), I can use sys.stdin to read the data POSTed by a random client, and this has been working fine. However, I would like to be able to read the HTTP header information as well - incoming IP, user agent, and so on. I'd also like to keep it very simple for now, by using only Python libraries (so no mod-python). How do I do this?

推荐答案

如果你是作为CGI运行的,你不能直接读取HTTP头,但是Web服务器将大部分信息放入环境变量中。您可以从os.environ []

If you are running as a CGI, you can't read the HTTP header directly, but the web server put much of that information into environment variables for you. You can just pick it out of os.environ[]

可能存在的环境变量列表很长。您可以通过Web搜索公共网关接口找到它。例如,在 http://www.ietf.org/rfc/rfc3875.txt 中它们被称为元变量。

The list of environment variables that might be there is pretty long. You can find it by doing a web search for "common gateway interface". For example, in http://www.ietf.org/rfc/rfc3875.txt they are called "meta-variables".

这篇关于从Python CGI脚本中读取客户端的头文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆