Skip to content

Commit 9171d0e

Browse files
Getting started with grpc python (unary and streaming) (#7)
Co-authored-by: Arvind Bright <[email protected]>
1 parent c23c312 commit 9171d0e

29 files changed

+3975
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Here is a list of available codelabs in this repository:
2222
- [Getting Started with gRPC-Go (Streaming)](codelabs/Getting_Started_with_gRPC_Go_Streaming)
2323
- [Getting Started with gRPC-Java](codelabs/Getting_Started_with_gRPC_Java)
2424
- [Getting Started with gRPC-Java (Streaming)](codelabs/Getting_Started_with_gRPC_Java_Streaming)
25+
- [Getting Started with gRPC-Python](codelabs/Getting_Started_with_gRPC_Python)
26+
- [Getting Started with gRPC-Python (Streaming)](codelabs/Getting_Started_with_gRPC_Python_streaming)
2527
- [Setup Basic gRPC OpenTelemetry Plugin in gRPC C++](codelabs/Setup_Basic_gRPC_OpenTelemetry_Plugin_in_gRPC_CPP)
2628
- [Setup Basic gRPC OpenTelemetry Plugin in gRPC-Python](codelabs/Setup_Basic_gRPC_OpenTelemetry_Plugin_in_gRPC_Python)
2729
- [Setup Basic gRPC OpenTelemetry Plugin in gRPC-Go](codelabs/Setup_Basic_gRPC_OpenTelemetry_Plugin_in_gRPC_Go/)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Getting Started with gRPC-Python
2+
3+
Get hands-on with gRPC for Python in this interactive codelab! <!-- TODO(arvindbr8): Insert link once codelab is published. -->
4+
5+
6+
Perfect for Python developers new to gRPC, those seeking a refresher, or anyone building distributed systems. No prior gRPC experience needed!
7+
8+
#### Build a complete gRPC service from scratch, learning:
9+
- Protocol Buffers (protobuf): Define service contracts & data.
10+
- gRPC Code Generation: Auto-generate Python code.
11+
- Client/Server Communication: Implement seamless interactions.
12+
13+
#### You'll gain:
14+
- A working gRPC service in Python.
15+
- Hands-on experience with Protocol Buffers and code generation.
16+
- Skills to design, build, & test gRPC clients and servers.
17+
- A strong foundation in gRPC for real-world projects.
18+
19+
## How to use this directory
20+
21+
- [start_here](start_here/) directory serves as a starting point for the
22+
codelab.
23+
- [completed](completed/) directory showcases the finished code, giving you a
24+
peak of how the final implementation should look like.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2024 gRPC authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
syntax = "proto3";
16+
17+
option java_multiple_files = true;
18+
option java_package = "io.grpc.examples.routeguide";
19+
option java_outer_classname = "RouteGuideProto";
20+
option objc_class_prefix = "RTG";
21+
22+
package routeguide;
23+
24+
// Interface exported by the server.
25+
service RouteGuide {
26+
// A simple RPC.
27+
//
28+
// Obtains the feature at a given position.
29+
//
30+
// A feature with an empty name is returned if there's no feature at the given
31+
// position.
32+
rpc GetFeature(Point) returns (Feature) {}
33+
}
34+
35+
// Points are represented as latitude-longitude pairs in the E7 representation
36+
// (degrees multiplied by 10**7 and rounded to the nearest integer).
37+
// Latitudes should be in the range +/- 90 degrees and longitude should be in
38+
// the range +/- 180 degrees (inclusive).
39+
message Point {
40+
int32 latitude = 1;
41+
int32 longitude = 2;
42+
}
43+
44+
// A feature names something at a given point.
45+
//
46+
// If a feature could not be named, the name is empty.
47+
message Feature {
48+
// The name of the feature.
49+
string name = 1;
50+
51+
// The point where the feature is detected.
52+
Point location = 2;
53+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright 2024 gRPC authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""The Python implementation of the gRPC route guide client."""
15+
16+
import logging
17+
18+
import grpc
19+
import route_guide_pb2
20+
import route_guide_pb2_grpc
21+
22+
def format_point(point):
23+
# not delegating in point.__str__ because it is an empty string when its
24+
# values are zero. In addition, it puts a newline between the fields.
25+
return "latitude: %d, longitude: %d" % (point.latitude, point.longitude)
26+
27+
def run():
28+
point = route_guide_pb2.Point(latitude=409146138, longitude=-746188906)
29+
channel = grpc.insecure_channel('localhost:50051')
30+
stub = route_guide_pb2_grpc.RouteGuideStub(channel)
31+
feature = stub.GetFeature(point)
32+
if not feature.location:
33+
print("Server returned incomplete feature")
34+
return
35+
36+
if feature.name:
37+
print(
38+
"Feature called %r at %s"
39+
% (feature.name, format_point(feature.location))
40+
)
41+
else:
42+
print("Found no feature at %s" % format_point(feature.location))
43+
channel.close()
44+
45+
if __name__ == "__main__":
46+
logging.basicConfig()
47+
run()

0 commit comments

Comments
 (0)