[Welcome] [TitleIndex] [WordIndex

Problem

How do you read environment variables?

Solution

Call the get_environ() method of the HTTPRequest instance:

accept_types = self.environ.get('HTTP_ACCEPT')

accept_types will be set to a string containing the value, or None if there's no such variable. Like the get() method of dictionaries, you can supply a second value to be returned in case the key isn't found:

server_port = int(request.get_environ('SERVER_PORT', 80))

Alternatively you can access the .environ attribute of the request object, which is just a dictionary:

for k,v in request.environ.items():
    '%s: %s<br>\n' % (k,v)

Discussion

Do not use the standard Python os.environ. Its contents may coincidentally be correct if the Quixote application is being run using CGI, but they'll be wrong if the application is run with FastCGI, SCGI, or as a standalone server.

See http://hoohoo.ncsa.uiuc.edu/cgi/env.html for a list of the CGI environment variables.


CategoryCookbook


2010-09-22 22:14