Python

  1. Home
  2. Computing & Technology
  3. Python

Programming CGI With Python

From Al Lukaszewski, for About.com

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

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

Easy ways to connect two computers for networking purposes. More >

Python

  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.