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