90 likes | 180 Views
Lecture 23 . Starting CGI. Documentation. http://docs.python.org/library/cgi.html. <html> <head> <title> First example </title> </head> <body> <form action = " cgi -bin/test.py" method = "get"> <form> Please enter your name. <input type = "text" name = "name"> < br >
E N D
Lecture 23 Starting CGI
Documentation http://docs.python.org/library/cgi.html
<html> <head> <title> First example </title> </head> <body> <form action = "cgi-bin/test.py" method = "get"> <form> Please enter your name. <input type = "text" name = "name"> <br> <input type = "submit"> </form> </body> </html>
#!/usr/bin/python print "Content-type: text/html" print import cgi import cgitb cgitb.enable() form = cgi.FieldStorage() fname = form.getvalue("name") print "<html>" print "<head> <title> Reply </title></head>" print '<body >' print print "<h2> Hello, " + fname + "<br>" print "</body></html>"
<html> <head> <title> First example </title> </head> <body> <form action = "cgi-bin/test1.py" method = "get"> <form> Please enter your name. <input type = "text" name = "name"> <br> Please enter all majors you are considering: <br> <input type = "checkbox" name = "major" value = "CSC"> Computer Science <br> <input type = "checkbox" name = "major" value = "MAT"> Mathematics <br> <input type = "checkbox" name = "major" value = "PHY"> Physics <br> <br> <input type = "submit"> </form> </body> </html>
#!/usr/bin/python print "Content-type: text/html" print import cgi import cgitb cgitb.enable() form = cgi.FieldStorage() fname = form.getvalue("name") majors = form.getlist("major") print "<html>" print "<head> <title> Reply </title></head>" print '<body >' print print "<h2> Hello, " + fname + "<br>" print "These are the majors you are considering:" + "<br>" for m in majors: print m + "\n" print "</body></html>"
Methods you need to know • form = cgi.FieldStorage() • To get the value of an input that has a single value that you know was filled in: item = form.getvalue(“name”) • A safer method that will provide a default item = form.getfirst(“name”, “”) • To get the list of choices for checkboxes choices = form.getlist(“items”)