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 |
Mugur Marculescu | 22ad36e | 2015-02-25 22:06:44 -0800 | [diff] [blame] | 19 | $ git clone https://github.com/grpc/grpc-common.git |
Jan Tattermusch | 7dfd4ab | 2015-02-25 15:00:46 -0800 | [diff] [blame] | 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 | ``` |
wilson | 4d059dd | 2015-03-26 17:49:03 +0800 | [diff] [blame^] | 49 | syntax = "proto3"; |
| 50 | |
| 51 | option java_package = "io.grpc.examples"; |
| 52 | |
| 53 | package helloworld; |
Jan Tattermusch | 7dfd4ab | 2015-02-25 15:00:46 -0800 | [diff] [blame] | 54 | |
| 55 | // The greeting service definition. |
| 56 | service Greeter { |
| 57 | // Sends a greeting |
| 58 | rpc SayHello (HelloRequest) returns (HelloReply) {} |
| 59 | } |
| 60 | |
| 61 | // The request message containing the user's name. |
| 62 | message HelloRequest { |
wilson | 4d059dd | 2015-03-26 17:49:03 +0800 | [diff] [blame^] | 63 | string name = 1; |
Jan Tattermusch | 7dfd4ab | 2015-02-25 15:00:46 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // The response message containing the greetings |
| 67 | message HelloReply { |
wilson | 4d059dd | 2015-03-26 17:49:03 +0800 | [diff] [blame^] | 68 | string message = 1; |
Jan Tattermusch | 7dfd4ab | 2015-02-25 15:00:46 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | ``` |
| 72 | |
| 73 | <a name="generating"></a> |
| 74 | ### Generating gRPC code |
| 75 | |
| 76 | Once we've defined our service, we use the protocol buffer compiler |
| 77 | `protoc` to generate the special client and server code we need to create |
| 78 | our application. The generated code contains both stub code for clients to |
| 79 | use and an abstract interface for servers to implement, both with the method |
| 80 | defined in our `Greeting` service. |
| 81 | |
| 82 | To generate the client and server side interfaces: |
| 83 | |
| 84 | ```sh |
| 85 | $ ./run_codegen.sh |
| 86 | ``` |
| 87 | Which internally invokes the proto-compiler as: |
| 88 | |
| 89 | ```sh |
wilson | 4d059dd | 2015-03-26 17:49:03 +0800 | [diff] [blame^] | 90 | $ protoc -I ../../protos --python_out=. --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_python_plugin` ../../protos/helloworld.proto |
Jan Tattermusch | 7dfd4ab | 2015-02-25 15:00:46 -0800 | [diff] [blame] | 91 | ``` |
| 92 | |
| 93 | Optionally, you can just skip the code generation step as the generated python module has already |
| 94 | been generated for you (helloworld_pb2.py). |
| 95 | |
| 96 | ### The client |
| 97 | |
| 98 | Client-side code can be found in [greeter_client.py](https://github.com/grpc/grpc-common/blob/master/python/helloworld/greeter_client.py). |
| 99 | |
| 100 | You can run the client using: |
| 101 | |
| 102 | ```sh |
| 103 | $ ./run_client.sh |
| 104 | ``` |
| 105 | |
| 106 | |
| 107 | ### The server |
| 108 | |
| 109 | Server side code can be found in [greeter_server.py](https://github.com/grpc/grpc-common/blob/master/python/helloworld/greeter_server.py). |
| 110 | |
| 111 | You can run the server using: |
| 112 | |
| 113 | ```sh |
| 114 | $ ./run_server.sh |
| 115 | ``` |