by December 31, 2008 0 comments

Python can be used as CGI scripts in the development of different websites.
CGI scripts act as a standard protocol between an application and the web
server. The HTML page which is fetched by the HTTPd is static, and CGI is used
to generate dynamic content and is executed in real time. For example, you need
to search a record on a database server, so a web page like will be
provided to you. After you enter the search string, the web server deamon will
run the respective CGI program to send the information to the database server
and then receive the result and display it back to the client’s browser.

In other words, you are allowing the web server deamon to run a program on
the server. The advantage of using CGI is that it is very simple to write
programs but is recommended for low load applications. As each time a request
comes from the client a new Python interpretor is loaded, hence making things

Direct Hit!

Applies To: Web developers
USP: Use CGI and FastCGI to create
interactive websites
Primary Link:
Keywords: CGI, fastcgi

FastCGI is another concept similar to CGI but a better option than CGI. In
case of FastCGI, a new process gets started which runs behind the web server
independently. Hence whenever a request is generated, the application gets
executed without loading the interpretor as was the case with CGI. This results
in better performance.

Enabling CGI module
Before we proceed, let’s confirm if the CGI module has been loaded onto the web
server. For this, check the web server’s configuration file, and look for ‘LoadModule
cgi_module modules/’. If the line is marked as comment, then remove
the ‘#’ sign and save the configuration file.

Implementing CGI
Now let’s create a simple CGI script and test it on the web server. Write down
the following code and save it within the cgi-bin folder on the web server with
extension ‘.py’ or ‘.cgi’.

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# enable debugging
import cgitb; cgitb.enable()
print "Content-Type: text/plain;charset=utf-8"
print "This is a test"

Set the permission of the file to executable. For this execute the following
Fedora command:

#chmod a+x <filename>.py

Now open up any web browser and type the URLhttp://<address-of-the-server>/cgi-bin/<filename>.py.
This will give output on the web browser as ‘This is a test’. Incase SELinux is
running on Fedora, disable it for the time being as it can block the CGI script
from executing.

The ‘cgitb’ line helps developers to debug, as it helps in displaying well
formatted traceback instead or displaying just ‘internal server error’ or
crashing. But avoid using this line in the production environment as it can
expose some holes.

Implementing FastCGI
One of the advantages of using FastCGI is that Unlike CGI it is not restricted
to the internal archcitecture of the web server. Hence it is considered to be
stable and even if the web architecture changes, it doesn’t affect FastCGI.
Apart from performance enhancements, there are two other benefits if you
implement FastCGI: First, it allows distributed computing and second, it can do
modular authentication and translate data from one type to other.

Installing FastCGI
By default you will not find FastCGI enabled or even installed on Apache. You
have to manually get the FastCGI package and have it installed. The FastCGI
package can be downloaded by executing the following Fedora command:

# wget

Once the file is download onto your machine extract the file and change your
PWD to the newly created folder. For extracting the ‘tar.gz’ file and for
changing the PWD run the following command:

# tar -zvxf mod_fastcgi-2.4.2.tar.gz
# cd mod_fastcgi-2.4.2

Before you install it, there is a slight change that needs to be made to the
Makefile. It is assumed that on the system apache2 is installed. So first rename
the file ‘Makefile.AP2’ to ‘Makefile’ by executing the following command:

# mv Makefile.AP2 Makefile

To modify the ‘makefile,’ open the file and change the necessary
configuration that reflects your system configuration. For example, specify the
build directory and the path where apache2 is installed. Next, specify the
apache include directory and library directory and some more changes which are
shown below:

builddir = .
top_dir = /usr/share/apache2
top_srcdir = ${top_dir}
top_builddir = ${top_dir}

include ${top_builddir}/build/

APXS = apxs
APACHECTL = apachectl

all: local-shared-build
install: install-modules-yes
-rm -f *.o *.lo *.slo *.la

After the changes are done its time to install it, for installation run the
following command:

# make
# make install

Testing the application
After the installation is complete, edit the httpd.conf file so that the fastCGI
module can be loaded and used. For this open up the httpd.conf file for ‘/etc/httpd/conf/’
folder and add up these lines and restart the web server daemon:

LoadModule fastcgi_module lib/apache/
<IfModule mod_fastcgi.c>
AddHandler fastcgi-script .fcgi

Once the installation and configuration part is done you can test FastCGI by
executing the following code:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from cgi import escape
import sys, os
from flup.server.fcgi import WSGIServer
def app(environ, start_response):
start_response(‘200 OK’, [(‘Content-Type’, ‘text/html’)])
yield ‘<h1>FastCGI Environment</h1>’
yield ‘<table>’
for k, v in sorted(environ.items()):
yield ‘<tr><th>%s</th><td>%s</td></tr>’ % (escape(k), escape(v))
yield ‘</table>’

Both CGI and FastCGI are good option but it is better to opt for FastCGI as
it more stable, better in performance compared to CGI, independent of the web
server and doesn’t load interpretor every time a request is generated.

No Comments so far

Jump into a conversation

No Comments Yet!

You can be the one to start a conversation.

Your data will be safe!Your e-mail address will not be published. Also other data will not be shared with third person.