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