CGI Programming in Python

FREE Online Courses: Transform Your Career – Enroll for Free!

We use the browser almost every day. Do you know that CGI acts as an interface between your browser and the server?
This article will learn about CGI and use the cgi module for CGI scripting in Python. So, let us start with the introduction to CGI.

Introduction to CGI

The Common Gateway Interface (CGI) is the set of standard rules that helps in the exchange of information between the scripts in the web applications and the server. It helps in establishing connections between the client and web server.

1. When a client sends a request to the server, the CGI sends the output back to the server-based

2. It is a standard interface for the programs connecting to the HTTP server

3. It is dynamic and the web pages it generates can respond to the user’s inputs and interact with the server.

Role of CGI as an Interface

First, let us see what happened when you browse something or click a hyperlink:

1. The web browser contacts the HTTP web server and asks for the URL, i.e., name of the file in which the content of that website is stored in the server.

2. Web Server parses the URL and searches for the filename.It sends the file back to the browser, if found, else sends an error message showing that wrong file is requested.

3. The browser takes response from the server and displays result as a webpage or an error message.

Now let us see where CGI comes in this process.

We can modify this process by making the file from the database get executed as a program, rather than being sent to the browser the server to be displayed. This is called CGI and the programs that execute are called CGI scripts. The scripts can be in any language including Python Script,C, C++, PERL Script, Shell Script, etc.

These convert the data in the file into an Html format and send it back to the web servers in form of an HTML page. These scripts give two outputs separated by a blank line. The first one is the headers that tell about the kind of data to be displayed to the client. The objective of these scripts The other output is the HTML. The main goal of these scripts is to retrieve the data from the server/database in a faster and efficient method.

CGI module in Python

Python provides a module named CGI for doing CGI programming. We can import this module by writing the below statement.

import cgi

There is a function called enable() that helps in handling and reporting the error if any and also storing them in a file to debug the script. We can write this function as follows.

Example of enable() function:

cgitb.enable()

Example of storing the error report in a file using enable():

cgitb.enable(display=0, logdir="path")

The next step is to call the FieldStorage class. It reads content from the standard input. Because there is a chance of it absorbing the input, it should be instantiated only once. Another thing to note while instantiation is that it ignores fields with empty strings. To avoid this, we can set the optional parameter keep_blank_values as True while creating the instance of FieldStorage.

The instance of this class can be used as a dictionary and we can perform operations on it as we do it on a dictionary. We can

1. use the methods like keys() and len()

2. use membership operators to check if the fields are set to non empty strings. For example,

inst= cgi.FieldStorage()

if "name" not in inst:
  print("<H1>Error!</H1>")
  print("Please fill in the name field.")
  return
print("<p>name:", inst["name"].value)

In this code, the inst[‘key’] is used to access instances of the FieldStorge class. If more than one of the fields has the same name, then this outputs a list of instances of the FieldStorage.

3. use getlist() method to get list of values. For example,

vals = inst.getlist("username")
usernames=’’
for i in vals:
usernames = usernames +i

4. When the field denotes a file uploaded, we can access it using the attribute ‘value’, or the method getvalue(). And to test for the file, we can test either the ‘filename’ or the ‘file’ attribute. After this, we can read the data from the ‘file’ attribute. This should be done before the file automatically closes being a part of the garbage collection of the FieldStorage instance. We can do this by

file_item = inst["file_name"]
if file_item .file:
    count = 0
while True:
    l = file_item .file.readline() 
    if not l: break
    count = count + 1

We can use a FieldStorage object in a with-statement. It automatically closes the file when done. If any error occurs while uploading the file’s contents like if a user presses the Back or Cancel button, it sets the ‘done’ attribute for the object for the field to -1.

We can upload multiple files by using the item as dictionary-like FieldStorage item.

Functions in CGI Module

The CGI module has the below functions to do more programming :

1. cgi.parse(fp=None, environ=os.environ, keep_blank_values=False, strict_parsing=False)

This function will parse a query in the environment or from a file, with the default for value of sys.stdin.

2. cgi.parse_qs(qs, keep_blank_values=False, strict_parsing=False)

This is use for backwards-compatibility. Instead, we can als use urllib.parse.parse_qs().

3. cgi.parse_qsl(qs, keep_blank_values=False, strict_parsing=False)

This function is deprecated too, and is used for backwards-compatibility. An alternative is urllib.parse.parse_qsl().

4. cgi.parse_multipart(fp,pdict)

This function will parse the input (multipart/form-data) for file uploads. The first argument fp is the input file, and the second is a dictionary holding other parameters in the Content-Type header.

5. cgi.parse_header(string)

This function will parse a MIME header into the main value and a dictionary of parameters.

6. cgi.test()

This function is a test script. We can use it as the main program. It will write minimal HTTP headers and will format the information into HTML form.

7. cgi.print_environ()

This function formats the shell environment in HTML.

8. cgi.print_form(form)

This formats a form in HTML.

9. cgi.print_directory()

This function will format the current directory in HTML.

10. cgi.print_environ_usage()

This function prints a list of all useful environment variables in HTML.

11. cgi.escape(s, quote=False)

This function converts characters ‘<’, ‘>’, and ‘&’ in the string ‘s’ to HTML-safe sequences. These help in displaying these characters in HTML.

We discussed a lot about CGI. Now let us see some pros and cons.

Advantages of Python CGI Programming

1. They are portable, can work on many operating systems and web servers.
2. These are independent of the language of the script.
3. The programs are scalable in nature, they can perform both simple and complex tasks.
4. These develop dynamic communication in web applications.
5. These decrease the development and maintenance costs.

Disadvantages of Python CGI Programming

1. The scripts have to be evaluated every time the program is initiated. This might create an increase the traffic.
2. Writing the CGI programs is complex.
3. The scripts compromise server security, make the free and easily available data prone to vulnerability

Conclusion

In this article, we discussed the CGI programming in Python, the CGI module, and its functions. Finally, we saw some advantages and disadvantages.

Hope you understood the concepts covered. Happy learning!

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google | Facebook


Leave a Reply

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