CGI Environment Variables in Python

PythonServer Side ProgrammingProgramming

All the CGI programs have access to the following environment variables. These variables play an important role while writing any CGI program.

Sr.No.Variable Name & Description
1CONTENT_TYPE
The data type of the content. Used when the client is sending attached content to the server. For example, file upload.
2CONTENT_LENGTH
The length of the query information. It is available only for POST requests.
3HTTP_COOKIE
Returns the set cookies in the form of key & value pair.
4HTTP_USER_AGENT
The User-Agent request-header field contains information about the user agent originating the request. It is name of the web browser.
5PATH_INFO
The path for the CGI script.
6QUERY_STRING
The URL-encoded information that is sent with GET method request.
7REMOTE_ADDR
The IP address of the remote host making the request. This is useful logging or for authentication.
8REMOTE_HOST
The fully qualified name of the host making the request. If this information is not available, then REMOTE_ADDR can be used to get IR address.
9REQUEST_METHOD
The method used to make the request. The most common methods are GET and POST.
10SCRIPT_FILENAME
The full path to the CGI script.
11SCRIPT_NAME
The name of the CGI script.
12SERVER_NAME
The server's hostname or IP Address
13SERVER_SOFTWARE
The name and version of the software the server is running.

Here is small CGI program to list out all the CGI variables.

#!/usr/bin/python
import os
print "Content-type: text/html\r\n\r\n";
print "<font size=+1>Environment</font><\br>";
for param in os.environ.keys():
   print "<b>%20s</b>: %s<\br>" % (param, os.environ[param])
raja
Published on 31-Jan-2020 12:18:09
Advertisements