What are the modules required for CGI programming in Python?

PythonServer Side ProgrammingProgrammingCGI

Python's cgi module is usually the starting place in writing CGI programs in Python. The main purpose of the cgi module is to extract the values passed to a CGI program from an HTML form. Mostly one interacts with CGI applications through an HTML form. One fills out some values in the form that specify details of the action to be performed, then call on the CGI to perform its action using your specifications.

You may include many input fields within an HTML form which can be of a number of different types (text, checkboxes, picklists, radio buttons, etc.).

Your Python script should begin with import cgi. The main thing done by the CGI module is to treat all the fields in the calling HTML form in a dictionary-like fashion. What you get is not exactly a Python dictionary, but it is easy to work with. Let's see an example −

Example

import cgi
form = cgi.FieldStorage()   # FieldStorage object to
# hold the form data
# check whether a field called "username" was used...
# it might be used multiple times (so sep w/ commas)
# Next item -- insert comma
else:
# First item -- don't insert comma
else:
field_list = field_list + '</ul>\n'