Python CGI Programming

Python Programming language is broadly used by developers to create standalone, desktop, enterprise and web applications these days. Here in this article, I’m going to cover trending CGI programming in python. If you have just marked your presence in the python programming universe, then I believe you should go for Simplilearn’s Python Training accessible anytime over the internet. Here, you’ll learn about developing simple to complex python based applications with briefly described basics to advanced terms used in programming. It will also make you develop the python programming based game- flappy bird clone.

Python CGI Programming

CGI (Common Gateway Interface), is a group of standards for defining the data exchange among server and customized scripts. The CGI specifications are managed by NCSA.

What is Common Gateway Interface?

CGI is a set of standard designed for external gateway programs. It assists them to connect through the servers such as HTTP server. The latest version available is CGI 1.1 and CGI 1.2 is under development.

Web Browsing

Allow me to explain the concept of CGI in an elaborative way. Let’s check out what happens when you click on a particular hyperlink for browsing a URL:

  • The browser connects with HTTP web server by sending a request demanding for the URL, that is, filename.
  • Web server parses the URL and searches for the name of the file. If it recognise that file, sends it back to the main client such as browser. And, if not, an error message will be indicated, saying you requested a wrong file.
  • Browser collects the response from the web server. It either displays the error message or collected file to the client.

Though, it is easy to create an HTTP server so that server can directly return output to the browser made request by executing the file instead of sending file. This feature is known as Common Gateway Interface or CGI. The programs written for it are known as CGI scripts. If Python is not your primary programming language, don’t worry, you can write CGI programs in PERL, C, C++, Shell etc.

Web Server Configuration

Talking more about the CGI programming always ensure that your web server supports CGI. Also, it is capable of handling CGI scripts. A pre-configured directory will be stored in the HTTP server for running all the CGI scripts. The name of this directory is CGI directory. The convention path will be /var/www/cgi-bin. CGI files support .cgi extension. Best part is you can also keep your python .py extension files in this folder.

The Linux server executes these scripts in cgi-bin directory as /var/www. For defining any specific directory to execute your script, you can make following changes in httpd.conf file:

<Directory "/usr/var/www/cgi-bin">
AllowOverride None
Options ExecCGI +MultiViews
Require all granted
</Directory>

<Directory "/var/www/cgi-bin">
Options All
</Directory>

Now, you got a web server and you are free to execute any program written in C, Perl etc.

Writing CGI Program

Let’s start by writing a simple python script which will be pointing to a CGI script known as firstpgm.py. This file is stored in the folder /var/www/cgi-bin. Now, before execution, make sure to change permission by using Unix command chmod for execution.

Chmod 755 firstpgm.py

File:

#!/usr/bin/python

print "content-type: text/html/r/n/r/n" 
print '<html>'
print '<head>'
print '<title> Hi all- first program</title>'
print '</head>'
print '<body>'
print '<h3> Yo- successfully executed my program</h3>'
print '</body>'
print '</html>'

On execution, an output will be shown:

Yo- successfully executed my program

HTTP Header

In the above program the content-type: text/html\r\n\r\n represents a HTTP header. It is sent to the browser for recognising content.

Various HTTP-headers are: Content-type, Location: URL, Set Cookie: String, Content-Length: N, Last-Modified: Date, Expires: Date.

Environment Variables in CGI

The environment variables are accessible from programs. Few environment variables broadly used in the programming are following:

  • CONTENT_TYPE: It represents the data type of the content sent by client to the server.
  • HTTP_COOKIE: key and value pair cookie setup.
  • PATH_INFO: represents the CGI script path.
  • REMOTE_ADDR: used for authentication. It displays the IP   of the remote host.
  • REQUEST_METHOD: methods used for requests like GET and POST.
  • REMOTE_HOST: It displays the hostname.
  • SCRIPT_NAME: It displays the script name.
  • SERVER_NAME: hostname of server.
  • SERVER_SOFTWARE: name and version of software running over the server.
  • QUERY_STRING: It takes the URL information. This information is taken by GET method.

You can also check the list of environment variables by writing script:

#!/usr/bin/python

import os

print "Content-type: text/html\r\n\r\n";
print "<font size=+4>Environment</font><\br>";
for param in os.environ.keys():
   print "<b>%20s</b>: %s<\br>" % (param, os.environ[param])

Final Words

In this article, I’ve covered the key pillar of the backend programing-CGI. I hope now you’ll able to develop your application in an effective manner. Feel free to ask any question..!!

1 thought on “Python CGI Programming”

Leave a Comment

Your email address will not be published. Required fields are marked *