Join Club ShowMeDo to Learn Python!

This series is a part of Club ShowMeDo. Click to learn why you should join our club with a simple monthly subscription.

  • We teach Python and all the cool modules
  • Topics include beginner Python, GUIs, Web and Desktop Apps
  • Long, specially crafted video tutorials just for you
  • Club video tutorials extend our normal Free content
  • Save your time - we've done the research for you
  • Keep your skills up-to-date
  • Learn at your own pace, everything you need is shown to you
  • Over 12 hours of archived material are waiting for you in the club

Name: [011] John Montgomery
Member: 126 months
Authored: 21 videos
Description: I'm a C/C++, Java and Python programmer living in Hove, UK. ...

Using Python's CGI module [ID:647] (3/6)

in series: Introduction to Python web-programming: CGI

(Showmedo is undergoing major changes. To report any problems viewing the videos please email us and include browser and OS specifics. Cheers - Kyran.)

In this ShowMeDo I cover using Python's CGI module for handling form fields.

I also demonstrate the use of the cgitb module (CGI Traceback) to help make debugging script errors easier.

To round it off I demonstrate how one needs to be careful with user input. In particular I show how easy it can be to accidentally allow cross-site scripting exploits.


import cgi

# use this to enable debug output
import cgitb; cgitb.enable()

print "Content-Type: text/html"
print "<html>"
print "<body>"

print "<h1>Using the CGI module</h1>"

print "<form method='post'>"

# standard single line text field
print "text_field:"
print "<input type='text' name='text_field' value='' />"
print "<br />"

# hidden field (not editable by user - though they can if they really want)
print "<input type='hidden' name='hidden_field' value='a hidden value' />"

# check box field
print "checkbox_field:"
print "<input type='checkbox' name='checkbox_field' value='ticked' />"
print "<br />"

# radio buttons
print "radio_field_field:"
print "<input type='radio' name='radio_field' value='left' />"
print "<input type='radio' name='radio_field' value='right' />" 
print "<br />"

# select box
print "select_field: "
print "<select name='select_field'>"
print "<option value='selected_one'>one</option>"
print "<option value='selected_two'>two</option>"
print "<option value='selected_three'>three</option>"
print "</select>"
print "<br />"

# text area
print "text_area:<br />"
print "<textarea name='text_area' rows='5' cols='20'></textarea>"
print "<br />"

print "<input type='submit' value='submit form' />"
print "</form>"

form = cgi.FieldStorage()
print "<h2>Parameters</h2>"
print "<ul>"
for field_name in form:
	print "<li>"
	print " = "
	print cgi.escape(repr(field.value))
	print "</li>"
print "</ul>"

print "</body>"
print "</html>"

Got any questions?

Get answers in the ShowMeDo Learners Google Group.

Video statistics:

  • Video's rank shown in the most popular listing
  • Video plays: 1 (since July 30th)
  • Plays in last week: 0
  • Published: 100 months ago

Thank-yous, questions and comments

If this video tutorial was helpful please take some time to say thank-you to the authors for their hard work. Feel free to ask questions. Let the author know why their video tutorial was useful - what are you learning about? Did the video tutorial save you time? Would you like to see more?

You may also want to see our ShowMeDo Google Group to speak to our active users and authors.

Your email address will not be published.

Show some quick comments >>

All comments excluding tick-boxed quick-comments

I never really looked into the python cgi stuff, now however I find myself wanting to learn more about it. Great work.

5. anonymous Thu, 24 Sep 2009 07:41

can u explain about how to do mini project in python

great stuff !!!

great stuff !!!

Video published, thanks for contributing to ShowMeDo

Showmedo is a peer-produced video-tutorials and screencasts site for free and open-source software (FOSS)- with the exception of some club videos, the large majority are free to watch and download.

how to help » about » faq »

Educating the Open-source Community With Showmedo

Although as important as the software it supports, education and documentation are relatively neglected in the Open-source world. Coders love to code, and explaining how best to use or improve the software tends to be deferred or even sidelined.

At Showmedo we believe the community can play a vital role here and also say thanks for the tools and software that make our lives easier. If you have a piece of software you love or a programming langugage you are enthusiastic about, why not make a screencast showing others how to use it? All the stuff you wish you'd been told, the tips, tricks, insights that would have saved you time and frustration.

Screencasting is easier than you think, and we're happy to help you. You can emailus for advice or just use some of the how-to screencasts on the site. This screencasting learning-pathis a good place to start.

Kudos and Thanks for John

The 'Hello World' Applet - Part 1

Thanks very helpful!


maccosmeticswholesalers mac makeup 32pcs brushes c 1
70 months ago
Code-completion in Eclipse


maccosmeticswholesalers benefit cosmetics c 49
70 months ago


Showmedo's development is fairly rapid and bugs will inevitably creep in. If you have any problems please drop us a line using the contact address below. Likewise, any suggestions for improvements to the site are gratefully received.