1 / 22

lecture14 Programming with Google App Engine

lecture14 Programming with Google App Engine. Keke Chen. Outline. Using google app engine - walk through a simple example. How cgi works. Request: request a url, click a button, etc. Client. server. Request a url: “get” url Response: CGI returns some html code

atara
Télécharger la présentation

lecture14 Programming with Google App Engine

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. lecture14 Programming with Google App Engine • Keke Chen

  2. Outline Using google app engine - walk through a simple example

  3. How cgi works Request: request a url, click a button, etc Client server Request a url: “get” url Response: CGI returns some html code Click buttons: “post” something Request : content/url sent by the client Response: content return to the client’s browser

  4. Python “hello world” CGI program helloworld.py print 'Content-Type: text/plain'print ''print 'Hello, world!'

  5. Preparation • Have your gmail account • Installing google apps tool • dev_appserver.py, the development web server • appcfg.py, for uploading your app to App Engine

  6. Setup the development env Windows/mac: check http://code.google.com/appengine/downloads.html to find the package Nimbus17: already installed at /usr/local/gae Python version needs 2.5: /usr/bin/python2.5

  7. Webapp does • Wraps the CGI functions • API for handling request/response • Python Web Server Gateway Interface (WSGI) • A framework for developing CGI programs with Python

  8. from google.appengine.ext importwebappfrom google.appengine.ext.webapp.util importrun_wsgi_appclassMainPage(webapp.RequestHandler):defget(self):self.response.headers['Content-Type']='text/plain'self.response.out.write('Hello, webapp World!')application = webapp.WSGIApplication([('/',MainPage)],debug=True)def main(): run_wsgi_app(application)if __name__ =="__main__": main()

  9. Configuration file • app.yaml • A standard file format for configuration • A set of API designed to access the configuration • http://www.yaml.org/ • Don’t need to know much about yaml • Check GAE’s typical configurations

  10. App.yaml If threadsafe is set to true, use helloworld.application application: helloworld version: 1 runtime: python27 threadsafe: false api_version: 1 handlers: - url: /.* script: helloworld.py

  11. Start development server • Use python 2.5 /usr/bin/python2.5 /usr/local/gae/dev_appserver.py helloworld/ -p port -a address

  12. Use users service Use the google account service

  13. Using users service user = users.get_current_user() if user:      self.response.headers['Content-Type'] = 'text/plain'       self.response.out.write('Hello, ' + user.nickname()) else:       self.redirect(users.create_login_url(self.request.uri))

  14. Handling forms with webapp self.response.out.write("""          <html>            <body>              <form action="/sign" method="post">                <div><textarea name="content" rows="3" cols="60"></textarea></div>                <div><input type="submit" value="Sign Guestbook"></div>              </form>            </body>          </html>""")

  15. db - datastore db.Model GQL: sql like query language greetings = db.GqlQuery("SELECT * FROM Greeting ORDER BY date DESC LIMIT 10") Or omit select *: greetings = Greeting.gql("ORDER BY date DESC LIMIT 10") With parameters: greetings = Greeting.gql("WHERE author = :1 ORDER BY date DESC",users.get_current_user()) greetings = Greeting.all()        greetings.filter("author =", users.get_current_user())        greetings.order("-date")

  16. Entity group Passing parameters

  17. Use templates Templates are used to generate the similar webpages with different variables

  18. Use stylesheet Change app.yaml Include the code for using stylesheet

  19. Upload application Create an application at Google AppEngine Change the app name in app.yaml correspondingly Run appcfg.py update helloworld/

More Related