Python 2.7 has reached end of supportand will bedeprecatedon January 31, 2026. After deprecation, you won't be able to deploy Python 2.7applications, even if your organization previously used an organization policy tore-enable deployments of legacy runtimes. Your existing Python2.7 applications will continue to run and receive traffic after theirdeprecation date. We recommend thatyoumigrate to the latest supported version of Python.

Google Protocol RPC Library Overview

Note: If you wish to use authentication with Google Protocol RPC, you can use the authentication currently available for App Engineapps in the Google Cloud console. You'll also need to specify the use login requirement in yourapp.yamlfile. Other auth methodologies are not currently supported by the Google Protocol RPC library withinApp Engine.

The Google Protocol RPC library is a framework for implementing HTTP-based remote procedure call (RPC) services. An RPC service is a collection of message types and remote methods that provide a structured way for external applications to interact with web applications. Because you can define messages and services in the Python programming language, it's easy to develop Protocol RPC services, test those services, and scale them on App Engine.

While you can use the Google Protocol RPC library for any kind of HTTP-based RPC service, some common use cases include:

  • Publishing web APIs for use by third parties
  • Creating structured Ajax backends
  • Cloning to long-running server communication

You can define a Google Protocol RPC service in a single Python class that contains any number of declared remote methods. Each remote method accepts a specific set of parameters as a request and returns a specific response. These request and response parameters are user-defined classes known as messages.

The Hello World of Google Protocol RPC

This section presents an example of a very simple service definition that receives a message from a remote client. The message contains a user's name (HelloRequest.my_name) and sends back a greeting for that person (HelloResponse.hello):

fromprotorpcimportmessagesfromprotorpcimportremotefromprotorpc.wsgiimportservicepackage='hello'# Create the request string containing the user's nameclassHelloRequest(messages.Message):my_name=messages.StringField(1,required=True)# Create the response stringclassHelloResponse(messages.Message):hello=messages.StringField(1,required=True)# Create the RPC service to exchange messagesclassHelloService(remote.Service):@remote.method(HelloRequest,HelloResponse)defhello(self,request):returnHelloResponse(hello='Hello there,%s!'%request.my_name)# Map the RPC service and path (/hello)app=service.service_mappings([('/hello.*',HelloService)])

Getting Started with Google Protocol RPC

This section demonstrates how to get started with Google Protocol RPC using the guestbook application developed in thePython. Users can visit the guestbook (also included as a demo in the Python SDK) online, write entries, and view entries from all users. Users interact with the interface directly, but there is no way for web applications to easily access that information.

That's where Protocol RPC comes in. In this tutorial, we'll apply Google Protocol RPC to this basic guestbook, enabling other web applications to access the guestbook's data. This tutorial only covers using Google Protocol RPC to extend the guestbook functionality; it's up to you what to do next. For example, you might want to write a tool that reads the messages posted by users and makes a time-series graph of posts per day. How you use Protocol RPC depends on your specific app; the important point is that Google Protocol RPC greatly expands what you can do with your application's data.

To begin, you'll create a file,postservice.py, which implements remote methods to access data in the guestbook application's datastore.

Creating the PostService Module

The first step to get started with Google Protocol RPC is to create a file calledpostservice.py in your application directory. You'll use this file to define the new service, which implements two methods—one that remotely posts data and another that remotely gets data.

You don't need to add anything to this file now—but this is the file where you'll put all the code defined in the subsequent sections. In the next section, you'll create a message that represents a note posted to the guestbook application's datastore.

Working with Messages

Messages are the fundamental data type used in Google Protocol RPC. Messages are defined by declaring a class that inherits from theMessage base class. Then you specify class attributes that correspond to each of the message's fields.

For example, the guestbook service allows users to post a note. If you haven't done so already, create a file calledpostservice.py in your application directory. The PostService uses the Greeting class to store a post in the datastore. Let's define a message that represents such a note:

fromprotorpcimportmessagesclassNote(messages.Message):text=messages.StringField(1,required=True)when=messages.IntegerField(2)

The note message is defined by two fields,text andwhen. Each field has a specific type. The text field is a unicode string representing the content of a user's post to the guestbook page. Thewhen field is an integer representing the post's timestamp. In defining the string, we also:

  • Give each field a unique numerical value (1 fortext and2 forwhen) that the underlying network protocol uses to identify the field.
  • Maketext a required field. Fields are optional by default, you can mark them as required by settingrequired=True. Messages must be initialized by setting required fields to a value. Google Protocol RPC service methods accept only properly initialized messages.

You can set values for the fields using the constructor of the Note class:

# Import the standardtime Python library to handle the timestamp.importtimenote_instance=Note(text=u'Hello guestbook!',when=int(time.time()))

You can also read and set values on a message like normal Python attribute values. For example, to change the message:

printnote_instance.textnote_instance.text=u'Good-bye guestbook!'printnote_instance.text
which outputs the following
Hello guestbook!Good-bye guestbook!

Defining a Service

Aservice is a class definition that inherits from theService base-class. Remote methods of a service are indicated by using theremote decorator. Every method of a service accepts a single message as its parameter and returns a single message as its response.

Let's define the first method of the PostService. Add the following to yourpostservice.py file:

importdatetimefromprotorpcimportmessage_typesfromprotorpcimportremoteimportguestbookclassPostService(remote.Service):# Add the remote decorator to indicate the service methods@remote.method(Note,message_types.VoidMessage)defpost_note(self,request):# If the Note instance has a timestamp, use that timestampifrequest.whenisnotNone:when=datetime.datetime.utcfromtimestamp(request.when)# Else use the current timeelse:when=datetime.datetime.now()note=guestbook.Greeting(content=request.text,date=when,parent=guestbook.guestbook_key)note.put()returnmessage_types.VoidMessage()

Theremote decorator takes two parameters:

  • The expected request type. The post_note() method accepts a Note instance as its request type.
  • The expected response type. The Google Protocol RPC library comes with a built-in type called a VoidMessage (in theprotorpc.message_types module), which is defined as a message with no fields. This means that the post_note() message does not return anything useful to its caller. If it returns without error, the message is considered to have been posted.

SinceNote.when is an optional field, it may not have been set by the caller. When this happens, the value ofwhen is set to None. WhenNote.when is set to None, post_note() creates the timestamp using the time it received the message.

The response message is instantiated by the remote method and becomes the remote method's return value.

Registering the Service

You can publish your new service as a WSGI application using theprotorpc.wsgi.service library. Create a new file calledservices.py in your application directory and add the following code to create your service:

fromprotorpc.wsgiimportserviceimportpostservice# Map the RPC service and path (/PostService)app=service.service_mappings([('/PostService',postservice.PostService)])

Now, add the following handler to yourapp.yaml file above the existing catch-all entry:

-url:/PostService.*script:services.app-url:.*script:guestbook.app

Testing the Service from the Command Line

Now that you've created the service, you can test it usingcurl or a similar command-line tool.

# After starting the development web server:# NOTE: ProtoRPC always expect a POST.%curl-H \'content-type:application/json' \-d'{"text": "Hello guestbook!"}'\http://localhost:8080/PostService.post_note

An empty JSON response indicates that the note posted successfully. You can see the note by going to your guestbook application in your browser (http://localhost:8080/).

Adding Message Fields

Now that we can post messages to the PostService, let's add a new method to get messages from the PostService. First, we'll define a request message inpostservice.py that defines some defaults and a new enum field that tells the server how to order notes in the response. Define itabove thePostService class that you defined earlier:

classGetNotesRequest(messages.Message):limit=messages.IntegerField(1,default=10)on_or_before=messages.IntegerField(2)classOrder(messages.Enum):WHEN=1TEXT=2order=messages.EnumField(Order,3,default=Order.WHEN)

When sent to the PostService, this message requests a number of notes on or before a certain date and in a particular order. Thelimit field indicates the maximum number of notes to fetch. If not explicitly set,limit defaults to 10 notes (as indicated by thedefault=10 keyword argument).

The order field introduces theEnumField class, which enables theenum field type when the value of a field is restricted to a limited number of known symbolic values. In this case, theenum indicates to the server how to order notes in the response. To define the enum values, create a sub-class of theEnum class. Each name must be assigned a unique number for the type. Each number is converted to an instance of the enum type and can be accessed from the class.

print'Enum value Order.%s has number%d'%(GetNotesRequest.Order.WHEN.name,GetNotesRequest.Order.WHEN.number)

Eachenum value has a special characteristic that makes it easy to convert to their name or their number. Instead of accessing the name and number attribute, just convert each value to a string or an integer:

print'Enum value Order.%s has number%d'%(GetNotesRequest.Order.WHEN,GetNotesRequest.Order.WHEN)

Enum fields are declared similarly to other fields except they must have the enum type as its first parameter before the field number. Enum fields can also have default values.

Defining the Response Message

Now let's define the get_notes() response message. The response needs to be a collection of Note messages. Messages can contain other messages. In the case of theNotes.notes field defined below, we indicate that it is a collection of messages by providing theNote class as the first parameter to themessages.MessageField constructor (before the field number):

classNotes(messages.Message):notes=messages.MessageField(Note,1,repeated=True)

TheNotes.notes field is also a repeated field as indicated by therepeated=True keyword argument. Values of repeated fields must be lists of the field type of their declaration. In this case,Notes.notes must be a list of Note instances. Lists are automatically created and cannot be assigned to None.

For example, here is how to create a Notes object:

response=Notes(notes=[Note(text='This is note 1'),Note(text='This is note 2')])print'The first note is:',response.notes[0].textprint'The second note is:',response.notes[1].text

Implement get_notes

Now we can add the get_notes() method to the PostService class:

importdatetimeimporttimefromprotorpcimportremoteclassPostService(remote.Service):@remote.method(GetNotesRequest,Notes)defget_notes(self,request):query=guestbook.Greeting.query().order(-guestbook.Greeting.date)ifrequest.on_or_before:when=datetime.datetime.utcfromtimestamp(request.on_or_before)query=query.filter(guestbook.Greeting.date <=when)notes=[]fornote_modelinquery.fetch(request.limit):ifnote_model.date:when=int(time.mktime(note_model.date.utctimetuple()))else:when=Nonenote=Note(text=note_model.content,when=when)notes.append(note)ifrequest.order==GetNotesRequest.Order.TEXT:notes.sort(key=lambdanote:note.text)returnNotes(notes=notes)

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.