Python for Google App Engine
()
About this ebook
- Use the power of Python to build full-fledged, scalable web applications running on Google's infrastructure
- Learn how to use Google Cloud Platform tools and services adding features and enriching your Python web applications
- Build a real-world web application in no time with this comprehensive step-by-step guide
If you are a Python developer, whether you have experience in web applications development or not, and want to rapidly deploy a scalable backend service or a modern web application on Google App Engine, then this book is for you.
Related to Python for Google App Engine
Related ebooks
Building Web Applications with Flask Rating: 0 out of 5 stars0 ratingsRaspberry Pi 3 Projects for Java Programmers Rating: 0 out of 5 stars0 ratingsDjango Project Blueprints Rating: 0 out of 5 stars0 ratingsLearning Website Development with Django Rating: 0 out of 5 stars0 ratingsNode Web Development, Second Edition Rating: 0 out of 5 stars0 ratingsBuilding Python Real-Time Applications with Storm Rating: 0 out of 5 stars0 ratingsPython Web Scraping - Second Edition Rating: 5 out of 5 stars5/5Google Maps JavaScript API Cookbook Rating: 2 out of 5 stars2/5Learning Flask Framework Rating: 4 out of 5 stars4/5Neo4j High Performance Rating: 0 out of 5 stars0 ratingsDistributed Computing with Python Rating: 0 out of 5 stars0 ratingsGetting Started with Beautiful Soup Rating: 3 out of 5 stars3/5Nginx Troubleshooting Rating: 0 out of 5 stars0 ratingsBeginning PyQt: A Hands-on Approach to GUI Programming Rating: 0 out of 5 stars0 ratingsMastering Google App Engine Rating: 0 out of 5 stars0 ratingsMastering Python Rating: 0 out of 5 stars0 ratingsMastering PostgreSQL: From Basics to Expert Proficiency Rating: 0 out of 5 stars0 ratingsBuilding RESTful Python Web Services Rating: 5 out of 5 stars5/5Web Development with Django Cookbook - Second Edition Rating: 0 out of 5 stars0 ratingsNumPy: Beginner's Guide - Third Edition Rating: 4 out of 5 stars4/5Mastering Scala Machine Learning Rating: 0 out of 5 stars0 ratingsNeo4j Cookbook Rating: 0 out of 5 stars0 ratingsNode Cookbook: Second Edition Rating: 3 out of 5 stars3/5Large Scale Machine Learning with Python Rating: 2 out of 5 stars2/5Python for Professionals Rating: 0 out of 5 stars0 ratingsPython Business Intelligence Cookbook Rating: 0 out of 5 stars0 ratingsPython Networking 101: Navigating essentials of networking, socket programming, AsyncIO, network testing, simulations and Ansible Rating: 0 out of 5 stars0 ratings
Internet & Web For You
Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Everybody Lies: Big Data, New Data, and What the Internet Can Tell Us About Who We Really Are Rating: 4 out of 5 stars4/5How to Be Invisible: Protect Your Home, Your Children, Your Assets, and Your Life Rating: 4 out of 5 stars4/5No Place to Hide: Edward Snowden, the NSA, and the U.S. Surveillance State Rating: 4 out of 5 stars4/5Six Figure Blogging Blueprint Rating: 5 out of 5 stars5/5Coding For Dummies Rating: 5 out of 5 stars5/5The Hacker Crackdown: Law and Disorder on the Electronic Frontier Rating: 4 out of 5 stars4/5Podcasting For Dummies Rating: 4 out of 5 stars4/5An Ultimate Guide to Kali Linux for Beginners Rating: 3 out of 5 stars3/5Surveillance and Surveillance Detection: A CIA Insider's Guide Rating: 3 out of 5 stars3/5The $1,000,000 Web Designer Guide: A Practical Guide for Wealth and Freedom as an Online Freelancer Rating: 4 out of 5 stars4/5Social Engineering: The Science of Human Hacking Rating: 3 out of 5 stars3/5The Gothic Novel Collection Rating: 5 out of 5 stars5/5Coding with AI For Dummies Rating: 1 out of 5 stars1/5Cybersecurity All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsHow To Start A Profitable Authority Blog In Under One Hour Rating: 5 out of 5 stars5/5The Beginner's Affiliate Marketing Blueprint Rating: 4 out of 5 stars4/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5How to Disappear and Live Off the Grid: A CIA Insider's Guide Rating: 0 out of 5 stars0 ratingsThe Digital Marketing Handbook: A Step-By-Step Guide to Creating Websites That Sell Rating: 5 out of 5 stars5/5WordPress For Dummies Rating: 0 out of 5 stars0 ratingsTor and the Dark Art of Anonymity Rating: 5 out of 5 stars5/5JavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5Deep Dive: Exploring the Real-world Value of Open Source Intelligence Rating: 0 out of 5 stars0 ratingsTLS Cryptography In-Depth: Explore the intricacies of modern cryptography and the inner workings of TLS Rating: 0 out of 5 stars0 ratings
Reviews for Python for Google App Engine
0 ratings0 reviews
Book preview
Python for Google App Engine - Massimiliano Pippi
Table of Contents
Python for Google App Engine
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. Getting Started
The cloud computing stack – SaaS, PaaS, and IaaS
Google Cloud Platform
Hosting + Compute
Storage
BigQuery
Services
What Google App Engine does
The runtime environment
The services
Making our first Python application
Download and installation
Installing on Windows
Installing on Mac OS X
Installing on Linux
App Engine Launcher
Creating the application
The app.yaml configuration file
The main.py application script
Running the development server
Uploading the application to App Engine
Google Developer Console
Development Console
Summary
2. A More Complex Application
Experimenting on the Notes application
Authenticating users
HTML templates with Jinja2
Handling forms
Persisting data in Datastore
Defining the models
Basic querying
Transactions
Using static files
Summary
3. Storing and Processing Users' Data
Uploading files to Google Cloud Storage
Installing Cloud Storage Client Library
Adding a form to upload images
Serving files from Cloud Storage
Serving files through Google's Content Delivery Network
Serving images
Serving other types of files
Transforming images with the Images service
Processing long jobs with the task queue
Scheduling tasks with Cron
Sending notification e-mails
Receiving users' data as e-mail messages
Summary
4. Improving Application Performance
Advanced use of Datastore
More on properties – arrange composite data with StructuredProperty
More on queries – save space with projections and optimize iterations with mapping
Projection queries
Mapping
NDB asynchronous operations
Caching
Backup and restore functionalities
Indexing
Using Memcache
Breaking our application into modules
Summary
5. Storing Data in Google Cloud SQL
Creating a Cloud SQL instance
Configuring access
Setting the root password
Connecting to the instance with the MySQL console
Creating the notes database
Creating a dedicated user
Creating tables
Connecting to the instance from our application
Loading and saving data
Using the local MySQL installation for development
Summary
6. Using Channels to Implement a Real-time Application
Understanding how the Channel API works
Making our application real time
Implementing the server
The JavaScript code for clients
Tracking connections and disconnections
Summary
7. Building an Application with Django
Setting up the local environment
Configuring a virtual environment
Installing dependencies
Rewriting our application using Django 1.7
Using Google Cloud SQL as a database backend
Creating a reusable application in Django
Views and templates
Authenticating users with Django
Using the ORM and migrations system
Processing forms with the Forms API
Uploading files to Google Cloud Storage
Summary
8. Exposing a REST API with Google Cloud Endpoints
Reasons to use a REST API
Designing and building the API
Resources, URLs, HTTP verbs, and response code
Defining resource representations
Implementing API endpoints
Testing the API with API Explorer
Protecting an endpoint with OAuth2
Summary
Index
Python for Google App Engine
Python for Google App Engine
Copyright © 2015 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: January 2015
Production reference: 1210115
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78439-819-4
www.packtpub.com
Credits
Author
Massimiliano Pippi
Reviewers
Dom Derrien
Samuel Goebert
Marcos Placona
Commissioning Editor
Taron Pereira
Acquisition Editor
Richard Brookes-Bland
Content Development Editor
Vaibhav Pawar
Technical Editor
Tanmayee Patil
Copy Editors
Deepa Nambiar
Vikrant Phadke
Stuti Srivastava
Project Coordinator
Kranti Berde
Proofreaders
Simran Bhogal
Maria Gould
Ameesha Green
Paul Hindle
Indexer
Priya Sane
Production Coordinator
Nitesh Thakur
Cover Work
Nitesh Thakur
About the Author
Massimiliano Pippi has been a software developer for over 10 years, more than half of which he spent working with scientific visualization and backend software for a private company, using C++ and Qt technologies. He started using Python in 2008 and currently works at Evonove, a small company where he has been leading a number of Python software projects, most of which are based on the Django web framework. He is also an open source advocate and active contributor, documentation fanatic, and speaker at conferences. He writes about Python and other development-related topics at http://dev.pippi.im.
About the Reviewers
Dom Derrien is a full-stack web developer who has been defining application environments with a focus on high availability and scalability. He's been in the development field for more than 15 years and has worked for big and small companies and also as an entrepreneur.
He's currently working for the game company Ubisoft Inc., where he defines the next generation services platform for its successful AAA games. To extend the gamer experience on the Web and on mobiles, he provides technical means that are transparent, efficient, and highly flexible.
On receiving the invitation to review this book, after a comparable work for the books Google App Engine Java and GWT Application Development, Packt Publishing, he was pleased to share his knowledge about Google App Engine again.
I want to thank my wife, Sophie, and our sons, Erwan and Goulven, with whom I enjoy a peaceful life in Montréal, Québec, Canada.
Samuel Goebert is a computer science PhD student at Plymouth University, UK. Samuel has over 12 years of experience in software development and associated technologies. For further details about him, check out his LinkedIn profile at www.linkedin.com/in/samuelgoebert.
Marcos Placona grew up in Sao Paulo, Brazil, and started tinkering with web development when 14.400 kbs modems were the coolest thing.
He then eagerly pursued a computer science degree and soon after an opportunity arose on the other side of the Atlantic. In his 20s, he decided to move to England where he worked as a software engineer at a software house. He also started blogging on www.placona.co.uk.
Marcos has published and printed articles in several web portals, magazines, and books.
He is currently working as a developer evangelist at Twilio; he actively works with developers and communities to equip and inspire them while making their applications better.
www.PacktPub.com
Support files, eBooks, discount offers, and more
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Free access for Packt account holders
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.
To Azzurra and Valerio, thanks for being patient with me. But I also have been patient with you, so I think we're even.
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,