blob: e20022366b37c3c6891f8a110a64d4426336314d [file] [log] [blame] [view]
LisaFCc818cbd2015-03-31 16:46:45 +01001# gRPC Python Hello World
2
3This is a quick introduction with a simple example and installation instructions: for a more complete tutorial see [gRPC Basics: Python](https://github.com/grpc/grpc-common/tree/master/python/route_guide).
Jan Tattermusch7dfd4ab2015-02-25 15:00:46 -08004
5### Install gRPC
6Make 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
9This gives you a python virtual environment with installed gRPC Python
10in GRPC_ROOT/python2.7_virtual_environment. GRPC_ROOT is the path to which you
11have cloned the [gRPC git repo](https://github.com/grpc/grpc).
12
LisaFCc818cbd2015-03-31 16:46:45 +010013### Get the source code
Jan Tattermusch7dfd4ab2015-02-25 15:00:46 -080014
LisaFCc818cbd2015-03-31 16:46:45 +010015The example code for our Hello World and our other examples live in the `grpc-common`
Jan Tattermusch7dfd4ab2015-02-25 15:00:46 -080016GitHub repository. Clone this repository to your local machine by running the
17following command:
18
19
20```sh
Mugur Marculescu22ad36e2015-02-25 22:06:44 -080021$ git clone https://github.com/grpc/grpc-common.git
Jan Tattermusch7dfd4ab2015-02-25 15:00:46 -080022```
23
24Change your current directory to grpc-common/python/helloworld
25
26```sh
27$ cd grpc-common/python/helloworld/
28```
29
30### Defining a service
31
32The first step in creating our example is to define a *service*: an RPC
33service specifies the methods that can be called remotely with their parameters
34and return types. As you saw in the
35[overview](#protocolbuffers) above, gRPC does this using [protocol
36buffers](https://developers.google.com/protocol-buffers/docs/overview). We
37use the protocol buffers interface definition language (IDL) to define our
38service methods, and define the parameters and return
39types as protocol buffer message types. Both the client and the
40server use interface code generated from the service definition.
41
42Here's our example service definition, defined using protocol buffers IDL in
43[helloworld.proto](https://github.com/grpc/grpc-common/blob/master/python/helloworld/helloworld.proto). The `Greeting`
44service has one method, `hello`, that lets the server receive a single
45`HelloRequest`
46message from the remote client containing the user's name, then send back
47a greeting in a single `HelloReply`. This is the simplest type of RPC you
48can specify in gRPC.
49
50```
wilson4d059dd2015-03-26 17:49:03 +080051syntax = "proto3";
52
53option java_package = "io.grpc.examples";
54
55package helloworld;
Jan Tattermusch7dfd4ab2015-02-25 15:00:46 -080056
57// The greeting service definition.
58service Greeter {
59 // Sends a greeting
60 rpc SayHello (HelloRequest) returns (HelloReply) {}
61}
62
63// The request message containing the user's name.
64message HelloRequest {
wilson4d059dd2015-03-26 17:49:03 +080065 string name = 1;
Jan Tattermusch7dfd4ab2015-02-25 15:00:46 -080066}
67
68// The response message containing the greetings
69message HelloReply {
wilson4d059dd2015-03-26 17:49:03 +080070 string message = 1;
Jan Tattermusch7dfd4ab2015-02-25 15:00:46 -080071}
72
73```
74
75<a name="generating"></a>
76### Generating gRPC code
77
78Once we've defined our service, we use the protocol buffer compiler
79`protoc` to generate the special client and server code we need to create
80our application. The generated code contains both stub code for clients to
81use and an abstract interface for servers to implement, both with the method
82defined in our `Greeting` service.
83
84To generate the client and server side interfaces:
85
86```sh
87$ ./run_codegen.sh
88```
89Which internally invokes the proto-compiler as:
90
91```sh
wilson4d059dd2015-03-26 17:49:03 +080092$ 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 -080093```
94
95Optionally, you can just skip the code generation step as the generated python module has already
96been generated for you (helloworld_pb2.py).
97
98### The client
99
100Client-side code can be found in [greeter_client.py](https://github.com/grpc/grpc-common/blob/master/python/helloworld/greeter_client.py).
101
102You can run the client using:
103
104```sh
105$ ./run_client.sh
106```
107
108
109### The server
110
111Server side code can be found in [greeter_server.py](https://github.com/grpc/grpc-common/blob/master/python/helloworld/greeter_server.py).
112
113You can run the server using:
114
115```sh
116$ ./run_server.sh
117```