Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a client's header from Python CGI script?

Tags:

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

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?

like image 274
jbreed Avatar asked Apr 20 '10 17:04

jbreed


People also ask

Which of the given is an HTTP header frequently used in CGI programming?

HTTP Header There are few other important HTTP headers, which we will use frequently in our CGI Programming. Sr.No. The date the information becomes invalid. It is used by the browser to decide when a page needs to be refreshed.

Which method is used to fetch the string value directly using Python CGI?

We can use the getvalue() method to fetch the string value directly.

What does CGI FieldStorage do?

The FieldStorage instance can be indexed like a Python dictionary. It allows membership testing with the in operator, and also supports the standard dictionary method keys() and the built-in function len() .


1 Answers

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[]

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".

like image 86
sienkiew Avatar answered Sep 24 '22 13:09

sienkiew