Python CGI Programming
Python CGI Programming
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 can be a python script.
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 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).
Let's understand the following example of generate the minimal header section in the
Python CGI programming.
Example -
# HTML is following
1. print("Content-Type: text/html")
2. # blank line, end of headers
3. print()
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.
CGI Architecture
The data retrieved from the database is converted in HTML format and sent to the
webserver.
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.
1. import cgi
1. import cgi
2. cgitb.enable()
Disadvantages of CGI
Consider the following disadvantages of CGI.
o CGI programs are too complex and hard to debug.
o When we initiate the program, the interpreter has to evaluate a CGI script in each
initiation. The result is, creates a lot of traffic because are many requests from the side
of the client-server.
o CGI programs are quite vulnerable, as most of them are free and easily available
without the server security.
o CGI uses a lot of process time.
o During the page load, the data doesn't store in the cache memory.
o There are huge extensive codebases, most of it Perl.