Chapter 9: Python CGI Programming

As the name suggests, CGI means "Common" gateway interface for everything. CGI is one of the important parts of HTTP (Hyper-Text Transfer Protocol).

It is a set of standards that define a standard way of passing information or web-user request to an application program & to get data back to forward it to users. This is the exchange of information between web-server and a custom script. When the users requested the web-page the server sends the requested web-page. The web server usually passes the information to all application programs that process data and sends back an acknowledged message; this technique of passing data back-and-forth between server and application is the Common Gateway Interface. The current version of CGI is CGI/1.1 & CGI/1.2 is under process.

Browsing:

What happens when a user clicks a hyperlink to browse a particular web-page or URL (Uniform Resource Locator).

The steps are:

  • Browser contacts the HTTP web server for demanding the URL
  • Parsing the URL
  • Look for the filename
  • If it finds that file, a request is sent back
  • Web browser takes a response from web server
  • As the server response, it either shows the received file or an error message.

It may become possible to set-up an HTTP server because when a certain directory is requested that file is not sent back; instead it is executed as a program and that program's output is displayed back to your browser.

Configure CGI:

The steps are:

  1. Find out which user is running the Web-server
  2. Check for server configuration to see if you can run the scripts in a particular directory
  3. Check for file's permission
  4. Make a clear assurance that scripts you made are readable and executable by the web-server user
  5. Make sure the Python-Script's first line refers to web-server that the interpreter can run

The architecture of CHI is shown below:

img

The output of Python CGI script must consist of two sections separated by a blank line. The first part contains the number of headers that describe the client what kind of data is following.

Python code header section looks something like this:

print ("Content-Type : text/html")

\# then comes the rest hyper-text documents

print ("\<html\>")

print ("\<head\>")

print ("\<title\>My First CGI-Program \</title\>")

print ("\<head\>")

print ("\<body\>")

print ("\<h3\>This is HTML's Body section \</h3\>")

print ("\</body\>")

print ("\</html\>")

Save this file as CGI.py. When you will open that saved file, the output becomes:

This is HTML's Body section

Use of CGI Module:

If programmers write CGI scripts in Python, they can add these lines:

import cgitb

cgitb.enable()

The above code triggers special exception handler that will display a detailed report in the web-browser in case of occurrence of any error.

HTTP Headers:

Few are the important lists of HTTP header frequently used in CGI programs. These are:

img

CGI Environment Variables:

img

AUTHOR

READ NEXT

Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?