A Python Example

Now let’s look at an example written in Python. If the user’s form input contains the hidden field action, the script processes that information. Otherwise, it prints a new form to be filled out. The source code for the script is in Listing 11.4.

Listing 11.4. A CGI Script Written in Python
 1: #!/usr/local/bin/python 2: 3: import cgi 4: form = cgi.FieldStorage() 5: 6: def print_page_start(): 7: print "Content-type: text/html\n\n" 8: print "<html><head><title>Example Form</title></head><body>\n" 9: print "<h1>Example Form</h1>" 10: 11: def print_page_end(): 12: print "</body></html>\n" 13: 14: def print_form(): 15: print "<form>\n" 16: print "<input type=\"hidden\" name=\"action\" value=\"process\" />\n" 17: print "Name: <input ...

Get Sams Teach Yourself CGI in 24 Hours, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.