Python CGI Programming
In the Python CGI programming, we will learn how we can run the Python Script on the web; we will learn how Python file can be executed as CGI Script and discuss the its configuration of web-browser to Python script run as a CGI. Also, we will learn the following topics:
What is CGI?
The word CGI is the acronyms of the "Common Gateway Interface", which is used to define how to exchange information between the web server and a custom script. The NCSA officially manages the CGI scripts.
The Common Gateway Interface is a standard for external gateway programs to interface with the server, such as HTTP Servers.
In simple words, it is the collection of methods used to set up a dynamic interaction between the server, and the client application. When a client sends a request to the webserver, the CGI programs execute that particular request and send back the result to the webserver.
The users may submit the information in web browser by using HTML <form> or <isindex> element. There is a server's special directory called cgi-bin, where cgi script is generally stored. When a client makes a request, the server adds the additional information to request.
This additional information can be the hostname of the client, the query string, the requested URL, and so on. The webserver executes and sends the output to the web browser (or other client application).
Sometimes the server passes the data by a query string that is the part of the URL. A query string may not be conventionally a hierarchical path structure such as the following link.
So here the question arises what does a Python CGI script output look like? The HTTPs server gives back the output as two sections separated by a blank line. The first section grasps the number of headers, notifying the client what kind of data is following.
Let's understand the following example of generate the minimal header section in the Python CGI programming.
In the above example, the first statement says that, the server that html code follows; the second blank line indicates the header is ended here. Let's understand the following example of generating the minimal header section in the Python CGI programming.
Before understanding the CGI concepts, we should know the internal process of web page or URL when we click on the given link.
However, we can set a HTTP server so that whenever 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.
Configure Apache Web server for CGI
We need to configure the Apache Web server in order to run the CGI script on the server.
Using the cgi module
Python provides the cgi module, which consists of many useful built-in functions. We can use them by importing the cgi module.
Now, we can write further script.
The above script will stimulate an exception handler to show the detailed report in the web browser of occurred errors. We can also save the report by using the following script.
The above feature of the cgi module is helpful during the script development. These reports help us to debug the script effectively. When we get the expected output, we can remove this.
Previously, we have discussed the users save information using the form. So how can we get that information? Python provides the FieldStorage class. We can apply the encoding keyword parameter to the document if the form contains the non-ASCII character. We will find the content <META> tag in the <HEAD> section in our HTML document.
The FieldStorage class reads the form information from the standard input or the environment.
A FieldStorage instance is the same as the Python dictionary. We can use the len() and all dictionary function in the FieldStorage instance. It overlooks fields with empty string values. We can also consider the empty values using the optional keyword parameter keep_blank_values by setting True.
In the above example, we have used the form ["name"], here name is key. This is used to extract the value which is entered by the user.
We can use the getvalue() method to fetch the string value directly. This function also takes an optional second argument as a default. If the key is not present, it returns the default value.
If the submitted form data have more than one field with the same name, we should use the form.getlist() function. It returns the list of strings. Look at the following code, we add the any number of username field, which is separated by commas.
If the field is uploaded file, then it can be accessed by value attribute or the getvalue() method and read that uploaded file in bytes. Let's understand the following code if user upload the file.
Sometimes an error can interrupt the program while reading the content of the uploaded file (When a user clicks on Cancel Button or Back Button). FieldStorage class provides the done attribute to set to the value -1.
If we submit the form in the "old" format, the item will be instances of the class MiniFieldStorage. In this class, the list, file, and filename attributes are always None.
Generally, the form is submitted via POST and contains a query string with both FieldStorage and MiniStorage items.
Here, we are defining the FieldStorage attribute in the following table.
The FieldStorage instance uses the many built-in methods to manipulate the users' data. Below are a few FieldStorage's methods.
Running First Python File as CGI
In this section, we will discuss how can run the CGI program over the server. Before doing this, we must ensure that our system has the following configuration-
If you already have the XAMPP server in your system then you can skip this part.
Installing the XAMPP server
XAMPP stands for cross-platform, Apache, MySQL, PHP, and Perl, and it provides the localhost server to test or deploy websites. Generally, it gives the two essential components for its installation, first is - Apache that creates a local server and MySQL, which we can use a database.
Follow the below steps to install xampp.
Step - 1: Visit its official website (https://www.apachefriends.org/download.html) and download the latest version.
Step - 2: Once the download is complete, click on the run button.
Step - 3: Now, Click on the Next Button.
Step - 4: Next, it will display the xampp components. We can remove some of these but we will install all components of xampp, it won't affect our application.
Step - 6: Now, our set up is ready to install, start the installation by clicking the Next button.
It will take away on the webserver and start installing all packages and files.
Step - 7: Here, the download has completed. Select your desire language, and then, it will display the following dashboard of xampp.
To install the Python, visit out Python installation guide (https://www.javatpoint.com/how-to-install-python).
First CGI Program
We have created a new folder called example in xampp's htdocs folder. Then, we write a Python script, which includes the HTML tags. Let's see the following directory structure and the demo.py file.
And, its directory structure as follow.
Type the localhost/example/demo.py into the web browser. It will display the following output.
Note - We need to start the Apache server then execute the CGI script. Our script demo.py will run on host 127.0.0.1 by default.
Let's understand another example of CGI script.
Example - 2:
Structure of a Python CGI Program
Let's understand the following structure of the program.
Legend of Syntaxes
When scripting a CGI program in Python, take note of the following commonly used syntaxes.
In the above program, the line Content-type:text/html\r\n\r\n is a portion of the HTTP, which we will use in our CGI programs.
CGI Environment Variables
We should remember the following CGI environment variable along with the HTML syntax. Let's understand the commonly used CGI environment variables.
Functions of Python CGI Programming
The CGI module provides the many functions to work with the cgi. We are defining a few important functions as follows.
Debugging CGI Scripts
First, we need to check the trivial installation error. Most of the time, the error occurs during the installation of the CGI script. In the beginning, we must follow the installation instructions and try installing a copy of this module file cgi.py as a CGI script.
Next, we can use test() function from the script. Type the following code with a single statement.
Advantages of CGI Programming
There are various advantages of using CGI programming. Below are some of its advantages.
Disadvantages of CGI
Consider the following disadvantages of CGI.
Common Problems and Solutions
We can face problems during the implement the CGI script on the server. We have listed below the few common problems and their solutions.