Download presentation
Presentation is loading. Please wait.
Published byLynne Hawkins Modified over 9 years ago
1
Lecture # 29 Python III: Client Server
2
Motivation: How the Internet Works Static HTML Pages ApacheApache ApacheApache BrowserBrowser BrowserBrowser Client Server Request http://CS100.html Response CS100.html Display HTML files are text files CS100.html
3
Dynamic Web Pages ApacheApache ApacheApache BrowserBrowser BrowserBrowser Client Server CS100.html Request http://CS100.html Response CS100.html Display HTML files are text files CS100.cgi Motivation: How the Internet Works
4
Lab 9 Create Client Server Create & Set Up an HTML File 1 1
5
Lab 9 Create Client Server Create & Set Up an HTML File 1 1 Add CSS style or formatting 2 2
6
Lab 9 Create Client Server Create & Set Up an HTML File 1 1 Add CSS style or formatting 2 2 Add the python,.cgi to make it go 3 3
7
Lab 9 Create Client Server Hook it up Create & Set Up an HTML File 1 1 Add CSS style or formatting 2 2 Add the python,.cgi to make it go 3 3 4 4
8
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)
9
Lab 9: Step A Go to your account: The go to Public_html myServer Create folder convertExample
10
Lab 9: Steps B & C Copy the file “Convert_HTML_Only.html” into the folder and view the page source
11
Lab 9: Step D Copy the file “Convert_With_CSS.html” into the folder and view the page source
12
Lab 9: Step E Rename “Convert_With_CSS.html” to “Convert.html” Edit “Convert.html” as follows: Change 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%"
13
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 …. result = result.replace("%celsius%", celsiusValue) result = result.replace("%fahrenheit%", fahrenheitValue) print result
14
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.cgi
15
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.cgi Lab 9: Step G – Link to Homepage
16
Static and Dynamic Page Demo Hi there print #!/usr/bin/python print "Content-type: text\html" print print “”” Hi there “”” Static Web Page first.html Dynamic Web Page first.cgi
17
Lab 9: Part 2: Client Side
18
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 )
19
Lab 9: Part 2, Step 2 Create a CGI File that will display this HTML file. (See instruction in Lab Assignment )
20
Lab 9: Part 2, Step 3 Link it up. (See instruction in Lab Assignment ) Client Server Link it up My Family History.html MFH.cgi
21
Lab 9: Part 3: CSS
22
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 )
23
Testing Static and Dynamic Web Pages Create a directory called server –You can name it whatever you want Save the following file http://students.cs.byu.edu/~cs100ta/code/server.py in 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.htmlhttp://localhost:8000/test.html –http://localhost:8000/cgi-bin/first.cgihttp://localhost:8000/cgi-bin/first.cgi DEMO
24
More about Python
25
String Encodings ASCII –special characters: \n = Linefeed \r = Carriage return \t = Horizontal Tab Unicode –u”…..”
26
Accessing Substrings str[i] – getting the i th 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
27
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)
28
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
29
Examples Form Letter – Program_91.py (and command line)Program_91.py changeLittle Example – Program_92.py (and command line)Program_92.py –use sample.py as first parameter findSequence – Program_93.py (and command line)Program_93.py replaceAllOccurrences – Program_94.pyProgram_94.py –use sample.py again Web Scraping – Program_95.pyProgram_95.py titleDirectory – Program_96.pyProgram_96.py –setMediaPath to MediaSources random – Program_97.pyProgram_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
30
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
31
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)
32
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.