lecture # 29 python iii: client server. motivation: how the internet works static html pages...
TRANSCRIPT
Lecture # 29
Python III: Client Server
Motivation:How the Internet Works
Static HTML Pages
Apache
Apache
Browser
Browser
Client ServerRequest
http://CS100.html
ResponseCS100.html
DisplayDisplay
HTML files are text files
CS100.html
Dynamic Web Pages
Apache
Apache
Browser
Browser
Client Server
CS100.html
Requesthttp://CS100.html
ResponseCS100.html
DisplayDisplay
HTML files are text files
CS100.cgiCS100.cgi
Motivation:How the Internet Works
Lab 9
Create
Client Server
Create & Set Up an HTML File
Create & Set Up an HTML File
11
Lab 9
Create
Client Server
Create & Set Up an HTML File
Create & Set Up an HTML File
11
Add CSS style or formatting
Add CSS style or formatting
22
Lab 9
Create
Client Server
Create & Set Up an HTML File
Create & Set Up an HTML File
11
Add CSS style or formatting
Add CSS style or formatting
22
Add the python, .cgi to
make it go
Add the python, .cgi to
make it go
33
Lab 9
Create
Client Server
Hook it upCreate & Set Up an HTML File
Create & Set Up an HTML File
11
Add CSS style or formatting
Add CSS style or formatting
22
Add the python, .cgi to
make it go
Add the python, .cgi to
make it go
3344
Lab 9: Part 1: Server Side
1. Set up an HTML file
2. Add “style” or formatting (CSS)
3. Add the python (.cgi) to make it go (Server 1 and Server 2)
Lab 9: Step A
• Go to your account:
• The go toPublic_html
myServer Create folder convertExample
Lab 9: Steps B & C
• Copy the file “Convert_HTML_Only.html”
into the folder and view the page source
Lab 9: Step D
• Copy the file “Convert_With_CSS.html”
into the folder and view the page source
Lab 9: Step E• Rename “Convert_With_CSS.html” to “Convert.html”
• Edit “Convert.html” as follows:
Change <form name="demo" action=""> to <form name="demo" action=
"http://students.cs.byu.edu/~xxxxxx/myServer/Convert.cgi">
where xxxxxx = your account id
• Change "blankFahrenheit()" value=""
to "blankFahrenheit()" value="%celsius%"
and "blankCelsius()" value=""
to "blankCelsius()" value="%fahrenheit%"
Lab 9: Step F, Part I – Create the Convert Program “Convert.cgi” in Python
#!/usr/bin/env python
import cgi, cgitb
cgitb.enable()
def celsiusToFahrenheit(celsius):
fahrenheit = celsius * 9.0 / 5.0 + 32.0
result = round(fahrenheit, 0)
return result
…. <See Lab Assignment #9 web page for all of the .cgi program>
….
result = result.replace("%celsius%", celsiusValue)
result = result.replace("%fahrenheit%", fahrenheitValue)
print result
Lab 9: Step F, Part II – Copy “Convert.cgi” onto the Unix Server and make it Executable
•See instructions on the assignment web page
•You should now have the following set up:
Client Server
Convert.html(with the CSS
formatting)
Convert.html(with the CSS
formatting)Convert.cgiConvert.cgi
•Make sure that this part of the lab can be viewed from
http://students.cs.byu.edu/~yourUsername.
Client Server
Hook it up
Convert.html(with the CSS
formatting)
Convert.html(with the CSS
formatting)Convert.cgiConvert.cgi
Lab 9: Step G – Link to Homepage
Static and Dynamic Page Demo
<html> <head> </head>
<body> Hi there </body></html>
print #!/usr/bin/pythonprint "Content-type: text\html"printprint “””<html> <head> <head>
<body> Hi there <body><html>“””
Static Web Pagefirst.html
Dynamic Web Pagefirst.cgi
Lab 9: Part 2: Client Side
Lab 9: Part 2, Step 1
Create a First Version of the HTML File for the "My Family History" Web Page. (See instruction in Lab Assignment)
Lab 9: Part 2, Step 2
Create a CGI File that will display this HTML file. (See instruction in Lab Assignment)
Lab 9: Part 2, Step 3
Link it up. (See instruction in Lab Assignment)
Client Server
Link it up
My Family History.htmlMy Family
History.htmlMFH.cgiMFH.cgi
Lab 9: Part 3: CSS
Lab 9: Part 3
We are now going to add style information to the html file so that the final product will look like this (See instruction in Lab Assignment)
Testing Static andDynamic Web Pages
• Create a directory called server– You can name it whatever you want
• Save the following filehttp://students.cs.byu.edu/~cs100ta/code/server.pyin the directory called server
• include any files you want to test in the same directory as server.py (or in a subdirectory)
• make a directory “cgi-bin” in the same directory as “server.py”– Include any cgi files in this directory that you want to test
• execute “python server.py”• On the command line of your browser invoke your test files as
follows:– http://localhost:8000/test.html– http://localhost:8000/cgi-bin/first.cgi
• DEMO
More about Python
String Encodings
• ASCII– special characters:
\n = Linefeed
\r = Carriage return
\t = Horizontal Tab
• Unicode– u”…..”
Accessing Substrings
• str[i] – getting the ith character in str• len(str) – returns the length of the str• slices
– str[n:m] – str[n] through str[m-1]– str[:m] – str[0] through str[m-1]– str[n:] – str[n] through str[len(str)-1]– str[:] – str[0] through str[len(str)-1]
• The entire string– str[-1:] – str[len(str)-1] through str[len(str)-1]
• A sequence with 1 character, the last character– str[:-1] – str[0] through str[len(str)-2]
• Demo
String Methods• String methods
– capitalize() – only the first word in the string
– startswith(prefix) – returns 1 or 0
– endswith(suffix)
– find(str), find(str, start), find(str, start, end)
• returns -1 if string not found
– rfind variant
– upper(), lower(), swapcase()
• String methods– isalpha()
– isdigit()
– replace(string, replacement)
Importing Modules
• Decomposing a program into parts or modules• Module is a python file
– Ends in “.py”• Python file contains methods, variables, and classes• Forms of import
– import file_name• Assumes file_name ends in “.py”• Must use full path name to access member of module
– from file_name import *– from file_name import x, y, z– import x as y
Examples
• Form Letter – Program_91.py (and command line)• changeLittle Example – Program_92.py (and command line)
– use sample.py as first parameter• findSequence – Program_93.py (and command line)• replaceAllOccurrences – Program_94.py
– use sample.py again• Web Scraping – Program_95.py• titleDirectory – Program_96.py
– setMediaPath to MediaSources• random – Program_97.py• random sentences
– Execution of a module from command line• Program_97a.py• Program_97b.py
– Including a main routine• import97a.py• import97b.py
Built in Python Modules(The Python library)
• Often used modules– os, sys, random– datetime, calendar– calendar– math– zipfile– email– SimpleHTTPServer
• Why use modules– Save work, why reinvent the wheel?– Higher quality
• Fewer bugs• Run faster• Well documented
Lists
• Literals– a = [1, 2, 3, 4]
• Access– a[i]
• for loops– for i in a:
• Concatenation– “+” operator
• Lists of lists– access – a[i][j]
• Methods– append(element)– remove(something)– sort()– reverse()– count()– split(delimeter)
• strings to lists of strings
• Functions– max(list)– min(list)
Files
• List of bytes• Name• Suffix
– Type of information in file• Folder or Directory Structure
– Trees• Trees as lists of lists of lists of …
– Traversing a tree with dot notation• From root
– Traversing domain names• students.cs.byu.edu