CGI is an abbreviation for Common Gateway Interface. It is not a type of language but a set of rules (specification) that establishes a dynamic interaction between a web application and the client application (or the browser).

The programs based on CGI helps in communicating between the web servers and the client. Whenever the client browser makes a request, it sends it to the webserver, and the CGI programs return output to the webserver based on the input that the client-server provides.
Common Gateway Interface (CGI) provides a standard for peripheral gateway programs to interface with the data servers like an HTTP server.
The programming with CGI is written dynamically, which generates web-pages responding to the input from the user or the web-pages interacting with the software on the server.
Have you ever wondered how these blue-colored underlined texts, commonly known as hyperlinks, able to take you from one web-page or Uniform Resource Locator (URL) to another? What exactly happens when some user clicks on a hyperlink?
Let’s understand the very concept behind web browsing. Web browsing consists of some steps that are as follows:
STEP 1: Firstly, the browser communicates with the data server, say HTTP server, to demand the URL.
STEP 2: Once it is done, then it parses the URL.
STEP 3: After then, it checks for the specified filename.
STEP 4: Once it finds that file, a request is made and sent it back.
STEP 5: The Web browser accepts a response from the webserver.
STEP 6: As the server’s response, it can either display the requested file or a message showing error.
However, it may be possible to set up an HTTP server because whenever a file in a specific directory is requested, it is processed as a program rather than sending that file back. The output of that program is shown back to the browser. This function is also known as the Common Gateway Interface or abbreviated as CGI. These processed programs are known as CGI scripts, and they can be a C or C++ program, Shell Script, PERL Script, Python Script, etc.
Whenever the client-server requests the webserver, the Common Gateway Interface (CGI) handles these requests using external script files. These script files can be written in any language, but the chief idea is to recover the data more efficiently and quickly. These scripts are then used to convert the recovered data into an HTML format that can send data to these web servers in HTML formatted page.
An architectural diagram representing the working of CGI is shown below:
Python provides the cgi module consisting of numerous useful core properties. These properties and functions can be used by importing the cgi module, in current working program as shown below:
Now, We will use cgitb.enable() in our script to stimulate an exception handler in the web browser to display the detailed report for the errors that occurred. The save will look as shown below:
cgitb.enable()
Now, we can save the report with the help of the following script.
The function of the cgi module stated above would help throughout the script development. These reports help the user for debugging the script efficiently. Whenever the users get the expected result, they can eliminate this.
As we have discussed earlier, we can save information with the help of the form. But the problem is, how can we obtain that information? To answer this question, let’s understand the FieldStorage class of Python. If the form contains the non-ASCII characters, we can apply the encoding keyword parameter to the document. We will find the content <META> tag inside the <HEAD> section of the HTML file.
The FieldStorage class is used to read the form data from the environment or the standard input.
A FieldStorage instance is similar to the Python dictionary. The user can utilize the len() and all the dictionary functions as the FieldStorage instance. It is used to overlook the fields that have values as an empty string. The users can also consider the void values with the optional keyword parameter keep_blank_values by setting it to True.
Let’s see an example:
In the above snippet of code, we have utilized the form [“name”], where name is key, for extracting the value which the user enters.
To promptly fetch the string value, we can utilize the getvalue() method. This method also takes a second argument by default. And if the key is not available, it will return the value as default.
Moreover, if the information in the submitted form has multiple fields with the same name, we should take the help of the form.getlist() function. This function helps in returning the list of strings. Now let’s look at the following snippet of code; we have added some username fields and separate them by commas.
If we want to access the field where a file is uploaded and read that in bytes, we can use the value attribute or the getvalue() method. Let’s see the following snippet of code if the user uploads the file.
An error can often interrupt the program while reading the content of the file that was uploaded. It may happen when a user clicks on the Back Button or the Cancel Button. However, to set the value – 1, the FieldStorage class provides the done attribute.
Furthermore, the item will be objects of the MiniFieldStorage class if the submitted form is in the “old” format. The attributes like list, filename, and file are always None in this class.
Usually, the form is submitted with the POST method’s help and contains a query string with both the MiniFieldStorage and FieldStorage items.
Let’s see a list of the FieldStorage attribute in the following table.
FieldStorage Attributes:
In addition to the above, the FieldStorage instance uses various core methods for manipulating users’ data. Some of them are listed below:
FieldStorage Methods:

Let’s understand the structure of a Python CGI Program:
Let’s have a look at the Python code given below:
Now, let’s save the above file as cgi.py. Once we execute the file, we should see an output, as shown below:
Hello World! This is my first CGI program.
The above program is a simple Python script that writes the output to STDOUT file that is on-screen.
There are various HTTP headers defined that are frequently used in the CGI programs. Some of them are listed below:
There are some variables predefined in the CGI environment alongside the HTML syntax. Some of them are listed in the following table:
Before we start debugging CGI Scripts, we need to check the error in trivial installation. It often occurs while installing the CGI script. It is recommended to follow the installation instructions and install a replica of the module file – cgi.py as a CGI script.
Then, the test() function can be used from the script. We can write the following code using a single statement
Some Pros of CGI Programming:
There are numerous pros of using CGI programming. Some of them are as follows:
There are a few cons of using CGI programming. Some of them are as follows:

source