"Quickstart - Python App Engine Standard Environment"

Published: Mon 04 July 2016

In content.

This quickstart shows you how to create a small App Engine application that displays a short message.

Download the Hello World app

We've created a simple Hello World app for Python so you can quickly get a feel for deploying an app to Google Cloud Platform. Follow these steps to download Hello World to your local machine.

  1. Clone the Hello World sample app repository to your local machine:

    git clone https://github.com/GoogleCloudPlatform/python-docs-samples

  2. Then go to the directory that contains the sample code:

    cd python-docs-samples/appengine/standard/hello_world

Alternatively, you can download the sample as a zip file and extract it.

Test the application

Test the application using the development web server, which is included with the App Engine SDK.

  1. From within the sample directory, start the development server with the following command using Google Cloud SDK Shell:

    dev_appserver.py .

    The development server is now running, listening for requests on port 8080.

    Caution: If you did not create symlinks for dev_appserver.py when installing the App Engine SDK, you may need to specify the full path todev_appserver.py.

  2. Visit http://localhost:8080/ in your web browser to see the app in action.

For more information about running the development web server, see the Python Development Server reference.

Make a change

You can leave the web server running while you develop your application. The web server watches for changes in your source files and reloads them if necessary.

  1. Try it now: Leave the web server running, then edit main.py at python-doc-samples\appengine\standard\hello_world  to change Hello, World! to something else.
  2. Reload http://localhost:8080/ to see the results.

Deploy your app

To deploy your app to App Engine, you will need to register a project to create your project ID, which will determine the URL for the app.

  1. In the Cloud Platform Console, go to the Projects page and select or create a new project.GO TO THE PROJECTS PAGE
  2. Note the project ID that you created above.
  3. Upload your application to Google App Engine by invoking the following command. This opens a browser window for you to sign in using your Google account. You'll be providing the project ID as the argument for -A. Use the -V argument to specify a version name.

    appcfg.py -A <YOUR_PROJECT_ID> -V v1 update .

  4. Your app is now deployed and ready to serve traffic athttp://<YOUR_PROJECT_ID_>.appspot.com

.

Congratulations!

You have completed this quickstart.

The full URL for your application is http://<YOUR_PROJECT_ID>.appspot.com/. Optionally, you can purchase and use a top-level domain name for your app, or use one that you have already registered.

Clean up

To avoid incurring charges to your Google Cloud Platform account for the resources used in this quickstart:

  1. Go to the Cloud Platform Console.
  2. In the list of projects, select the project that you want to shut down.
  3. In the prompt, type your project ID to confirm the deletion.
  4. Click Shut down to schedule your project for deletion.

What's next

Use a custom domain

You can serve your App Engine app using your own custom domain instead of appspot.com. For more information, see Using Custom Domains and SSL.

Hello World code review

Hello World is the simplest possible App Engine app: it contains only one Python module, has only one version, and all of the code is located within the app's root directory. This section describes each of the app files in detail.

main.py

This Python script responds to a request with an HTTP header and the message Hello, World!

appengine/standard/hello_world/main.py

VIEW ON GITHUB

import webapp2 class MainPage(webapp2.RequestHandler): def get(self): self.response.headers['Content-Type'] \= 'text/plain' self.response.write('Hello, World!') app \= webapp2.WSGIApplication([ ('/', MainPage), ], debug\=True)

app.yaml

The app.yaml configuration file defines several options for the app, and describes which handler scripts should be used for which URLs.

appengine/standard/hello_world/app.yaml

VIEW ON GITHUB

runtime: python27 api_version: 1 threadsafe: true handlers: - url: /.* script: main.app

From top to bottom, this configuration file says the following about this application:

  • This code runs in the python27 runtime environment, API version 1. Additional runtime environments and languages may be supported in the future.
  • This application is threadsafe so the same instance can handle several simultaneous requests. Threadsafe is an advanced feature and can result in erratic behavior if your application is not specifically designed to be threadsafe.
  • Every request to a URL whose path matches the regular expression /.* (all URLs) should be handled by the app object in the main Python module.

The syntax of this file is YAML. For a complete list of configuration options, see the app.yaml reference.

Learn the whole platform

Now that you know what it's like to develop and deploy App Engine apps, you can stretch out and see the rest of Google Cloud Platform. For a guided walkthrough that teaches you how to create an application that uses the entire platform, not just App Engine, check out our Creating a Guestbookquickstart in which you expand this simple application to become a fully-fledged Guestbook application that lets authenticated Google accounts post messages to a public page.

social