Introduction to CGI in Python
CGI means Common Gateway Interface, which is used to exchange information between a web server and a random script. It is a set of instructions known as a protocol that defines a particular way of passing information or responding to the web-user requests by sending the information to the application and getting it back again and sending them to users again. When a user request a web page the web server sends the requested webpage back to the user to the application. The web server sends data to many applications that can process and send back data. This method of passing information back and forth between a web server and an application is known as the Common Gateway Interface.
To understand these better, let us look at the steps of how this process works
Step 1: You click on a hyperlink to navigate to a particular web page.
Step 2: The browser which is connected to an HTTP web server contacts it and requests for the URL or the extended URL which is a filename.
Step 3: Webserver accepts the request and looks for the requested URL.
Step 4: If the requested file is found by the server, it sends that file back along with the request, Otherwise, it sends an error message which indicates that the file is not found, and you are indicating a wrong file name
Step 5: The web browser receives the response from the webserver and displays it whether it may be a file name or an error message.
It is sometimes possible to set up an HTTP server because when the web server doesn’t find the requested file requested by the web browser, instead of sending a simple error message, it executes a program, and that program’s output is sent back and displayed in the web browser.
Web Server Configuration
Before you set up CGI programming, you have to ensure that your web server supports CGI programming and can handle CGI programs. The CGI programs which are to be executed by the webserver upon request by the web browser must be kept in a pre-configured directory. This directory in the webserver is known as the CGI directory or it is also named as “/var/www/cgi-bin”. In general, CGI files have a cgi extension to them but you can store your files with python extension .py as well.
Program Sturcture of Python CGI
Instead of a cgi extension, let us take python extension .py as an example
The python CGI scripts contain two sections separated by a blank space. The first part describes the number of headers that tell the client what type of data the server sends as a response and the second part contains the other hypertext documents that are to be sent as a response to the browser.
The code looks something like this
print ("Content-Type : text/html")
# Hyper-text documents
print ("<title>My First CGI-Program </title>")
print ("<h3>Hello World!</h3>")
The CGI.py is a simple python script that displays its output in the output console. Apart from taking input and displaying the output, the first line present in the code specifies the content type to be displayed on the screen. Only the type of content which is specified in the first line is printed. You can write many such CGI programs in python by specifying the exact directory. While writing the script in python, you can also add some extra code to your existing code as shown below
When you add this code, If there are any exceptions, it perfectly handles all of them and displays a detailed output in the browser if there is an error.
In the above code, we have used text/html content type, but there are many options as shown below to decide the other data types as well.
|Sr.no||HTTP Headers with its values and description|
|1||Content-Type : Text/html
It is the data type of the content that is sent
|2||Expires : Date
It is used by the web browser when it reloads the page.
|3||Location : URL
It is used to redirect a request that is sent to any file
It sets the cookies for the web browser that are passed in string form
|5||Content-Length : N
It decides the length of the content
GET and POST methods
Many times you have come across situations where you have to pass information from the web browser to the webserver to store it.
To pass any information to the servers through browsers, we initially use forms to collect any type of data from the user and then pass it to the servers then to the CGI program. There are two methods to pass information through forms
The GET Method
The GET method takes the input, encodes it then appends it to the page’s request. The encoded information is present in the page url after ?.
Whenever you pass information from the browser to the server, the default method to pass this information is the GET method. If you have any sensitive information like passwords you should not use the GET method to pass that information.
The POST Method
The most convenient and secure method to pass information is through the POST method. It sends information in the same way as the GET method but instead of passing it as a string after ? it passes it as a separate message which is received by the CGI script.
Conclusion – CGI in Python
- CGI means Common Gateway Interface, which is used to exchange information between a web server and a random script.
- The method of passing information back and forth between a web server and an application is known as the Common Gateway Interface.
- Before you set up CGI programming, you have to ensure that your web server supports CGI programming or not.
- You can pass data to the servers and into the CGI program through GET and POST methods.
This is a guide to CGI in Python. Here we discuss the steps of how the process of CGI works in Python in detail to understand. You may also have a look at the following articles to learn more