1 / 38

Another CGI

Another CGI. Python. Strings. Single and double quotes interchangeable Triple quotes ( &quot;&quot;&quot; ) allow multi-line literal quoting Escaping of <br> , % , etc by default Prefix r can be used for regexps, r'[a-z].*'

Download Presentation

Another CGI

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. Another CGI Python

  2. Strings • Single and double quotes interchangeable • Triple quotes (""") allow multi-line literal quoting • Escaping of \n, %, etc by default • Prefix r can be used for regexps, r'[a-z].*' • Any variable can be converted to a string with str(v). Can go back with int(s), float(s) • Strings operations: • Concatenation with +, repeating with * • Length with len() • Substrings with []. Interesting index options. • Special boolean operator in

  3. String Methods • count • find • isdigit • upper, lower • rjust, ljust • strip • replace

  4. Tuples and Lists • Tuples store a set of elements. Syntax: • foo = ("Korn", "Jeff") • Lists also store elements but unlike tuples can be modified (thus cheaper). Syntax: • foo = ["Korn", "Jeff"] • foo[1] = "Jeffrey" • List operations: • append, extend, +, *, len, index, in, pop, sort, reverse, join, etc. • Be careful about references!

  5. Dictionary • Dictionaries are associative arrays (arrays indexed by string or number). Values can be any object. • Syntax: • student = {'name' : 'Guido', 'score': 95 } • Operations: • has_key, keys, values, len, get, copy, update • Example: print student['name']

  6. Statements • Standard if, then, else • While loops if person == 'Korn': status = 'teacher' elif person == 'Cano' status = 'yankee' else: status = 'unknown' print person, status while n <= 20: s.append(' ') n += 2 if magic: break a, b = 0, 1 while b < 100: print b a, b = b, a + b

  7. for loops • Similar to shell: • To do c-style loops, use special function range: • Also: xrange for val in ["foo", "bar", "baz"]: print "Hello %s" % val for val in range(5, 20, 2): print val # prints 5, 7, 9 ... 19

  8. Exceptions • Run-time errors are possible with Python, but they can be caught with try-except: • You can also use: raise, finally try: n = float(inputstring) m = n * 2 except ValueError, msg: print msg # will print: invalid literal for float(): foo

  9. Functions • Defined with def: • Types not needed • Arguments can have defaults: • Variables in functions are local • global changes this • return used to return values def my_func(foo, bar): print foo + bar def my_func(foo, bar=12): print foo + bar my_func(3) 15 my_func(3, bar=3)

  10. Modules • Python modules are libraries of reusable code with specific functionalities • Standard modules are distributed with Python to do a variety of things. • Modules names are unique, but functions in modules don't conflict with other modules. • Modules can have sub-modules.

  11. Using Modules • Include modules in your program with use, e.g. import math incorporates the math module import math print math.log(4906, 2) 12.0 print math.sqrt(100) 10

  12. Important modules • sys • sys.argv, sys.path, sys.platform, sys.exit, sys.stdin, sys.stderr, sys.stdout • os • os.getcwd, os.environ, os.chdir, os.listdir, os.mkdir, os.rmdir, os.remove, os.system, os.popen, os.getpid • os.path • os.path.abspath, os.path.dirname, os.path.basename, os.path.join, os.path.split, os.isfile, os.isdir

  13. HTTP • Protocol for the Web • Client-server model • Client (user agent) is typically web browser (e.g. Firefox) • Server is web server (e.g. Apache) • Client request a resource identified by URLs • e.g. http://cs.nyu.edu/csweb/index.html

  14. Apache HTTP Server • Open source HTTP (Web) server • Most popular web server since 1996 • The “A” in LAMP • Part of Apache Software Foundation • Other projects: Ant, Hadoop, Tomcat, SpamAssassin, …

  15. HTTP Transactions • HTTP request to web server GET /v40images/nyu.gif HTTP/1.1 Host: www.nyu.edu • HTTP response to web client HTTP/1.1 200 OK Content-type: image/gif Content-length: 3210

  16. HTML Example • <html> • <head> • <title>Some Document</title> • </head> • <body> • <h2>Some Topics</h2> • This is an HTML document • <p> • This is another paragraph • </body> • </html>

  17. HTML • File format that describes a webpage • Can be written by hand, or generated by a program • A good way to generate a HTML file is by writing a shell or Perl script

  18. Gateways • Interface between resource and a web server Web Server Gateway resource HTTP

  19. CGI • Common Gateway Interface - a standard interface for running helper applications to generate dynamic contents • Specify the encoding of data passed to programs • Allow HTML documents to be created on the fly • Transparent to clients • Client sends regular HTTP request • Web server receives HTTP request, runs CGI program, and sends contents back in HTTP responses • CGI programs can be written in any language

  20. How CGI Works HTTP request Web Server HTTP response spawn process Script Document

  21. Forms • HTML forms are used to collect user input • Data sent via HTTP request • Server launches CGI script to process data <form method=POST action=“http://cs.nyu.edu/~unixtool/cgi-bin/search.cgi”> Enter your query: <input type=text name=Search> <input type=submit> </form>

  22. Input Types • Text Field <input type=text name=zipcode> • Radio Buttons <input type=radio name=size value=“S”> Small <input type=radio name=size value=“M”> Medium <input type=radio name=size value=“L”> Large • Checkboxes <input type=checkbox name=extras value=“lettuce”> Lettuce <input type=checkbox name=extras value=“tomato”> Tomato • Text Area <textarea name=address cols=50 rows=4> … </textarea>

  23. Submit Button • Submits the form for processing by the CGI script specified in the form tag <input type=submit value=“Submit Order”>

  24. HTTP Methods • Determine how form data are sent to web server • Two methods: • GET • Form variables stored in URL • POST • Form variables sent as content of HTTP request

  25. Encoding Form Values • Browser sends form variable as name-value pairs • name1=value1&name2=value2&name3=value3 • Names are defined in form elements • <input type=text name=ssn maxlength=9> • Special characters are replaced with %## (2-digit hex number), spaces replaced with + • e.g. “10/20 Wed” is encoded as “10%2F20+Wed”

  26. GET/POST examples GET: GET /cgi-bin/myscript.pl?name=Bill%20Gates&company=Microsoft HTTP/1.1 HOST: www.cs.nyu.edu POST: POST /cgi-bin/myscript.pl HTTP/1.1 HOST: www.cs.nyu.edu …other headers… name=Bill%20Gates&company=Microsoft

  27. GET or POST? • GET method for • Retrieving information, e.g. from a database • A “safe” method - no action taken other than retrieval • Embedding data in URL without form element • POST method for • Forms with many fields or long fields • Sending data for updating database, posting to bulletin board, etc. • GET requests may be cached by clients browsers or proxies, but not POST requests

  28. Parsing Form Input • Method stored in HTTP_METHOD • GET: Data encoded into QUERY_STRING • POST: Data in standard input (from body of request) • Most scripts parse input into an associative array • Parse it yourself, or • Use available libraries (e.g. Perl CGI module)

  29. CGI Environment Variables • DOCUMENT_ROOT • HTTP_HOST • HTTP_REFERER • HTTP_USER_AGENT • HTTP_COOKIE • REMOTE_ADDR • REMOTE_HOST • REMOTE_USER • REQUEST_METHOD • SERVER_NAME • SERVER_PORT

  30. Example: Comment Form

  31. Part 1: HTML Form <html> <center> <H1>Anonymous Comment Submission</H1> </center> Please enter your comment below which will be sent anonymously to <tt>kornj@cs.nyu.edu</tt>. If you want to be extra cautious, access this page through <a href="http://www.anonymizer.com">Anonymizer</a>. <p> <form action=cgi-bin/comment.cgi method=post> <textarea name=comment rows=20 cols=80> </textarea> <input type=submit value="Submit Comment"> </form> </html>

  32. Part 2: CGI Script (ksh) #!/home/unixtool/bin/ksh . cgi-lib.ksh # Read special functions to help parse ReadParse PrintHeader print -r -- "${Cgi.comment}" | /bin/mailx -s "COMMENT" kornj print "<H2>You submitted the comment</H2>" print "<pre>" print -r -- "${Cgi.comment}" print "</pre>"

  33. Example: Find words in Dictionary <form action=dict.cgi> Regular expression: <input type=“text” name=re value=".*"> <input type=submit> </form>

  34. Example: CGI Script (ksh) #!/home/unixtool/bin/ksh PATH=$PATH:. . cgi-lib.ksh ReadParse PrintHeader print "<H1> Words matching <tt>${Cgi.re}</tt> in the dictionary </H1>\n"; print "<OL>" grep "${Cgi.re}" /usr/dict/words | while read word do print "<LI> $word" done print "</OL>"

  35. Debugging • Debugging can be tricky, since error messages don't always print well as HTML • One method: run interactively $ QUERY_STRING='birthday=10/15/03'$ ./birthday.cgi Content-type: text/html <html>Your birthday is <tt>10/15/02</tt>.</html>

  36. A Python CGI Script #!/usr/bin/python import cgi import cgitb cgitb.enable() form = cgi.FieldStorage() bday = form['birthday'].value # Print header print 'Content-Type: text/html' print # Your HTML body print "Your birthday is %s.\n" % bday

  37. Debugging Python CGI Scripts • Debugging CGI script is tricky - error messages don’t always come up on your browser • Run script with test data • python cgiScript prod=“MacBook” price=“1800” Content-Type: text/html <html> … </html>

  38. CGI Benefits • Simple • Language independent • UNIX tools are good for this because • Work well with text • Integrate programs well • Easy to prototype • No compilation (CGI scripts)

More Related