Title: Serverside Technology
1(No Transcript)
2Serverside Technology
- Transport Server
- BSD Sockets
- Dynamic Webserver
- Serverside Scripting, CMS
- Multitier Webserver
- Database Intro, SQL, GoogleAE
3Transport Server
4Remember Ports
- lookup table
- 216 entries per host
- each packet has
- a destination port
5Ports
6Services and Ports
- Server applications wait
- for incoming connections
- or packets
7Berkeley Sockets
- (bad) metaphore for digital connections
- passive sockets wait for requests
- active sockets exchange
- data with a remote active socket
8The Berkeley Socket API
- The most common transport-layer interface
- low-level
- connect and disconnect sockets
- send and receive data over a socket
- connectionless and connection-oriented
9Socket Programming Interface
- socket create a new socket
- bind bind to a local TCP/IP port
- listen allow incoming data on a port
- accept wait for data to arrive
- select wait for data on multiple sockets
- connect connect to a remote TCP/IP port
- send transmit data
- recv receive data (if any)?
- close close an existing socket
10socket()
- create a socket
- choose a transportation
- mode
?
11server bind()
- occupy a port
- as passive socket
- if not already taken!
12server listen()
13client connect()
- 'walk up to a desk'
- i.e., select a port
- ask to open a connection
- if connection-oriented
14server accept()
- wait a single
- connection request
- only if connection-oriented
- creates a new active socket on the server to
- handle the request.
15- Now a connection
- is established
- Both sides have
- an active socket
16send() and recv()
17send() and recv()
18close()
19Simple Server 1/2
- import socket
- import sys
- create a socket
- s socket.socket(socket.AF_INET,
socket.SOCK_STREAM)? - associate the socket with a hostport
combination - hostname can be empty on the serverside
- s.bind((, 80))?
20Simple Server 2/2
- s.listen(1)?
- conn, addr s.accept()?
- print client is at, addr
- read string from client
- data conn.recv(???)?
- conn.send(HTTP/1.0 200 OK ...)?
- conn.close()?
21Assignment 1.b Hint
- The telnet protocol is simpler, but similar
- Search for examples online!
22Serverside Technology
- Transport Server
- BSD Sockets
- Dynamic Webserver
- Serverside Scripting, CMS
- Multitier Webserver
- Database Intro, SQL, GoogleAE
23Transport Server Programming
Application Server Programming
24Application Server Programming
- Purpose
- templates, data-driven web, cms
- Overview
- Tools
- Programming
25Scripting 1 Templates
- Reuse HTML Code
- Within a page
- Throughout a site
- Consistency
- Rapid Development
26Scripting 2 Data-driven webpages
27Scripting 2 Data-driven webpages
28Scripting 2 Data-driven webpages
29Scripting 3 CMS
- Web-driven data management
- Content Management System
- Frontend and Backend processing
- Foolproof, location-independent
30Scripting 3 CMS
31Scripting 3 CMS
32Application Server
- Webserver
- Application (in Python, PHP, ...)
- Standard scripts
- Storage (Files, Database)?
- A CMS is an example of a web application server
- another would be an e-commerce site
33Application Server Programming
- Purpose
- Overview
- Tools
- Programming
34Dynamic Document Generation
- Combine static HTML
- and
- dynamic data
- to create a page
- on demand
- i.e., when a request comes in
35Simplified examples
- templates file.read()
- worldclock time.ctime()
- wiki socket.read()
36Many Serverside Languages
- CGI
- PHP
- Python
- Coldfusion
- Java JSP and Servlets
- MS Appliation Server Pages (ASP)?
37Two Broad Approaches
- Code inlining
- vs.
- Page generation
38Code Inlining
lthtmlgtltheadgtlttitlegtClockgtlt/titlegtlt/headgt ltbodygt ltp
gt The current time is lt?php echo
date('his') ?gt lt/pgt lt/bodygt lt/htmlgt
- examples PHP, Java Server Pages, ASP
39Page Generation
import time socket.write('''lthtmlgtltheadgtlttitlegtCl
ockgtlt/titlegtlt/headgt ltbodygt ltpgt The current time
is ''' time.ctime() ''' lt/bodygt lt/htmlgt''')
examples Python, CGI/Perl, Java Servlets
40Application Server Programming
- Purpose
- Overview
- Tools
- Sessions, Cookies, Authentication
- Programming
41Revisiting Sessions
42Revisiting Sessions
43Revisiting Cookies
- Server sends data to Client
- Client sends data in each request to Server
- If server changes operation based on data
- and changes data ...
- ... no longer stateless
44Cookie implementation
-
- GET /index.html HTTP/1.1
- Host www.example.org
-
- HTTP/1.1 200 OK
- Content-type text/html
- Set-Cookie namevalue
-
- (content of page)
- GET /spec.html HTTP/1.1
- Host www.example.org
- Cookie namevalue
- Accept /
Client
Server
Client
45(No Transcript)
46Cookie expiration
- Server can set an expiration date
- to save cookies across sessions
- Otherwise cookies are removed
- when the browser closes
- Set-Cookie keyvalue expiresFri, 31-Dec-2010
235959 GMT
47Cookie Scope
- Server can expand scope beyond single URL
- Scope governed by (key, domain, path)
- Set-Cookie keyvalue expiresFri, 31-Dec-2010
235959 GMT path/ domainjustthetext.appspot.c
om
48Cookie details
- maximally 4 kB per cookie
- at least 20 per domain allowed
- case insensitive
49Exercise Cookies and Sessions
- Write an application that prints a counter
- that increases each time a user visits the server
50Authentication
- User roles
- Don't use HTTP Authentication
- Encrypted transport and Forms
- (We'll skip https)
51Application Server Programming
- Purpose
- Overview
- Tools
- Programming
52Template Generation
- longname Homepage
- shortname index
- head open("header.html", "r")
- body open(shortname ".contents.html", "r")
- print "lthtmlgtltheadgtlttitlegt" longname
"lt/titlegtlt/headgtltbodygt" - print head.read()
- print body.read()
- head.close()
- body.close()
- OK. Return HTTP Code 200 (OK)
- return 200
53Data-driven webpages
- import BaseHTTPServer
- class myhandler(BaseHTTPServer.BaseHTTPRequestHand
ler) - def do_GET(self)
- self.send_response(200)
- self.send_header("Content-
type", "text/html") - self.end_headers()
- self.wfile.write("lthtmlgtlth
eadgtlttitlegtReslt/titlegtlt/headgt") - self.wfile.write("ltbodygtlth
1gtForm Resultslt/h1gtltpgt") - self.wfile.write(self.path
) - self.wfile.write("lt/pgtlt/bo
dygtlt/htmlgt") - port 8000
- server BaseHTTPServer.HTTPServer(("", port),
myhandler) - print "Serving data from port " str(port)
- server.serve_forever()
54Content Management System
55Serverside Technology
- Transport Server
- BSD Sockets
- Dynamic Webserver
- Serverside Scripting, CMS
- Multitier Webserver
- Database Intro, SQL, GoogleAE
56For the exam, remember the function and
organizationof databasesnot the SQL syntax
57Persistent Storage
- Needed for real applications
- HTTP is stateless
- Transferring large cookies?
- slow
- can be manipulated by user
58Separation of Concerns
- Presentation
- Application Logic
- Data Storage
- For clarity/reuse as well as load balancing
59Multi-tier Servers
- Presentation (browser)?
- Application (appserver)?
- Storage (database)?
- Compare Model-View-Controller
60Databases
- Can store data more efficienty (space/time)?
- Can retrieve data more intelligently
- Topic of another class
61Relational Databases
- Combine multiple sets of data
- Example ...
62Example
Students
Courses
Grades
The grade list is a relation of students and
courses
The grade list is a relation of students and
courses
63SQL
- Pronounce Sequel
- Information Retrieval
- Information Modification
- Information Definition
- statements are case insensitive
- (but value strings aren't)
64Information Retrieval
- Select something
- From somewhere
- Where some constraints hold
65Example
- Select name
- From students
66Example constraints
- Select year
- From students
- Where name 'Alice'
67Example relationships
- Select s.name, s.year
- From students s, grades g
- Where s.name g.student
- and
- g.grade lt 5
68Example order
- Select s.name, s.year
- From students s, grades g
- Where s.name g.student
- and
- g.grade lt 6
- Order by s.name ASC
69Note on relationships
- student.name and g.student both hold willem
- might become inconsistent (e.g., address)
- better is to add unique IDs
- students (id, name, year, address)
- and point to IDs
- grades(student, course)
70Example
- Select s.name, s.year
- From students s, grades g
- Where s.name g.student
- and
- g.grade lt 6
71Data Modification Language (DML)?
- INSERT
- INSERT INTO students VALUES (willem, 1997)
- UPDATE
- UPDATE grades SET grade10 WHERE
studentWillem - DELETE
- DELETE FROM courses WHERE studentWillem
72Data Definition Language (DDL)?
- CREATE TABLE
- CREATE TABLE dropouts (name varchar(255), year
int) - DROP TABLE
- DROP TABLE dropouts
73A lot more to say on this topic
74Serverside Technology
- Transport Server
- BSD Sockets
- Dynamic Webserver
- Serverside Scripting, CMS
- Multitier Webserver
- Database Intro, SQL, GoogleAE
75The remainder of this talk is practical
support,not exam material
76Google App Engine
- Server Basics
- GAE Overview
- GAE Use
77First a python webserver
- import BaseHTTPRequest
- two classes
- HTTPServer
- BaseHTTPRequestHandler
NB This was assignment 1.b optional code is now
available from data/python/webserver.py
78First a python webserver
- override the requesthandler's methods
- do_GET(self)
- do_POST(self)
- write output not using print, but using
- self.wfile.write()
79Exercises
- Using webserver.py, build a
- Clock page that tells the time
- Counter page that increases a per-user counter
- use cookies
- NB I DID NOT EXPLAIN THIS IN CLASS
- SEE NEW NEXT 2 SLIDES
- Multiple pages clock and counter
80Writing Cookies
- Set a cookie by adding a self.send_header() call
to do_GET - Each call adds an HTTP header to the response
- Make sure the header matches the Cookie syntax
- (shown for instance on slide 45)
- NB there is now a file cookie.py in
- Internet Programming 2009\data\python
81Reading Cookies
- Read a cookie using module Cookie
- Have python extract the HTTP request cookies as
string - cookiesstring self.headers.get('Cookie')
- If there are cookies, have them parsed into a
dictionary using - cookies Cookie.SimpleCookie(cookiesstring)
- mycookie will be a string (if the cookie
exists)
82A template-driven website
- Instead of writing plain text (or markup)
- read file contents
- Revisit slideset 3, with a file example
- headerfile open(header.html, r)
- headerdata headerfile.readlines()
- self.wfile.write(headerdata)
- headerfile.close()
code is available from data/python/template.py
83Exercises
- Using webserver.py and template.py,
- build a minimal template driven website
- then
- convert your static website into a template
driven version
84Working forms
- requires parsing the URL encoded parameters
- http//www.google.com.gh/search?qgoogleappengin
eurlparseieutf-8oeutf-8aqtrlscom.ubuntue
n-USunofficialclientfirefox-a - Not simple in Movable IDLE
- because of older Python version (2.4)
- Will defer until we move to GAE
85Google App Engine
- Server Basics
- GAE Overview
- GAE Use
86Google App Engine
87What is Google App Engine?
- Google App Engine lets you run your web
applications on Google's infrastructure. App
Engine applications are easy to build, easy to
maintain, and easy to scale as your traffic and
data storage needs grow. With App Engine, there
are no servers to maintain You just upload your
application, and it's ready to serve your users.
code.google.com/appengine/docs/whatisgoogleappengi
ne.html
88Cloud Computing
Your Program
Python
89What is Google App Engine?
- Google App Engine lets you run your web
applications on Google's infrastructure. App
Engine applications are easy to build, easy to
maintain, and easy to scale as your traffic and
data storage needs grow. With App Engine, there
are no servers to maintain You just upload your
application, and it's ready to serve your users.
code.google.com/appengine/docs/whatisgoogleappengi
ne.html
90Cloud Computing
- A form of hosting
- Pro
- Scalable
- Fault tolerant
- Vendor APIs
- Low (maintenance) cost
- Against
- Data privacy concerns
- Vendor Lock-in
en.wikipedia.org/wiki/Cloud_computing
91Cloud Computing Providers
- Amazon Web Services
- low-level services storage, computation
- Google App Engine
- high-level services image manipulation
- Others (HP, Microsoft) are preparing offerings
92Documentation
- code.google.com/appengine/docs
- overview
- tutorial
- reference
- FAQ
- articles
- samples
- ....
USE IT!
93GAE Restrictions
- Sandbox
- No file access
- Limited amount of work
- No full SQL, but 'Google Query Language'
94Persistent Storage
- Google Query Language (QGL)
- Similar to SQL, but restricted
- no concept of tables or relationships
- i.e., no select from a, b
- only test for inequality on one property
- if sorting must sort on this property first
(huh?)
95Google App Engine
- Server Basics
- GAE Overview
- GAE Use
96Again, all that comes is explained in more
detailatcode.google.com/appengine/docs
97Step 1 Sign up
98Step 2 Register Project
- Log in and go to appengine.google.com
99Step 2 Register Project
100Step 2 Register Project
- Google will send you an SMS on your first project
- If you don't want this, use my account instead
101Step 3 Install Python
102Create Project
- In Internet Programming 2009\python\google_appengi
ne - create subdirectory for your project
- in Internet Programming 2009\python\google_appengi
ne\ltprojectgt - create file app.yaml
- application ltprojectgt
- version 1
- runtime python
- api_version 1
- handlers
- - url /.
- script helloworld.py
103helloworld.py
print 'Content-Type text/plain' print '' print
'Hello, world!'
- next, change it to output a valid html page
104Step 4 Development server
- Runs locally, at port 8080
- Start using
- run_project.bat in google_appengine
- copy to run_ltmyprojectgt.bat) and edit
-
105Step 4 Development server
- shared!
- keep private backup of your files
106Putting your website online
- modify app.yaml to serve static files from
...\ltprojectgt\staticfiles - application ltprojectgt
- version 1
- runtime python
- api_version 1
- handlers
- - url /staticfiles
- static_dir staticfiles
- - url /.
- script helloworld.py
107Putting your website online
- create directory ...\ltprojectgt\staticfiles
- copy all your static files into this directory
- verify that they show up on the development
webserver - if you're happy, upload to your domain at
ltprojectgt.appspot.com
108Step 5 Upload website
- Test locally and upload when there are no bugs
- Can overwrite previous versions
- Use
- appcfg.py --emailltgoogleemailgt update ltprojectgt
- Better
- copy to upload_project.bat and edit
- more info code.google.com/appengine/docs/appcfgpy
.html
109A slightly different webserver
- No BaseHTTPServer.HTTPServer,
- but webapp.WSGIApplication
- Concept is the same create a server object,
which - will call your handler objects
- Difference individual objects for individual
files
more info at http//code.google.com/appengine/docs
/webapp/running.html
110Webserver code in your app
- from google.appengine.ext import webapp
- from google.appengine.ext.webapp.util import
run_wsgi_app - application webapp.WSGIApplication(('/',
MainPage), -
('/contact', Contact), -
('/editentry', EditEntry), -
('/deleteentry', DeleteEntry), - ,
- debugTrue)
- def main()
- run_wsgi_app(application)
111Mapping requests to handlers
- You attach a path to a handler ('/', MainPage)
- Can attach multiple paths to the same handler
- ('/', MainPage), ('/other', MainPage)
- and find out which path used in the object
112Writing a requesthandler
class myreqhandler(webapp.RequestHandler) def
get(self) self.response.out.write("lthtmlgtltbody
gtltpgtHi there!lt/pgtlt/bodygtlt/htmlgt")
- can override get, post, ... methods
- can generate output using self.response.out.write
- can inspect request using self.request
- code.google.com/appengine/docs/webapp/requesthandl
ers.html - code.google.com/appengine/docs/webapp/requesthandl
erclass.html
113Accessing request data
- value request.get(key)
- class myreqhandler(webapp.RequestHandler)
- def get(self)
- self.response.out.write("lthtmlgtltbodygtltpgtHi )
- self.response.out.write(self.request.get(name
)) - self.response.out.write(lt/pgtlt/bodygtlt/htmlgt")
- code.google.com/appengine/docs/webapp/requesthandl
erclass.html
114Forms
- Read form results using request.get(ltkeygt)
- code.google.com/appengine/docs/gettingstarted/hand
lingforms.html
115Mapping requests to handlers (2)
- Can also map unlimited paths at once
- (r'/browse/(.)/(.)', BrowseHandler)
Wildcard arbitrary item
Seen by Browsehandler as arguments class
BrowseHandler(webapp.RequestHandler) def
get(self, category, product_id)
116Templates
- Cannot store snippets in files
- Store snippets in data objects instead
117A snippet object
- class snippet(db.Model)
- name db.StringProperty()
- content db.StringProperty(multilineTrue)
- headersnippet snippet()
- headersnippet.name header
- headersnippet.content lthtmlgtltheadgt ...
- headersnippet.put()
- query db.GqlQuery(
- SELECT FROM snippet WHERE nameheader)
- headersnippet query.get()
- headersnippets query.fetch(10)
118References
- key snippet.put()
- snippet db.get(key)
119Deleting objects
- snippets db.GqlQuery(...)
- db.delete(snippets)
120Clearing the datastore
- development server
- "dev_appserver.py --clear_datastore ltprojectgt"
- at appspot.com?
121Exercise
- change your template driven
- website to use objects
- i.e., make it work on GAE