The Python SDK includes a library called Remote API that lets you transparently access App Engine services from any Python application. For example, you can use Remote API to access a production datastore from an app running on your local machine.
Enabling Remote API
You can enable the Remote API using either the
builtins directive, which will make the Remote API available on the default path, or the
Note that if the URL is changed, all applications that connect to the App Engine application will need to change the URL as well. Both methods are detailed below.
To map the Remote API to the default path (
/remote_api), add the
remote_api builtin to your
builtins: - remote_api: on
If you need to map the Remote API to a path other than the default, you can use
url directive in
For Python 2.7:
- url: /remoteapi.* script: google.appengine.ext.remote_api.handler.application login: admin
Using the Remote API Shell
The Python SDK includes a Remote API shell which allows you to run a shell that has access to App Engine services and your applications datastore.
To use modules defined by your application you will need to run the Remote API shell from your application's directory. From the application directory, execute the command:
$GAE_SDK_ROOT/remote_api_shell.py -s <your_app_id>.appspot.com
When executing the Remote API shell you can also provide the flag
This will allow you to connect using OAuth 2.0 credentials from the
command line tool, which is more secure than typing in your username and
password. You can also provide your OAuth 2.0 credentials through other methods
which are detailed on the
Application Default Credentials page.
From the Remote API shell you can simply import application modules just as you would in a normal App Engine application. An example of accessing the Guestbook applications datastore is below:
>>>> import helloworld >>> # Fetch the most recent 10 guestbook entries >>> entries = helloworld.Greeting.all().order("-date").fetch(10) >>> >>> # Create our own guestbook entry >>> helloworld.Greeting(content="A greeting").put()
In general, the Remote API shell behaves exactly as if you were accessing the datastore directly. However because the script is running on your local machine, you can access files and resources on your local machine. There are however limitations to the Remote API which are discussed along with best practices in the Remote API article.
Using the Remote API in a Local Client
The Remote API can also be used within local applications. This will allow you to write local applications that use App Engine services and access datastore. It is important to note that using the Remote API will incur quota usage on the application you are accessing. Other limitations of the Remote API are discussed in the Remote API article.
Before beginning, make sure the App Engine SDK is added to your Python path and Remote API is enabled in your App Engine application. An example local application that uses Remote API and the Guestbook application is shown below:
from google.appengine.ext.remote_api import remote_api_stub from helloworld import helloworld import getpass def auth_func(): return (raw_input('Username:'), getpass.getpass('Password:')) remote_api_stub.ConfigureRemoteApi(None, '/remote_api', auth_func, 'your_app_id.appspot.com') # Fetch the most recent 10 guestbook entries entries = helloworld.Greeting.all().order("-date").fetch(10) # Create our own guestbook entry helloworld.Greeting(content="A greeting").put()
If you want to use OAuth 2.0 credentials instead when configuring the Remote API, you can instead write:
from google.appengine.ext.remote_api import remote_api_stub from helloworld import helloworld remote_api_stub.ConfigureRemoteApiForOAuth('your_app_id.appspot.com', '/remote_api') # Fetch the most recent 10 guestbook entries entries = helloworld.Greeting.all().order("-date").fetch(10) # Create our own guestbook entry helloworld.Greeting(content="A greeting").put()