Lectures‎ > ‎

Week04

Preliminaries

  • Check website regularly.
  • Make sure you are subscribed to the CS110-02 Google Group

Overview

  • Strategies for doing labs and studying.
  • Review of Chapter 4
  • More on HTML and web directories
  • Executing python in web directories

Functions

  • Return values
  • Composition

Doc Tests

  • Used to test python code
  • Extremely usefule

Executing Python Code in Web Directories

  • First, create a cs110 subdirectory in your web directory:
$ cd /home/web/<usernmae>
$ mkdir cs110
$ chmod 755 cs110
  • The last chmod allows the CS web server to access your new cs110 directory
    • You will need to do this for each new subdirectory you create.
  • In your /home/web/<username>/cs110 directory, create an ".htaccess" file:
$ cd /home/web/<username>/cs110
$ cat > .htaccess
Options +ExecCGI
AddHandler cgi-script .py
^D
$ chmod 644 .htaccess


  • Now you can put .py files in any subdirectory
  • Remember to set the permissions for each file you add to your web directory (chmod 644 <filename>)
  • If you create new subdirectories, be sure to set the permissions of the subdirectory with chmod 755 <dirname>
  • The first line of your python program should be:
#!/usr/bin/env python
  • Your Python code must print the following first before any HTML:
print 'Content-type: text/html'
print
  • For each Python file you create, you also need to give the web server permission to read and execute it:
chmod 755 file.py

  • Example:
$ cat test.py
#!/usr/bin/env python

print 'Content-type: text/html'
print

print '<html>'
print '<body>'

print 'Hello World directly from Python!'

print '</body>'
print '</html>'
$ chmod 755 test.py

Format Strings in Python

  • So, far we have used print with "," and "+" to construct strings
  • Python also provides a special "format" string that can be used as a template.
  • For example:
a = 1
name = 'Greg'

print 'a = %d and name = %s' % (a, name)

  • Multi-line strings:
$ cat multiline.py 
text = """
This is a
multi-line
string
"""

print text
$ python multiline.py 

This is a
multi-line
string

$

Simple HTML Form and Python CGI

  • You can create a simple HTML form that accepts input from the user.
  • Here is an example:
$ cat simpleform.py 
#!/usr/bin/env python
import cgi

print "Content-type: text/html"
print

form = cgi.FieldStorage()
first_name = form.getvalue('first_name', '')
last_name = form.getvalue('last_name', '')


print """
<html>
<body>
<form action='simpleform.py'>
First Name: <input type="text" name="first_name" />
<p>
Last Name: <input type="text" name="last_name" />
<p>
<input type='submit' />
<p>
Your name: %s %s
</form>
</body>
</html>""" % (first_name, last_name)
  • If you put this example in your web directory and point your web browser at it, e.g.:
    •  http://www.cs.usfca.edu/~<username>/cs110/week4/simpleform.py
  • It will ask for a first and last name.
  • When you press return or click on the "Submit" button, it will call the simpleform.py program again, but this time with the first and last name that was provided by the user.
  • Some things to notice:
    • We import cgi so that we can get at the user supplied input
    • In the HTML, we use a "form" and specify different input fields.
    • Notice that we need to match up the names with the Python variable names
    • We use a multi-line format string to print the user-supplied name.
  • You can learn more about HTML forms here:
Comments