Python Quick Start

This guide gets you started with gRPC in Python with a simple working example.

Prerequisites

  • Python 2.7, or Python 3.4 or higher
  • pip version 9.0.1 or higher

If necessary, upgrade your version of pip:

  1. $ python -m pip install --upgrade pip

If you cannot upgrade pip due to a system-owned installation, you canrun the example in a virtualenv:

  1. $ python -m pip install virtualenv
  2. $ virtualenv venv
  3. $ source venv/bin/activate
  4. $ python -m pip install --upgrade pip

gRPC

Install gRPC:

  1. $ python -m pip install grpcio

Or, to install it system wide:

  1. $ sudo python -m pip install grpcio

On El Capitan OSX, you may get the following error:

  1. $ OSError: [Errno 1] Operation not permitted: '/tmp/pip-qwTLbI-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six-1.4.1-py2.7.egg-info'

You can work around this using:

  1. $ python -m pip install grpcio --ignore-installed

gRPC tools

Python’s gRPC tools include the protocol buffer compiler protoc and thespecial plugin for generating server and client code from .proto servicedefinitions. For the first part of our quick-start example, we’ve alreadygenerated the server and client stubs fromhelloworld.proto,but you’ll need the tools for the rest of our quick start, as well as latertutorials and your own projects.

To install gRPC tools, run:

  1. $ python -m pip install grpcio-tools

Download the example

You’ll need a local copy of the example code to work through this quick start.Download the example code from our GitHub repository (the following commandclones the entire repository, but you just need the examples for this quick startand other tutorials):

  1. # Clone the repository to get the example code:
  2. $ git clone -b v1.28.1 https://github.com/grpc/grpc
  3. # Navigate to the "hello, world" Python example:
  4. $ cd grpc/examples/python/helloworld

Run a gRPC application

From the examples/python/helloworld directory:

  • Run the server:
  1. $ python greeter_server.py
  • From another terminal, run the client:
  1. $ python greeter_client.py

Congratulations! You’ve just run a client-server application with gRPC.

Update a gRPC service

Now let’s look at how to update the application with an extra method on theserver for the client to call. Our gRPC service is defined using protocolbuffers; you can find out lots more about how to define a service in a .protofile inWhat is gRPC? andgRPC Basics: Python. For now all you needto know is that both the server and the client “stub” have a SayHello RPCmethod that takes a HelloRequest parameter from the client and returns aHelloReply from the server, and that this method is defined like this:

  1. // The greeting service definition.
  2. service Greeter {
  3. // Sends a greeting
  4. rpc SayHello (HelloRequest) returns (HelloReply) {}
  5. }
  6. // The request message containing the user's name.
  7. message HelloRequest {
  8. string name = 1;
  9. }
  10. // The response message containing the greetings
  11. message HelloReply {
  12. string message = 1;
  13. }

Let’s update this so that the Greeter service has two methods. Editexamples/protos/helloworld.proto and update it with a new SayHelloAgainmethod, with the same request and response types:

  1. // The greeting service definition.
  2. service Greeter {
  3. // Sends a greeting
  4. rpc SayHello (HelloRequest) returns (HelloReply) {}
  5. // Sends another greeting
  6. rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
  7. }
  8. // The request message containing the user's name.
  9. message HelloRequest {
  10. string name = 1;
  11. }
  12. // The response message containing the greetings
  13. message HelloReply {
  14. string message = 1;
  15. }

Remember to save the file!

Generate gRPC code

Next we need to update the gRPC code used by our application to use the newservice definition.

From the examples/python/helloworld directory, run:

  1. $ python -m grpc_tools.protoc -I../../protos --python_out=. --grpc_python_out=. ../../protos/helloworld.proto

This regenerates helloworld_pb2.py which contains our generated request andresponse classes and helloworld_pb2_grpc.py which contains our generatedclient and server classes.

Update and run the application

We now have new generated server and client code, but we still need to implementand call the new method in the human-written parts of our example application.

Update the server

In the same directory, open greeter_server.py. Implement the new method likethis:

  1. class Greeter(helloworld_pb2_grpc.GreeterServicer):
  2. def SayHello(self, request, context):
  3. return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name)
  4. def SayHelloAgain(self, request, context):
  5. return helloworld_pb2.HelloReply(message='Hello again, %s!' % request.name)
  6. ...

Update the client

In the same directory, open greeter_client.py. Call the new method like this:

  1. def run():
  2. channel = grpc.insecure_channel('localhost:50051')
  3. stub = helloworld_pb2_grpc.GreeterStub(channel)
  4. response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'))
  5. print("Greeter client received: " + response.message)
  6. response = stub.SayHelloAgain(helloworld_pb2.HelloRequest(name='you'))
  7. print("Greeter client received: " + response.message)

Run!

Just like we did before, from the examples/python/helloworld directory:

  • Run the server:
  1. $ python greeter_server.py
  • From another terminal, run the client:
  1. $ python greeter_client.py

What’s next