Book Image

Python for Google App Engine

By : Massimiliano Pippi
Book Image

Python for Google App Engine

By: Massimiliano Pippi

Overview of this book

Table of Contents (15 chapters)
Python for Google App Engine
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

In April 2008, 10,000 developers from all around the world were lucky enough to get an account to access the preview release of Google App Engine, which is a tool designed to let users run their web applications on the same infrastructure Google uses for its own services. Announced during Google's Campfire One event, App Engine was described as something easy to use, easy to scale and free to get started; three design goals that perfectly matched the requirements of a typical tech start-up trying to reduce the time to market.

While other big companies at that time were already offering to lease part of their own infrastructure, selling reliability and scalability in an affordable, pay-per-use fashion, Google set App Engine one step ahead by providing developers with application-building blocks instead of simple access to hardware; it is a hosting model followed by many others later on. The goal of this model is to let developers focus on the code and forget about failing machines, network issues, scalability problems, and performance tuning; the choice of Python as the first programming language supported by App Engine was a natural choice for a tool whose aim is to make writing and running web applications easier.

During the Google I/O event in 2012, Google announced that several other building blocks from its own infrastructure would be made available under the name of Google Cloud Platform, first as a partner program and then as a general availability product. Currently, App Engine is not only a notable member of the Cloud Platform family but also a mature and well-maintained platform, widely adopted and with a huge list of customers' success stories.

This book will teach you how to write and run web applications in Python with App Engine, getting the most out of Google Cloud Platform. Starting with a simple application, you will add more and more features to it, each time with the help of the components and services provided by Google's infrastructure.

What this book covers

Chapter 1, Getting Started, will help you get your hands dirty with a very simple functional Python application running on a production server. The chapter begins with making a survey of Google's cloud infrastructure, showing where App Engine is placed and how it compares to other well-known cloud services. It then walks readers through downloading and installing the runtime for Linux, Windows, and OS X, coding a Hello, World! application and deploying it on App Engine. The last part introduces administration consoles both for the development and production servers.

Chapter 2, A More Complex Application, teaches you how to implement a complex web application running on App Engine. It begins with an introduction to the bundled webapp2 framework and possible alternatives; then, you will get in touch with user authentication and form handling and then an introduction to Google's Datastore nonrelational database. The last part shows you how to make HTML pages through templates rendering and how to serve all the static files needed to style the page.

Chapter 3, Storing and Processing Users' Data, will show you how to add more functionalities to the app from the previous chapter. The chapter begins by showing you how to let users upload files using Google Cloud Storage and how to manipulate such files when they contain image data with the Image API. It then introduces you to the task queues used to execute long jobs (such as image manipulation) outside the request process and how to schedule batches of such jobs. The last part shows you how to send and receive e-mails through the Mail API.

Chapter 4, Improving Application Performance, begins by showing how to improve application performance using advanced features of Datastore. It then shows you how to use the cache provided by App Engine and how to break the application into smaller services using Modules.

Chapter 5, Storing Data in Google Cloud SQL, is dedicated to the Google Cloud SQL service. It shows you how to create and manage a database instance and how to connect and perform queries. It then demonstrates how an App Engine application can save and retrieve data and how to use a local MySQL installation during development.

Chapter 6, Using Channels to Implement a Real-time Application, shows you how to make our application real time, in other words, how to update what clients see without reloading the page in the browser. The first part shows how the Channel API works, what happens when a client connects, and what roundtrip of a message is from the server to the client. Then, it shows you how to add a real-time feature to our application from previous chapters.

Chapter 7, Building an Application with Django, teaches you how to build an App Engine application using the Django web framework instead of webapp2. The first part shows you how to configure the local environment for development, and then the application from previous chapters is rewritten using some of the features provided by Django. The last part shows you how to deploy the application on a production server.

Chapter 8, Exposing a REST API with Google Cloud Endpoints, shows you how to rewrite part of our application to expose data through a REST API. The first part explores all the operations needed to set up and configure a project and how to implement a couple of endpoints for our API. The last part shows explores how to add OAuth protection to the API endpoints.

What you need for this book

In order to run the code demonstrated in this book, you need a Python interpreter for version 2.7.x and the App Engine Python SDK as described in the Download and installation section from Chapter 1, Getting Started.

Additionally, to access the example application once it runs on App Engine, you need a recent version of a web browser such as Google Chrome, Mozilla Firefox, Apple Safari, or Microsoft Internet Explorer.

Who this book is for

If you are a Python programmer who wants to apply your skills to write web applications using Google App Engine and Google Cloud Platform tools and services, this is the book for you. Solid Python programming knowledge is required as well as a basic understanding of the anatomy of a web application. Prior knowledge of Google App Engine is not assumed, nor is any experience with a similar tool required.

By reading this book, you will become familiar with the functionalities provided by Google Cloud Platform with particular reference to Google App Engine, Google Cloud Storage, Google Cloud SQL, and Google Cloud Endpoints at the latest versions available at the time of writing this book.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "If a user is already logged in, the get_current_user() method returns a User object, otherwise it returns None parameter".

A block of code is set as follows:

import webapp2

class HomePage(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('Hello, World!')

app = webapp2.WSGIApplication([('/', HomePage)], debug=True)

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "To create a new application, click the Create an Application button."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of. To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from: https://www.packtpub.com/sites/default/files/downloads/B03710_8194OS_Graphics.pdf

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy. Please contact us at with a link to the suspected pirated material. We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.