1. Home
  2. Computing & Technology
  3. Python

Programming CGI With Python

From , former About.com Guide

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.

Explore Python
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Python
  4. Web Development
  5. Python CGI - Accessing CGI Form Data With Python's CGI

©2009 About.com, a part of The New York Times Company.

All rights reserved.