Jan Tattermusch | 7dfd4ab | 2015-02-25 15:00:46 -0800 | [diff] [blame] | 1 | # gRPC Python Hello World Tutorial |
| 2 | |
| 3 | ### Install gRPC |
| 4 | Make sure you have built gRPC Python from source on your system. Follow the instructions here: |
| 5 | [https://github.com/grpc/grpc/blob/master/src/python/README.md](https://github.com/grpc/grpc/blob/master/src/python/README.md). |
| 6 | |
| 7 | This gives you a python virtual environment with installed gRPC Python |
| 8 | in GRPC_ROOT/python2.7_virtual_environment. GRPC_ROOT is the path to which you |
| 9 | have cloned the [gRPC git repo](https://github.com/grpc/grpc). |
| 10 | |
| 11 | ### Get the tutorial source code |
| 12 | |
| 13 | The example code for this and our other examples live in the `grpc-common` |
| 14 | GitHub repository. Clone this repository to your local machine by running the |
| 15 | following command: |
| 16 | |
| 17 | |
| 18 | ```sh |
| 19 | $ git clone https://github.com/google/grpc-common.git |
| 20 | ``` |
| 21 | |
| 22 | Change your current directory to grpc-common/python/helloworld |
| 23 | |
| 24 | ```sh |
| 25 | $ cd grpc-common/python/helloworld/ |
| 26 | ``` |
| 27 | |
| 28 | ### Defining a service |
| 29 | |
| 30 | The first step in creating our example is to define a *service*: an RPC |
| 31 | service specifies the methods that can be called remotely with their parameters |
| 32 | and return types. As you saw in the |
| 33 | [overview](#protocolbuffers) above, gRPC does this using [protocol |
| 34 | buffers](https://developers.google.com/protocol-buffers/docs/overview). We |
| 35 | use the protocol buffers interface definition language (IDL) to define our |
| 36 | service methods, and define the parameters and return |
| 37 | types as protocol buffer message types. Both the client and the |
| 38 | server use interface code generated from the service definition. |
| 39 | |
| 40 | Here's our example service definition, defined using protocol buffers IDL in |
| 41 | [helloworld.proto](https://github.com/grpc/grpc-common/blob/master/python/helloworld/helloworld.proto). The `Greeting` |
| 42 | service has one method, `hello`, that lets the server receive a single |
| 43 | `HelloRequest` |
| 44 | message from the remote client containing the user's name, then send back |
| 45 | a greeting in a single `HelloReply`. This is the simplest type of RPC you |
| 46 | can specify in gRPC. |
| 47 | |
| 48 | ``` |
| 49 | syntax = "proto2"; |
| 50 | |
| 51 | // The greeting service definition. |
| 52 | service Greeter { |
| 53 | // Sends a greeting |
| 54 | rpc SayHello (HelloRequest) returns (HelloReply) {} |
| 55 | } |
| 56 | |
| 57 | // The request message containing the user's name. |
| 58 | message HelloRequest { |
| 59 | optional string name = 1; |
| 60 | } |
| 61 | |
| 62 | // The response message containing the greetings |
| 63 | message HelloReply { |
| 64 | optional string message = 1; |
| 65 | } |
| 66 | |
| 67 | ``` |
| 68 | |
| 69 | <a name="generating"></a> |
| 70 | ### Generating gRPC code |
| 71 | |
| 72 | Once we've defined our service, we use the protocol buffer compiler |
| 73 | `protoc` to generate the special client and server code we need to create |
| 74 | our application. The generated code contains both stub code for clients to |
| 75 | use and an abstract interface for servers to implement, both with the method |
| 76 | defined in our `Greeting` service. |
| 77 | |
| 78 | To generate the client and server side interfaces: |
| 79 | |
| 80 | ```sh |
| 81 | $ ./run_codegen.sh |
| 82 | ``` |
| 83 | Which internally invokes the proto-compiler as: |
| 84 | |
| 85 | ```sh |
| 86 | $ protoc -I . --python_out=. --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_python_plugin` helloworld.proto |
| 87 | ``` |
| 88 | |
| 89 | Optionally, you can just skip the code generation step as the generated python module has already |
| 90 | been generated for you (helloworld_pb2.py). |
| 91 | |
| 92 | ### The client |
| 93 | |
| 94 | Client-side code can be found in [greeter_client.py](https://github.com/grpc/grpc-common/blob/master/python/helloworld/greeter_client.py). |
| 95 | |
| 96 | You can run the client using: |
| 97 | |
| 98 | ```sh |
| 99 | $ ./run_client.sh |
| 100 | ``` |
| 101 | |
| 102 | |
| 103 | ### The server |
| 104 | |
| 105 | Server side code can be found in [greeter_server.py](https://github.com/grpc/grpc-common/blob/master/python/helloworld/greeter_server.py). |
| 106 | |
| 107 | You can run the server using: |
| 108 | |
| 109 | ```sh |
| 110 | $ ./run_server.sh |
| 111 | ``` |