The Google Cloud CLI includes a local development server
(dev_appserver.py
). You can use the local development server to simulate
running your application in production App Engine as well as use it to
access App Engine bundled services.
The simulated environment enforces some sandbox restrictions, such as restricted system functions and Python 3 module imports, but not others, like request timeouts or quotas.
The local development server also simulates the services provided by the libraries in the SDK for App Engine, including Datastore, Memcache, and Task Queues, by performing their tasks locally. When your application is running in the development server, you can still make remote API calls to the production infrastructure using Google APIs HTTP endpoints.
Running the local development server
After you create the
app.yaml
configuration file for your app, you can start the local development server with
the dev_appserver.py
command to run your app locally.
To start the local development server:
Running the local development server (dev_appserver.py
)
The local development server is located at
[PATH_TO_CLOUD_SDK]/google-cloud-sdk/bin/dev_appserver.py
. To run the tool,
you can either specify the full path when you run dev_appserver.py
, or you can
add dev_appserver.py
to your PATH
environment variable.
If you want to add the Google Cloud CLI tools to your PATH
and enable command
completion, run the following command:
[PATH_TO_CLOUD_SDK]/google-cloud-sdk/install.sh
In the directory that contains your
app.yaml
configuration file, run thedev_appserver.py
command. If Python 2 is not the default interpreter on your system, you need to runpython2 dev_appserver.py
to ensure the Python 2 interpreter is used.Specify the directory path to your app, for example:
dev_appserver.py --runtime_python_path=/usr/bin/python3 [PATH_TO_YOUR_APP]
You can also set the argument to a comma-separated key-value list like this:
dev_appserver.py --runtime_python_path="python27=/usr/bin/python2.7,python3=/usr/bin/python3" [PATH_TO_YOUR_APP]
Alternatively, you can specify the configuration file of a specific service, for example:
dev_appserver.py --runtime_python_path=/usr/bin/python3 app.yaml
To change the port, you include the
--port
option:dev_appserver.py --runtime_python_path=/usr/bin/python3 --port=9999 [PATH_TO_YOUR_APP]
Is dev_appserver.py not working?
To learn more about the
dev_appserver.py
command options, see Local Development Server Options.The local development server is now running and listening for requests. You can visit http://localhost:8080/ in your web browser to see the app in action.
If you specified a custom port with the
--port
option, remember to open your browser to that port.
To stop the local server from the command line, press the following:
- macOS or Linux: Control+C
- Windows: Control+Break
Specifying application IDs
To access your App ID in the local server, for example to spoof an email
address, use the
get_application_id()
function. To get the hostname of the running app, use the
get_default_version_hostname()
function.
Detecting application runtime environment
To determine whether your code is running in production or in the local
development server, you can check the value of the GAE_ENV
environment
variable:
if os.getenv('GAE_ENV', '').startswith('standard'):
# Production in the standard environment
else:
# Local development server
Using the Users service
App Engine provides a
Users Service to simplify
authentication and authorization for your application. The local development
server simulates the behavior of Google
Accounts
with its own sign-in and sign-out pages. While running under the local
development server, the
functions return URLs for /_ah/login
and /_ah/logout
on the local server.
Using Mail
The local development server can send email for calls to the App Engine mail service using either an SMTP server or a local installation of Sendmail.
Using SMTP
To enable mail support with an SMTP server, invoke dev_appserver.py
as
follows::
dev_appserver.py --smtp_host=smtp.example.com --smtp_port=25 \
--smtp_user=ajohnson --smtp_password=k1tt3ns [PATH_TO_YOUR_APP]
where you set the --smtp_host
, --smtp_port
, --smtp_user
and
--smtp_password
options with your own values.
Using Sendmail
To enable mail support with Sendmail, invoke dev_appserver.py
as follows:
dev_appserver.py --enable_sendmail=yes [PATH_TO_YOUR_APP]
The local server will use the sendmail
command to send email messages with
your installation's default configuration.
Using URL Fetch
When your application uses the URL fetch API to make an HTTP request, the local development server makes the request directly from your computer. The URL Fetch behavior on the local server may differ from production App Engine if you use a proxy server for accessing websites.