blob: 070b9e8837690ec9354c8cbe1c027347551606b1 [file] [log] [blame] [view]
LisaFCc818cbd2015-03-31 16:46:45 +01001# gRPC Python Hello World
2
Stanley Cheung56debcb2015-08-31 12:17:34 -07003This is a quick introduction with a simple example and installation instructions: for a more complete tutorial see [gRPC Basics: 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
Stanley Cheung0a268212015-08-27 14:38:38 -070015The example code for our Hello World and our other examples live in the `examples`
16directory. Clone this repository to your local machine by running the
Jan Tattermusch7dfd4ab2015-02-25 15:00:46 -080017following command:
18
19
20```sh
Stanley Cheung0a268212015-08-27 14:38:38 -070021$ git clone https://github.com/grpc/grpc.git
Jan Tattermusch7dfd4ab2015-02-25 15:00:46 -080022```
23
Stanley Cheung0a268212015-08-27 14:38:38 -070024Change your current directory to examples/python/helloworld
Jan Tattermusch7dfd4ab2015-02-25 15:00:46 -080025
26```sh
Stanley Cheung0a268212015-08-27 14:38:38 -070027$ cd examples/python/helloworld/
Jan Tattermusch7dfd4ab2015-02-25 15:00:46 -080028```
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
Christian Svenssondb0e3d62015-06-06 13:28:42 +010042Here's our example service definition. The `Greeting`
Jan Tattermusch7dfd4ab2015-02-25 15:00:46 -080043service has one method, `hello`, that lets the server receive a single
44`HelloRequest`
45message from the remote client containing the user's name, then send back
46a greeting in a single `HelloReply`. This is the simplest type of RPC you
47can specify in gRPC.
48
49```
wilson4d059dd2015-03-26 17:49:03 +080050syntax = "proto3";
51
52option java_package = "io.grpc.examples";
53
54package helloworld;
Jan Tattermusch7dfd4ab2015-02-25 15:00:46 -080055
56// The greeting service definition.
57service Greeter {
58 // Sends a greeting
59 rpc SayHello (HelloRequest) returns (HelloReply) {}
60}
61
62// The request message containing the user's name.
63message HelloRequest {
wilson4d059dd2015-03-26 17:49:03 +080064 string name = 1;
Jan Tattermusch7dfd4ab2015-02-25 15:00:46 -080065}
66
67// The response message containing the greetings
68message HelloReply {
wilson4d059dd2015-03-26 17:49:03 +080069 string message = 1;
Jan Tattermusch7dfd4ab2015-02-25 15:00:46 -080070}
71
72```
73
74<a name="generating"></a>
75### Generating gRPC code
76
77Once 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
79our application. The generated code contains both stub code for clients to
80use and an abstract interface for servers to implement, both with the method
81defined in our `Greeting` service.
82
83To generate the client and server side interfaces:
84
85```sh
86$ ./run_codegen.sh
87```
88Which internally invokes the proto-compiler as:
89
90```sh
wilson4d059dd2015-03-26 17:49:03 +080091$ 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 -080092```
93
Jan Tattermusch7dfd4ab2015-02-25 15:00:46 -080094### The client
95
Stanley Cheung56debcb2015-08-31 12:17:34 -070096Client-side code can be found in [greeter_client.py](greeter_client.py).
Jan Tattermusch7dfd4ab2015-02-25 15:00:46 -080097
98You can run the client using:
99
100```sh
101$ ./run_client.sh
102```
103
104
105### The server
106
Stanley Cheung56debcb2015-08-31 12:17:34 -0700107Server side code can be found in [greeter_server.py](greeter_server.py).
Jan Tattermusch7dfd4ab2015-02-25 15:00:46 -0800108
109You can run the server using:
110
111```sh
112$ ./run_server.sh
113```