Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
33 views

Python CGI Programming

Uploaded by

Shraddha Wath
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Python CGI Programming

Uploaded by

Shraddha Wath
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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

Now, we can write further script.

1. import cgi
2. cgitb.enable()

Structure of a Python CGI Program


Let's understand the following structure of the program.
o The CGI script must contain two sections which separated by a blank line.
o The header must be in the first section, and the second section will contain the kind of
data that will be used during the execution of the script.

Advantages of CGI Programming


There are various advantages of using CGI programming. Below are some of its
advantages.
o They are language independent. We can use CGI programs with any programming
languages.
o The CGI programs can work on almost any web server and the portable.
o They are portable.
o The CGI programs can perform both simple and complex tasks; means they are fairly
scalable.
o The CGIs can increase the dynamic communication in the web applications.
o The CGIs can also be profitable, if we use them in development; they reduce the
development costs and maintenance costs.
o The CGIs takes less time to process the requests.

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.

You might also like