Serverside Technology - PowerPoint PPT Presentation

1 / 121
About This Presentation
Title:

Serverside Technology

Description:

data with a remote active socket. The Berkeley Socket API ... Pronounce 'Sequel' Information Retrieval. Information Modification. Information Definition ... – PowerPoint PPT presentation

Number of Views:68
Avg rating:3.0/5.0
Slides: 122
Provided by: netstre
Category:

less

Transcript and Presenter's Notes

Title: Serverside Technology


1
(No Transcript)
2
Serverside Technology
  • Transport Server
  • BSD Sockets
  • Dynamic Webserver
  • Serverside Scripting, CMS
  • Multitier Webserver
  • Database Intro, SQL, GoogleAE

3
Transport Server
4
Remember Ports
  • lookup table
  • 216 entries per host
  • each packet has
  • a destination port

5
Ports
  • Defaults
  • smtp is 25

6
Services and Ports
  • Server applications wait
  • for incoming connections
  • or packets

7
Berkeley Sockets
  • (bad) metaphore for digital connections
  • passive sockets wait for requests
  • active sockets exchange
  • data with a remote active socket

8
The 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

9
Socket 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

10
socket()
  • create a socket
  • choose a transportation
  • mode

?
11
server bind()
  • occupy a port
  • as passive socket
  • if not already taken!

12
server listen()
  • start accepting
  • clients

13
client connect()
  • 'walk up to a desk'
  • i.e., select a port
  • ask to open a connection
  • if connection-oriented

14
server 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

16
send() and recv()
17
send() and recv()
18
close()
19
Simple 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))?

20
Simple 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()?

21
Assignment 1.b Hint
  • The telnet protocol is simpler, but similar
  • Search for examples online!

22
Serverside Technology
  • Transport Server
  • BSD Sockets
  • Dynamic Webserver
  • Serverside Scripting, CMS
  • Multitier Webserver
  • Database Intro, SQL, GoogleAE

23
Transport Server Programming
Application Server Programming
24
Application Server Programming
  • Purpose
  • templates, data-driven web, cms
  • Overview
  • Tools
  • Programming

25
Scripting 1 Templates
  • Reuse HTML Code
  • Within a page
  • Throughout a site
  • Consistency
  • Rapid Development

26
Scripting 2 Data-driven webpages
27
Scripting 2 Data-driven webpages
28
Scripting 2 Data-driven webpages
29
Scripting 3 CMS
  • Web-driven data management
  • Content Management System
  • Frontend and Backend processing
  • Foolproof, location-independent

30
Scripting 3 CMS
31
Scripting 3 CMS
32
Application 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

33
Application Server Programming
  • Purpose
  • Overview
  • Tools
  • Programming

34
Dynamic Document Generation
  • Combine static HTML
  • and
  • dynamic data
  • to create a page
  • on demand
  • i.e., when a request comes in

35
Simplified examples
  • templates file.read()
  • worldclock time.ctime()
  • wiki socket.read()

36
Many Serverside Languages
  • CGI
  • PHP
  • Python
  • Coldfusion
  • Java JSP and Servlets
  • MS Appliation Server Pages (ASP)?

37
Two Broad Approaches
  • Code inlining
  • vs.
  • Page generation

38
Code 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

39
Page 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
40
Application Server Programming
  • Purpose
  • Overview
  • Tools
  • Sessions, Cookies, Authentication
  • Programming

41
Revisiting Sessions
42
Revisiting Sessions
43
Revisiting 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

44
Cookie 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)
46
Cookie 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

47
Cookie 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

48
Cookie details
  • maximally 4 kB per cookie
  • at least 20 per domain allowed
  • case insensitive

49
Exercise Cookies and Sessions
  • Write an application that prints a counter
  • that increases each time a user visits the server

50
Authentication
  • User roles
  • Don't use HTTP Authentication
  • Encrypted transport and Forms
  • (We'll skip https)

51
Application Server Programming
  • Purpose
  • Overview
  • Tools
  • Programming

52
Template 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

53
Data-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()

54
Content Management System
  • ...

55
Serverside Technology
  • Transport Server
  • BSD Sockets
  • Dynamic Webserver
  • Serverside Scripting, CMS
  • Multitier Webserver
  • Database Intro, SQL, GoogleAE

56
For the exam, remember the function and
organizationof databasesnot the SQL syntax
57
Persistent Storage
  • Needed for real applications
  • HTTP is stateless
  • Transferring large cookies?
  • slow
  • can be manipulated by user

58
Separation of Concerns
  • Presentation
  • Application Logic
  • Data Storage
  • For clarity/reuse as well as load balancing

59
Multi-tier Servers
  • Presentation (browser)?
  • Application (appserver)?
  • Storage (database)?
  • Compare Model-View-Controller

60
Databases
  • Can store data more efficienty (space/time)?
  • Can retrieve data more intelligently
  • Topic of another class

61
Relational Databases
  • Combine multiple sets of data
  • Example ...

62
Example
Students
Courses
Grades
The grade list is a relation of students and
courses
The grade list is a relation of students and
courses
63
SQL
  • Pronounce Sequel
  • Information Retrieval
  • Information Modification
  • Information Definition
  • statements are case insensitive
  • (but value strings aren't)

64
Information Retrieval
  • Select something
  • From somewhere
  • Where some constraints hold

65
Example
  • Select name
  • From students

66
Example constraints
  • Select year
  • From students
  • Where name 'Alice'

67
Example relationships
  • Select s.name, s.year
  • From students s, grades g
  • Where s.name g.student
  • and
  • g.grade lt 5

68
Example 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

69
Note 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)

70
Example
  • Select s.name, s.year
  • From students s, grades g
  • Where s.name g.student
  • and
  • g.grade lt 6

71
Data Modification Language (DML)?
  • INSERT
  • INSERT INTO students VALUES (willem, 1997)
  • UPDATE
  • UPDATE grades SET grade10 WHERE
    studentWillem
  • DELETE
  • DELETE FROM courses WHERE studentWillem

72
Data Definition Language (DDL)?
  • CREATE TABLE
  • CREATE TABLE dropouts (name varchar(255), year
    int)
  • DROP TABLE
  • DROP TABLE dropouts

73
A lot more to say on this topic
  • Separate course

74
Serverside Technology
  • Transport Server
  • BSD Sockets
  • Dynamic Webserver
  • Serverside Scripting, CMS
  • Multitier Webserver
  • Database Intro, SQL, GoogleAE

75
The remainder of this talk is practical
support,not exam material
76
Google App Engine
  • Server Basics
  • GAE Overview
  • GAE Use

77
First 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
78
First a python webserver
  • override the requesthandler's methods
  • do_GET(self)
  • do_POST(self)
  • write output not using print, but using
  • self.wfile.write()

79
Exercises
  • 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

80
Writing 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

81
Reading 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)

82
A 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
83
Exercises
  • Using webserver.py and template.py,
  • build a minimal template driven website
  • then
  • convert your static website into a template
    driven version

84
Working 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

85
Google App Engine
  • Server Basics
  • GAE Overview
  • GAE Use

86
Google App Engine
87
What 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
88
Cloud Computing
Your Program
Python
89
What 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
90
Cloud 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
91
Cloud Computing Providers
  • Amazon Web Services
  • low-level services storage, computation
  • Google App Engine
  • high-level services image manipulation
  • Others (HP, Microsoft) are preparing offerings

92
Documentation
  • code.google.com/appengine/docs
  • overview
  • tutorial
  • reference
  • FAQ
  • articles
  • samples
  • ....

USE IT!
93
GAE Restrictions
  • Sandbox
  • No file access
  • Limited amount of work
  • No full SQL, but 'Google Query Language'

94
Persistent 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?)

95
Google App Engine
  • Server Basics
  • GAE Overview
  • GAE Use

96
Again, all that comes is explained in more
detailatcode.google.com/appengine/docs
97
Step 1 Sign up
  • Get a google account

98
Step 2 Register Project
  • Log in and go to appengine.google.com

99
Step 2 Register Project
100
Step 2 Register Project
  • Google will send you an SMS on your first project
  • If you don't want this, use my account instead

101
Step 3 Install Python
  • why?

102
Create 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

103
helloworld.py
print 'Content-Type text/plain' print '' print
'Hello, world!'
  • next, change it to output a valid html page

104
Step 4 Development server
  • Runs locally, at port 8080
  • Start using
  • run_project.bat in google_appengine
  • copy to run_ltmyprojectgt.bat) and edit

105
Step 4 Development server
  • shared!
  • keep private backup of your files

106
Putting 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

107
Putting 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

108
Step 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

109
A 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
110
Webserver 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)

111
Mapping 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

112
Writing 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

113
Accessing 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

114
Forms
  • Read form results using request.get(ltkeygt)
  • code.google.com/appengine/docs/gettingstarted/hand
    lingforms.html

115
Mapping 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)
116
Templates
  • Cannot store snippets in files
  • Store snippets in data objects instead

117
A 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)

118
References
  • key snippet.put()
  • snippet db.get(key)

119
Deleting objects
  • snippets db.GqlQuery(...)
  • db.delete(snippets)

120
Clearing the datastore
  • development server
  • "dev_appserver.py --clear_datastore ltprojectgt"
  • at appspot.com?

121
Exercise
  • change your template driven
  • website to use objects
  • i.e., make it work on GAE
Write a Comment
User Comments (0)
About PowerShow.com