320 likes | 446 Vues
This lecture explores the fundamentals of client-server architecture using Python, focusing on how the internet works with static and dynamic HTML pages. Students will learn to set up an Apache server, create HTML files, and utilize Python CGI scripts to generate dynamic web content. The lab includes hands-on experience with creating and linking HTML pages, using CSS for styling, and developing a simple conversion program with input forms. By the end, students will have a foundational understanding of server-client interactions and web development.
E N D
Lecture # 29 Python III: Client Server
Motivation:How the Internet Works Static HTML Pages Apache Server Client Request http://CS100.html Browser Display CS100.html Response CS100.html HTML files are text files
Motivation:How the Internet Works Dynamic Web Pages Apache Server Client Request http://CS100.html CS100.cgi Browser Display Creates Response CS100.html CS100.html HTML files are text files
Lab 9 Create Server Client 1 Create & Set Up an HTML File
Lab 9 Create Server Client 1 Create & Set Up an HTML File 2 Add CSS style or formatting
Lab 9 Create Server Client 1 Create & Set Up an HTML File 3 Add the python, .cgi to make it go 2 Add CSS style or formatting
Lab 9 Create Server Client Hook it up 1 Create & Set Up an HTML File 3 Add the python, .cgi to make it go 4 2 Add CSS style or formatting
Lab 9: Part 1: Server Side • Set up an HTML file • Add “style” or formatting (CSS) • Add the python (.cgi) to make it go (Server 1 and Server 2)
Lab 9: Step A • Go to your account: • The go to Public_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: Server Client Convert.html (with the CSS formatting) Convert.cgi
Lab 9: Step G – Link to Homepage • Make sure that this part of the lab can be viewed from http://students.cs.byu.edu/~yourUsername. Server Client Hook it up Convert.html (with the CSS formatting) Convert.cgi
Static and Dynamic Page Demo Static Web Page first.html Dynamic Web Page first.cgi <html> <head> </head> <body> Hi there </body> </html> print #!/usr/bin/python print "Content-type: text\html" print print “”” <html> <head> <head> <body> Hi there <body> <html> “””
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) Server Client Link it up My Family History.html MFH.cgi
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 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.html • http://localhost:8000/cgi-bin/first.cgi • DEMO
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