Python CGI Programming

Python CGI Programming is a Common Gateway Interface that has specifications (set of rules) that helps to establish a dynamic interaction between a web application and the browser (or the client application). The CGI programs make possible communication between clients and web servers. Whenever the client browser sends a request to the webserver the CGI programs send the output back to the web server based on the input provided by the client-server.

  • Python CGI is the standard for programs to interface with HTTP servers.
  • Python CGI programming is written dynamically generating webpages that respond to user input or webpages that interact with software on the server.

Web Browsing:

Before understanding the CGI concepts, we should know the internal process of the web page or URL when we click on the given link.

  • Your browser contacts the HTTP web server and demands for the URL, i.e., filename.
  • Web Server parses the URL and looks for the filename. If it finds that file then sends it back to the browser, otherwise sends an error message indicating that you requested the wrong file.
  • Web browser takes the response from a web server and displays either the received file or error message.

However, the HTTP server can be used that whenever a user requests in a particular dictionary, then it should be sent to the client; instead, it executed as a program, and whatever the result is sent back for the client to display. This process is called the Common Gateway Interface or CGI and the programs are called CGI scripts. We can write the CGI programs as Python Script, PERL, Script, Shell Script, C or C++, programs, etc.

CGI Architecture Diagram:

cgiarch 1

Web Server Support and Configuration:

Sometimes recently you continue with CGI Programming, make beyond any doubt that your Web Server supports CGI and it is designed to handle CGI Programs. All the CGI Programs to be executed by the HTTP server are kept in a pre-configured registry. This directory is called CGI Directory and by tradition, it is named as /var/www/cgi-bin. By tradition, CGI files have extensions. CGI, but you’ll be able to keep your records with python extension .py as well.

By default, the Linux server is configured to run only the scripts in the cgi-bin directory in /var/www. If any other directory is specified to run CGI scripts, comment the following lines given below in the httpd.conf file:

#web server configuration

<Directory "/usr/var/www/cgi-bin">
AllowOverride None
Options ExecCGI +MultiViews
Require all granted
<Directory "/var/www/cgi-bin">
Options All

First CGI Program:

Let’s start by writing a simple python script which will be pointing to a CGI script known as This file is stored in the folder /var/www/cgi-bin. Now, before execution, make sure to change permission by using Unix command chmod for execution.

#first CGI program
print "content-type: text/html/r/n/r/n" 
print '<html>'
print '<head>'
print '<title> Hello- python program</title>'
print '</head>'
print '<body>'
print '<h3> Yes! successfully run Python Program.</h3>'
print '</body>'
print '</html>'

HTTP Header:

The line Content-type:text/html\r\n\r\n is part of HTTP header which is sent to the browser to understand the content. All the HTTP header will be in the following form:

#http header

HTTP Field Name: Field Content

Content-type: text/html\r\n\r\n

There are few other important HTTP headers use frequently in your CGI Programming.

Content-typeA MIME string defining the format of the file being returned.
Example is Content-type:text/html
Location: URLThe URL that is returned instead of the URL requested.
You can use this field to redirect a request to any file.
Expires: DateThe date the information becomes invalid. It is used by the
browser to decide when a page needs to be refreshed.
A valid date string is in the format 01 Jan 1998 12:00:00 GMT.
Last-modified: DateThe date of the last modification of the resource.
Set-Cookie: StringSet the cookie passed through the string
Content-length: NThe length, in bytes, of the data being returned.
The browser uses this value to report the estimated
download time for a file.

CGI Environment Variables:

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

Variable NameDescription
CONTENT_TYPEThe data type of the content. Used when the client is
sending attached content to the server. For example, file upload.
CONTENT_LENGTHThe length of the query information.
It is available only for POST requests.
Returns the set cookies in the form of key & value pair.
The User-Agent request-header field contains information
about the user agent originating the request.
It is the name of the web browser.
The full path to the CGI script.
PATH_INFOThe path for the CGI script.
REQUEST_METHODThe method used to make the request.
The most common methods are GET and POST.
SERVER_NAMEThe server’s hostname or IP Address.
SERVER_SOFTWAREThe name and version of the software the server is running.
REMOTE_HOSTThe fully qualified name of the host making the request.
If this information is not available, then REMOTE_ADDR
can be used to get the IR address.
QUERY_STRINGThe URL-encoded information is sent with a GET method request.
REMOTE_ADDRThe IP address of the remote host making the request.
This is useful for logging or for authentication.