However, it is possible to set up the HTTP server so that whenever a file in a certain directory is requested that file is not sent back; instead it is executed as a program, and whatever that program outputs is sent back for your browser to display.This function is called the Common Gateway Interface or CGI and the programs are called CGI scripts.
All the CGI Programs to be executed by the HTTP server are kept in a pre-configured directory.
This directory is called CGI Directory and by convention it is named as /var/www/cgi-bin.
By convention, CGI files have extension as.cgi, but you can keep your files with python extension as well.
By default, the Linux server is configured to run only the scripts in the cgi-bin directory in /var/
If you want to specify any other directory to run your CGI scripts, comment the following lines in the file − Here, we assume that you have Web Server up and running successfully and you are able to run any other CGI program like Perl or Shell, etc.
Here is a simple link, which is linked to a CGI script called
This file is kept in /var/www/cgi-bin directory and it has following content.
Before running your CGI program, make sure you have change mode of file using chmod 755 UNIX command to make file executable.
This script is a simple Python script, which writes its output on STDOUT file, i.e., screen.
There is one important and extra feature available which is first line to be printed Content-type:text/html\r\n\r\n.
This line is sent back to the browser and it specifies the content type to be displayed on the browser screen.