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