Title: Python
1Python
- Web Applications
- A KISS Introduction
2Web Applications with Python
- Fetching, parsing, text processing
- Database client mySQL, etc., for building
dynamic information on-the-fly - Python CGI web pages or even web servers!
3Fetching Pages
- Use urllib or urllib2 library
- Python is used frequently to parse data pages
regular expressions are very important - Nothing new here
gtgtgt from urllib2 import gtgtgt file
urlopen("http//www.mizzao.tk/mao") gtgtgt
file.read() 'lthtmlgt\nltheadgt\nlttitlegtA Simple
Pagelt/titlegt\nlt/headgt\nltbodygt\nNothing to
see...\nlt/bodygt\nlt/htmlgt gtgtgt file.read.split(\n
) (all the lines at once) gtgtgt file.readline() (on
e line at a time)
4Parsing Data
- Say there is a webpage you want to pull certain
information from every dayuse Python! - You already know how to parse webpages though
and that isnt really a web application - Lots of complex parsers that are beyond the scope
of this demo - On to the interesting stuff!
5Databases
- A database is a program that manages a set of
data (gee, who would have guessed) - Most databases are relational SQL databases
- SQL Structured Query Language, a standardized
syntax for telling a database what to do
although Microsofts SQL is different from
everyone elses
6Databases
- mySQL is the most widely used open-source
database Python supports it! - Uses library MySQLdb a mySQL API for Python
- Must install this yourself on Linux with a
package manager or by tarball, on Windows with an
archive
7How SQL Databases Work
- Databases take some sort of connection as
communication usually through TCP/IP - Most databases are set up as a group of schemas,
each containing a set of tables - Each table contains a set of columns, each with a
different data type - Rows are the entries in a table
8MySQL Queries
- The library is called _mysql, and it implements
the standard Python Database API Specification - Almost all database modules for python implement
the API, which defines the set of functions - connect() takes parameters to establish a
connection, and select_db() chooses the schema to
work with
gtgtgt from _mysql import gtgtgt db
connect(host"localhost",user"mao",passwd"") gtgtgt
db.select_db("test")
9Some Python Database Commands
10Some Simple Database Examples
- gtgtgt db.query("CREATE TABLE students (id INT NOT
NULL AUTO_INCREMENT PRIMARY KEY, lname TEXT,
fname TEXT)") - gtgtgt db.query("INSERT INTO students (lname, fname)
VALUES ('Mao', 'Andrew')") - gtgtgt db.query("INSERT INTO students (lname, fname)
VALUES ('Walker', 'James')") - gtgtgt db.query("INSERT INTO students (lname, fname)
VALUES ('Cohen', 'Ian')") - gtgtgt db.query("SELECT FROM students")
- gtgtgt people db.store_result()
- gtgtgt people.fetch_row()
- (('1', 'Mao', 'Andrew'),)
- gtgtgt people.fetch_row()
- (('2', 'Walker', 'James'),)
- gtgtgt people.fetch_row()
- (('3', 'Cohen', 'Ian'),)
- gtgtgt people.fetch_row()
- ()
- gtgtgt db.close()
11What Do We Do Now?
- So we have access to a database and we can get
data from it but how do we get that across the
web? - Answer CGI!
- CGI stands for Common Gateway Interface Allows
any script (perl, bash, python) to output to
HTTP. - Its how you submit your CSE 121 homework
12Using Python through CGI
- First, you must set up the web server to use
handle Python scripts with CGI - With Apache, this is easy 2 lines in httpd.conf
AddHandler cgi-script .cgi .py ltDirectory
/gt Options ExecCGI lt/Directorygt
13A Web Python Hello World!
- When we run a Python program as a CGI script, the
output goes out directly and the file must be
executable (x). - The first line we have to print are the HTTP
headers this tells the browser to read it as
either text, or render it as HTML. - Any line after that comprises the actual HTML or
text itself
!/usr/bin/python Required header that tells
the browser how to render the text. print
"Content-Type text/plain\n\n" Print a simple
message to the display window. print "Hello,
World!\n"
14Lo and Behold
- Notice that the first line went through as HTTP
headers.
15Tying It All Together
- We want to be able to receive some input to our
python program, and then output something based
on that input. - The python cgi library can find all the HTTP POST
and GET variables. - We can then use these to query the database, etc.
16A Final Simple Program
- cgitb is a library that dumps errors to the
output you dont have to go searching for it in
your log file. - cgi.FieldStorage() is a pseudo-dictionary
containing all the POST and GET variables and
their values.
!/usr/bin/python import cgi,_mysql,cgitb
cgitb.enable() search cgi.FieldStorage()
returns a dictionary print "Content-Type
text/plain\n\n" if not (search.has_key("fname")
and search.has_key("lname")) print "Need a
first and last name!" exit fname,lname
search"fname".value,search"lname".value conn
_mysql.connect(host"localhost",user"mao") conn
.select_db("test") conn.query("SELECT FROM
students WHERE lname '"lname"' AND fname
'"fname"'") people conn.store_result() row
people.fetch_row() if row () print
fname,lname,"is not a student.\n" else print
fname,lname,"is a student.\n" conn.close()
17HTML Form Interface
lthtmlgt ltheadgt lttitlegtStudent Searchlt/titlegt lt/head
gt ltbodygt ltform action "cgi-bin/search.py"
method 'GET'gt First Nameltinput type 'text'
name 'fname'gt ltbrgt Last Nameltinput type
'text' name 'lname'gt ltbrgt ltinput type
'submit' value 'Find this Person'gt lt/bodygt lt/htm
l
18Questions?