google application engine introduction jim eng with thanks to charles severance :

47
Introduction Jim Eng with thanks to Charles Severance http://www.appenginelearn.com / http://www.dr-chuck.com/

Upload: victor-byrd

Post on 14-Dec-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

Google Application Engine

Introduction

Jim Eng

with thanks to Charles Severance

http://www.appenginelearn.com/http://www.dr-chuck.com/

Cloud Computinghttp://en.wikipedia.org/wiki/Cloud_computing

LocalNetwork

Pre-Cloud View

The Internet

...

Router

HardwareHardware

SoftwareSoftware

ctools.umich.edu

In a pre-cloud view servers have a

geographic location and we use the

Internet to exchange data with those

servers.

World-Scale Applications

•For world-scale applications - the servers must be distributed around the world

•But users must see a uniform “single image” - www.google.com

•Also the programmers cannot know the structure or grography of the servers - because this always changes

Google Server Locations

http://royal.pingdom.com/2008/04/11/map-of-all-google-data-center-locations/

This is an educated guess.

Programming in the Cloud

•Programmers operate in a controlled environment

•Programs do their programming thing - code + data

•A complex software framework manages getting the right code and data to/from the right servers.

•Software developers are unaware of geography

The geographic location no longer

matters - the software “runs

somewhere in the cloud”.

Resources can be dynamically adjusted as load changes.

LocalNetwork

Pre-Cloud View

The Internet

...

Router

SoftwareSoftware

Server Server Busy?Busy?

Post-Cloud ViewThe Cloud

...

My My CodeCode

My My CodeCode

Your Your CodeCode

My User

My User

Your User

HTTP - Request / Response

•The nature of the HTTP Request/Response cycle makes the cloud possible

•Since clients are not connected for very long - the cloud can be changed in between requests

•As long as the cloud “fakes” everything about the protocol - no one is the wiser..

•The cloud engineers at Google/Amazon/Yahoo are pretty clever.

HTTP Request / Response Cycle

http://www.oreilly.com/openbook/cgi/ch04_02.html

Browser

Web Server

HTTPRequest

HTTPResponse

Internet Explorer, FireFox, Safari,

etc.

HTTP Request / Response Cycle

http://www.oreilly.com/openbook/cgi/ch04_02.html

Browser

Web Server

HTTPRequest

HTTPResponse

Internet Explorer, FireFox, Safari,

etc.

GET /index.html

<head> .. </head><body><h1>Welcome to my application</h1> ....</body>

Post-Cloud ViewThe Cloud

My My CodeCode

My My CodeCode

Your Your CodeCode

My User

Post-Cloud ViewThe Cloud

My My CodeCode

My My CodeCode

Your Your CodeCode

My User

GETGET

Cloud Summary

•The cloud is the Internet plus computing that is “embedded” “inside” the network

•Companies like Google, Amazon, and Yahoo put servers all over the world

•Software runs on whichever server is most appropriate and data/code is moved around and the cloud can be reconfigured dynamically

Materials

•Google Application Engine

•Free hosted web services using Python

•http://code.google.com/appengine/

•We will be using web materials and making materials as we go.

Overview Video

•Builds a Google App Engine application in 10 minutes

•Basic structure - GET / POST

•Form Input

•Templating

•Database

http://www.youtube.com/watch?v=tcbpTQXNwac

Google App Engine

Google App Engine

•Expose Google’s worldwide Infrastructure to us as developers

http://www.youtube.com/watch?v=3Ztr-HhWX1chttp://www.youtube.com/watch?v=tcbpTQXNwac

Google App Engine•When you write a Google Application Engine

Application - you are running in the Google Cloud

• Just like you were a Google Developer

•You don’t know where you are running or if one copy of a thousand copies of you are running

•Google hosts small applications for *free* - larger applications pay by usage

Free Accounts• A free account

can use up to 500MB of persistent storage and enough CPU and bandwidth for about 5 million page views a month.

Installing Google App Engine

http://code.google.com/appengine/downloads.html

Your First Web Application

app.yaml - Tells Google about the applicationindex.py - the code for our application

File -> Add Existing ApplicationPick Your Directory

/Users/csev/a539/apps/ae-01-trivial

Select your new application and press “Run”. When it has a little green symbol

- then press “Browse”.

Yay!

Remember to ignore files with tildes (~)or

JEdit -> Utilities -> Global Options -> Autosave & Backupand set the number of backups to zero

A Second ApplicationDumping Input data

Dumper•This application makes a form and then we

submit the form via POST

•This application dumps the input variables that come in from the HTTP Request

•This is the most basic view of a web application

• It is like a Dinosaur - it is a web program written using the “old ways”

Common Gateway Interface

•A set of rules about taking input parameters and data from an incoming HTTP request and handing them to the program.

HTTP Request / Response Cycle

http://www.oreilly.com/openbook/cgi/ch04_02.html

Browser

Web Server

HTTPRequest

HTTPResponse

HTTP Request / Response Cycle

GET /index.htmlAccept: www/sourceAccept: text/htmlUser-Agent: Lynx/2.4 libwww/2.14

http://www.oreilly.com/openbook/cgi/ch04_02.html

Browser

Web Server

HTTPRequest

HTTPResponse

<head> .. </head><body><h1>Welcome to my application</h1> ....</body>

Passing Parameters to The ServerGET /simpleform.html?

yourname=fredAccept: www/sourceAccept: text/htmlUser-Agent: Lynx/2.4 libwww/2.14

POST /cgi-bin/program.pl HTTP/1.0Accept: www/sourceAccept: text/htmlUser-Agent: Lynx/2.4 libwww/2.14Content-type: application/x-www-form-urlencodedContent-length: 13yourname=fred

HTTPRequest

Browser

Web Server

<input type="text" name="yourname" id="yourid" />

http://hoohoo.ncsa.uiuc.edu/cgi/in.html

CGI In App Engine

•When a Request is received in the App Engine, according to the rules of CGI

•The environment variables such as server name, document path, etc come in a dictionary object

•Any POST data comes in on standard input (sys.stdin)

•Whatever the program prints goes to the browser as the HTTP response

<form method="post" action="/">Zap Data: <input type="text" name="zap"><br>Zot Data: <input type="text" name="zap"><br><input type="submit"></form>

EnvironmentData

POST Datazap=Stuff&zot=MoreStuff

dict()

stdin

<form method= ....></form>

print

CGIProgram

BBrroowwsseerr

import osimport sys

print 'Content-Type: text/html'print ' 'print '<form method="post" action="/">'print 'Zap Data: <input type="text" name="zap"><br>'print 'Zot Data: <input type="zot" name="zap"><br>'print '<input type="submit">'print ‘</form>’

The standard output (print statements) produce the HTTP

Response.

The output consists of HTTP headers followed

by the body of the document.

print 'Environment keys:'print ' 'for param in os.environ.keys(): print param, ":", os.environ[param]print ' '

print 'Data'count = 0for line in sys.stdin: count = count + 1 print line if count > 100: breakprint ‘</pre>’

Up Next...• If we wanted to write a real web application - we

would write a lot of string parsing code that looked at the environment variables and standard input

•We would spend a lot of time reading the HTTP documents to figure out the exact format of these strings

•This has already been done for us - this is provided as a framework on App Engine

http://code.google.com/appengine/docs/gettingstarted/usingwebapp.html

Summary•We introduced Cloud Computing - servers move

“into” the network cloud

•Google App Engine allows us to use the Google Cloud

•Common Gateway Interface - The ancient ways

•Writing simple low-level Python programs in AppEngine

•Using the AppEngine web framework (up next)