Cheat Sheet Python 7.pdf copy.docx

An instance of in order to retrieve information from

This preview shows page 2 out of 4 pages.

an instance of in order to retrieve information from a form in a Python CGI script Simple FORM Example:GET Method passes two values using HTML FORM and submit button < form   action   =   "/cgi-bin/hello_get.py"   method                                        =   "get" >    First Name:  <input   type   =   "text"   name   =                    "first_name" >    <br   />    Last Name:  <input   type   =   "text"   name   =                      "last_name"   />     <input   type   =   "submit"   value   =   "Submit"   /> </form> Passing Information Using POST Method [recommend] #!/usr/bin/python # Import modules for CGI handling  import  cgi ,  cgitb  # Create instance of FieldStorage  form  =  cgi . FieldStorage ()   # Get data from fields first_name  =  form . getvalue ( ' first_name' ) last_name   =  form . getvalue ( 'last_name' ) print   "Content-type:text/html\r\n\r\n" print   "<html>" print   "<head>" print   "<title>Hello - Second CGI  Program</title>" print   "</head>" print   "<body>" print   "<h2>Hello %s %s</h2>"   %   ( first_name ,   last_name ) print   "</body>" print   "</html>" <form   action   =   "/cgi-bin/hello_get.py"   method   =   "post" > First Name:  <input   type   =   "text"   name   =   "first_name" ><br   /> Last Name:  <input   type   =   "text"   name   =   "last_name"   /> <input   type   =   "submit"   value   =   "Submit"   /> </form> CGI Code example #!/usr/bin/python ''' This program creates a website with two text fields and a submit button. If the submit button is pressed and  the two text fields contained numbers, the webpage then  displays the sum of those two numbers as well as the original  form. ''' import  cgi import  cgitb cgitb.enable()  # This enables the traceback feature  for debugging. # Get values from POST request, if a POST request was  made. form = cgi.FieldStorage() number1 = form.getvalue( 'first' '' ) number2 = form.getvalue( 'second' '' ) # Report one or both numbers missing or try to compute  the sum. if  number1 ==  ''   or  number2 ==  '' :         result =  'One or both numbers missing.' else :          try :                 result =  str ( int (number1) +  int (number2))          except  ValueError:                 result =  'One or both numbers invalid.' # Print response. print ( '''Content-type: text/html <!DOCTYPE html> <html>   <body>     <p>Sum: {0}</p>     <form method="POST" action="hello.cgi">       <input type="text" name="first" />       <input type="text" name="second" />       <input type="submit" />  # submit button     </form>   </body> </html> ''' . format (result)) ------------------------------------------------------- ''' Must have: 'Content-Type: text/html' in the print() in  order to output in the website!! ''' print ( '''Content-Type: text/html <!DOCTYPE html> <html>     <body>         <p>Hi there!</p>     </body> </html>''' ) # This program tries to output a website with a form.
Image of page 2
You've reached the end of this preview.
  • Spring '16
  • World Wide Web, Regular expression, HTTP, URLError

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern