Python Crash Course + Web Forms (CGI)
CS370 SE Practicum, Cengiz Günay (Some slides courtesy of Eugene Agichstein and the Internets)
CS370, Günay (Emory) Python Intro + CGI Spring 2014 1 / 6
Python Crash Course + Web Forms (CGI) CS370 SE Practicum, Cengiz - - PowerPoint PPT Presentation
Python Crash Course + Web Forms (CGI) CS370 SE Practicum, Cengiz Gnay (Some slides courtesy of Eugene Agichstein and the Internets) CS370, Gnay (Emory) Python Intro + CGI Spring 2014 1 / 6 Agenda Lab sessions: Coming Tuesday 1/21 Can
CS370 SE Practicum, Cengiz Günay (Some slides courtesy of Eugene Agichstein and the Internets)
CS370, Günay (Emory) Python Intro + CGI Spring 2014 1 / 6
Lab sessions: Coming Tuesday 1/21 Can bring laptops? Or need to find alternate computer lab time:
1
Thu 5–6pm ?
CS370, Günay (Emory) Python Intro + CGI Spring 2014 2 / 6
1 Mondays ◮ 2–5 pm and 6–7 pm 2 Tuesdays ◮ 4–6:15 pm 3 Weds ◮ 2–3 pm and 4–5 pm 4 Thursdays ◮ 11:30 am–12:30 pm and 4–5 pm 5 Fridays ◮ 11:45 am–12:35 pm, 2–3 pm, and 4–5 pm CS370, Günay (Emory) Python Intro + CGI Spring 2014 3 / 6
Reading: Code Complete chapters 1–5 by next week
CS370, Günay (Emory) Python Intro + CGI Spring 2014 4 / 6
Reading: Code Complete chapters 1–5 by next week Today: Python Crash Course Python for getting data from web forms (by using the common gateway interface – CGI)
CS370, Günay (Emory) Python Intro + CGI Spring 2014 4 / 6
1/17/2013 5
6
compile execute
source code Hello.java byte code Hello.class interpret
source code Hello.py
1/17/2013
1/17/2013 7
Guido van Rossum: creator of Python
1/17/2013 8
>>> print "Hello, world" Hello, world >>> x = 12**2 >>> x/2 72 >>> # this is a comment
1/17/2013 9
1/17/2013 10
1/17/2013 11
1/17/2013 12
>>> a = range(5) # [0,1,2,3,4] >>> a.append(5) # [0,1,2,3,4,5] >>> a.pop() # [0,1,2,3,4] 5 >>> a.insert(0, 5.5) # [5.5,0,1,2,3,4] >>> a.pop(0) # [0,1,2,3,4] 5.5 >>> a.reverse() # [4,3,2,1,0] >>> a.sort() # [0,1,2,3,4]
1/17/2013 13
1/17/2013 14
1/17/2013 15
1/17/2013 16
1/17/2013 17
1/17/2013 18
1/17/2013 19
a 1 2 3 b a 1 2 3 b 4 a = [1, 2, 3] a.append(4) b = a a 1 2 3
1/17/2013 20
a 1 b a 1 b a = 1 a = a+1 b = a a 1 2
by assignment (a=...) new int object created by add operator (1+1)
1/17/2013 21
1/17/2013 22
for i in range(20): if i%3 == 0: print i if i%5 == 0: print "Bingo!" print "---"
for (i = 0; i < 20; i++) { if (i%3 == 0) { printf("%d\n", i); if (i%5 == 0) { printf("Bingo!\n"); } } printf("---\n"); }
Bingo!
Bingo!
1/17/2013 23
1/17/2013 24
def gcd(a, b): "greatest common divisor" while a != 0: a, b = b%a, a # parallel assignment return b >>> gcd.__doc__ 'greatest common divisor' >>> gcd(12, 20) 4
1/17/2013 25
class name: "documentation" statements
class name(baseclass1, baseclass2, ...): ... Typically, statements contains method definitions: def name(self, arg1, arg2, ...): ... May also contain class variable assignments
1/17/2013 26
class Stack: "A well-known data structure…" def __init__(self): # constructor self.items = [] def push(self, x): self.items.append(x) # the sky is the limit def pop(self): x = self.items[-1] # what happens if it’s empty? del self.items[-1] return x def empty(self): return len(self.items) == 0 # Boolean result
1/17/2013 27
x = Stack() # no 'new' operator!
x.empty() # -> 1 x.push(1) # [1] x.empty() # -> 0 x.push("hello") # [1, "hello"] x.pop() # -> "hello" # [1]
x.items # -> [1]
1/17/2013 28
class FancyStack(Stack): "stack with added ability to inspect inferior stack items" def peek(self, n): "peek(0) returns top; peek(-1) returns item below that; etc." size = len(self.items) assert 0 <= n < size # test precondition return self.items[size-1-n]
1/17/2013 29
class LimitedStack(FancyStack): "fancy stack with limit on stack size" def __init__(self, limit): self.limit = limit FancyStack.__init__(self) # base class constructor def push(self, x): assert len(self.items) < self.limit FancyStack.push(self, x) # "super" method call
1/17/2013 30
class Connection: verbose = 0 # class variable def __init__(self, host): self.host = host # instance variable def debug(self, v): self.verbose = v # make instance variable! def connect(self): if self.verbose: # class or instance variable? print "connecting to", self.host
1/17/2013 31
1/17/2013 32
1/17/2013 33
1/17/2013 34
1/17/2013 35
1/17/2013 36
1/17/2013 37
– sys.exc_info() == (exc_type, exc_value, exc_traceback)
– sys.last_type, sys.last_value, sys.last_traceback
1/17/2013 38
1/17/2013 39
More web programming next class Introduction: Common Gateway Interface (CGI)
◮ All dynamic web pages uses this CS370, Günay (Emory) Python Intro + CGI Spring 2014 6 / 6
More web programming next class Introduction: Common Gateway Interface (CGI)
◮ All dynamic web pages uses this
Submitting web form calls executable Executable produces HTML that shows up on “next” page Python is the executable in this example
CS370, Günay (Emory) Python Intro + CGI Spring 2014 6 / 6
1/17/2013 42
<form method="POST" action="http://host.com/cgi-bin/test.py"> <p>Your first name: <input type="text" name="firstname"> <p>Your last name: <input type="text" name="lastname"> <p>Click here to submit form: <input type="submit" value="Yeah!"> <input type="hidden" name="session" value="1f9a2"> </form>
1/17/2013 43
#!/usr/local/bin/python import cgi def main(): print "Content-type: text/html\n" form = cgi.FieldStorage() # parse query if form.has_key("firstname") and form["firstname"].value != "": print "<h1>Hello", form["firstname"].value, "</h1>" else: print "<h1>Error! Please enter first name.</h1>" main()
1/17/2013 44
– use cgi.FieldStorage class to parse query
{'foo': 'ab cd!ef', 'bar': 'spam'} # (well, actually, ...)
– this is up to you! – database interfaces available
– print statements are simplest – template solutions available
1/17/2013 45
form = cgi.FieldStorage() if not form: ...display blank form... elif ...valid form...: ...perform action, display results (or next form)... else: ...display error message (maybe repeating form)...
1/17/2013 46
1/17/2013 47
– translate "<", "&", ">" to "<", "&", ">"
– parse query string to dictionary {"foo": ["bar"], ...}
– ditto, takes query string from default locations
– convert between "~" and "%7e" (etc.)
– convert dictionary {"foo": "bar", ...} to query string "foo=bar&..." # note asymmetry with parse_qs() above
1/17/2013 48
1/17/2013 49