Python - CGI Programming
Python - CGI Programming
Python - CGI Programming
Login
Home
Coding
HTML Ground CSS Javascript SQL Python Java C C++
Jobs
Whiteboard
Python - CGI Programming
Tools
The Common Gateway Interface, or CGI, is a set of standards that define how information
is exchanged between the web server and a custom script. The CGI specs are currently
maintained by the NCSA.
What is CGI?
Web Browsing
To understand the concept of CGI, let us see what happens when we click a hyper link to
browse a particular web page or URL.
Your browser contacts the HTTP web server and demands for the URL, i.e.,
filename.
Web Server parses the URL and looks for the filename. If it finds that file then
sends it back to the browser, otherwise sends an error message indicating that you
requested a wrong file.
Web browser takes response from web server and displays either the received file
or error message.
However, it is possible to set up the HTTP server so that whenever a file in a certain
directory is requested that file is not sent back; instead it is executed as a program, and
whatever that program outputs is sent back for your browser to display. This function is
called the Common Gateway Interface or CGI and the programs are called CGI scripts.
These CGI programs can be a Python Script, PERL Script, Shell Script, C or C++ program,
etc.
<Directory "/var/www/cgi-bin">
AllowOverride None
Options ExecCGI
Order allow,deny
Allow from all
</Directory>
<Directory "/var/www/cgi-bin">
Options All
</Directory>
The following line should also be added for apache server to treat .py file as cgi script.
Here, we assume that you have Web Server up and running successfully and you are able
to run any other CGI program like Perl or Shell, etc.
print ("Content-type:text/html\r\n\r\n")
print ('<html>')
print ('<head>')
print ('<title>Hello Word - First CGI Program</title>')
print ('</head>')
print ('<body>')
print ('<h2>Hello Word! This is my first CGI program</h2>')
print ('</body>')
print ('</html>')
Note − First line in the script must be the path to Python executable. It appears as a
comment in Python program, but it is called shebang line.
http://localhost/cgi-bin/hello.py
This hello.py script is a simple Python script, which writes its output on STDOUT file, i.e.,
screen. There is one important and extra feature available which is first line to be printed
Content-type:text/html\r\n\r\n. This line is sent back to the browser and it specifies the
content type to be displayed on the browser screen.
By now you must have understood basic concept of CGI and you can write many
complicated CGI programs using Python. This script can interact with any other external
system also to exchange information such as RDBMS.
HTTP Header
The line Content-type:text/html\r\n\r\n is part of HTTP header which is sent to the
browser to understand the content. All the HTTP header will be in the following form −
For Example
Content-type: text/html\r\n\r\n
There are few other important HTTP headers, which you will use frequently in your CGI
Programming.