Advertisment

Create Interactive Websites With CGI

author-image
PCQ Bureau
New Update

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 google.com 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.

Advertisment

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

slower.

Direct Hit!

Applies To: Web developers



USP: Use CGI and FastCGI to create
interactive websites



Primary Link:
http://www.w3.org/CGI



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.

Advertisment

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/mod_cgi.so'. 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


print "This is a test"




Advertisment

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

Fedora command:

#chmod a+x .py

Now open up any web browser and type the URLhttp://

/cgi-bin/.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.

Advertisment

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:

Advertisment

# wget http://www.fastcgi.com/dist/mod_fastcgi-2.4.6.tar.gz

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

Advertisment

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:

Advertisment

builddir = .



top_dir = /usr/share/apache2


top_srcdir = ${top_dir}


top_builddir = ${top_dir}

include ${top_builddir}/build/special.mk

APXS = apxs



APACHECTL = apachectl

#DEFS=-Dmy_define=my_value



INCLUDES=-I/usr/include/apache


LIBS=-L/usr/lib/apache/


all: local-shared-build


install: install-modules-yes


clean:


-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/mod_fastcgi.so






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 '

FastCGI Environment








'



yield '

'



for k, v in sorted(environ.items()):


yield '


%s

%s

' % (escape(k), escape(v))



yield '

'



WSGIServer(app).run()

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.

Advertisment