1 / 9

Lecture 23

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 >

Télécharger la présentation

Lecture 23

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 23 Starting CGI

  2. Documentation http://docs.python.org/library/cgi.html

  3. <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>

  4. http://turing.centre.edu/~christine.shannon/test.html

  5. #!/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>"

  6. http://turing.centre.edu/~christine.shannon/test1.html

  7. <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>

  8. #!/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>"

  9. 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”)

More Related