1. Technology

Programming CGI With Python


2 of 4

Accessing CGI Form Data

CGI passes information to Python in the form of a class called FieldStorage. In your Pythonic CGI script, you must create an instance of FieldStorage in order to access the CGI data. The main attribute of class FieldStorage which you will need to know is 'getvalue'. For example, the data from an address book form might be accessed as follows:

 # Import modules for CGI handling 
 import cgi, cgitb 
 # Create instance of FieldStorage 
 form = cgi.FieldStorage() 
 # Get data from field 'name' 
 name = form.getvalue('name') 
 # Get data from field 'address' 
 address = form.getvalue('address') 
 # Get data from field 'phone' 
 phone = form.getvalue('phone') 
 # Get data from field 'email' 
 email = form.getvalue('email') 

From here, the data has been assigned to the variables. You can thus handle the values within the program like other literals.

  1. About.com
  2. Technology
  3. Python
  4. Web Development
  5. Python CGI - Accessing CGI Form Data With Python's CGI

©2014 About.com. All rights reserved.