Download presentation
Presentation is loading. Please wait.
Published byWilfrid Blair Modified over 9 years ago
1
Python Another CGI
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 Some Document Some Topics This is an HTML document This is another paragraph
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 resource Gateway 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 Web Server Script Document HTTP request HTTP response spawn process
21
Forms HTML forms are used to collect user input Data sent via HTTP request Server launches CGI script to process data Enter your query:
22
Input Types Text Field Radio Buttons Small Medium Large Checkboxes Lettuce Tomato Text Area …
23
Submit Button Submits the form for processing by the CGI script specified in the form tag
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 – 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 Anonymous Comment Submission Please enter your comment below which will be sent anonymously to kornj@cs.nyu.edu. If you want to be extra cautious, access this page through Anonymizer.
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 " You submitted the comment " print " " print -r -- "${Cgi.comment}" print " "
33
Example: Find words in Dictionary Regular expression: <input type=“text” name=re value=".*">
34
Example: CGI Script (ksh) #!/home/unixtool/bin/ksh PATH=$PATH:.. cgi-lib.ksh ReadParse PrintHeader print " Words matching ${Cgi.re} in the dictionary \n"; print " " grep "${Cgi.re}" /usr/dict/words | while read word do print " $word" done print " "
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 Your birthday is 10/15/02.
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 …
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)
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.