route_guide_client.py
Go to the documentation of this file.
1 # Copyright 2015 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 from __future__ import print_function
17 
18 import logging
19 import random
20 
21 import grpc
22 import route_guide_pb2
23 import route_guide_pb2_grpc
24 import route_guide_resources
25 
26 
27 def make_route_note(message, latitude, longitude):
29  message=message,
30  location=route_guide_pb2.Point(latitude=latitude, longitude=longitude))
31 
32 
33 def guide_get_one_feature(stub, point):
34  feature = stub.GetFeature(point)
35  if not feature.location:
36  print("Server returned incomplete feature")
37  return
38 
39  if feature.name:
40  print("Feature called %s at %s" % (feature.name, feature.location))
41  else:
42  print("Found no feature at %s" % feature.location)
43 
44 
47  stub, route_guide_pb2.Point(latitude=409146138, longitude=-746188906))
48  guide_get_one_feature(stub, route_guide_pb2.Point(latitude=0, longitude=0))
49 
50 
52  rectangle = route_guide_pb2.Rectangle(
53  lo=route_guide_pb2.Point(latitude=400000000, longitude=-750000000),
54  hi=route_guide_pb2.Point(latitude=420000000, longitude=-730000000))
55  print("Looking for features between 40, -75 and 42, -73")
56 
57  features = stub.ListFeatures(rectangle)
58 
59  for feature in features:
60  print("Feature called %s at %s" % (feature.name, feature.location))
61 
62 
63 def generate_route(feature_list):
64  for _ in range(0, 10):
65  random_feature = feature_list[random.randint(0, len(feature_list) - 1)]
66  print("Visiting point %s" % random_feature.location)
67  yield random_feature.location
68 
69 
72 
73  route_iterator = generate_route(feature_list)
74  route_summary = stub.RecordRoute(route_iterator)
75  print("Finished trip with %s points " % route_summary.point_count)
76  print("Passed %s features " % route_summary.feature_count)
77  print("Travelled %s meters " % route_summary.distance)
78  print("It took %s seconds " % route_summary.elapsed_time)
79 
80 
82  messages = [
83  make_route_note("First message", 0, 0),
84  make_route_note("Second message", 0, 1),
85  make_route_note("Third message", 1, 0),
86  make_route_note("Fourth message", 0, 0),
87  make_route_note("Fifth message", 1, 0),
88  ]
89  for msg in messages:
90  print("Sending %s at %s" % (msg.message, msg.location))
91  yield msg
92 
93 
94 def guide_route_chat(stub):
95  responses = stub.RouteChat(generate_messages())
96  for response in responses:
97  print("Received message %s at %s" %
98  (response.message, response.location))
99 
100 
101 def run():
102  # NOTE(gRPC Python Team): .close() is possible on a channel and should be
103  # used in circumstances in which the with statement does not fit the needs
104  # of the code.
105  with grpc.insecure_channel('localhost:50051') as channel:
107  print("-------------- GetFeature --------------")
108  guide_get_feature(stub)
109  print("-------------- ListFeatures --------------")
110  guide_list_features(stub)
111  print("-------------- RecordRoute --------------")
112  guide_record_route(stub)
113  print("-------------- RouteChat --------------")
114  guide_route_chat(stub)
115 
116 
117 if __name__ == '__main__':
118  logging.basicConfig()
119  run()
grpc.insecure_channel
def insecure_channel(target, options=None, compression=None)
Definition: src/python/grpcio/grpc/__init__.py:1962
capstone.range
range
Definition: third_party/bloaty/third_party/capstone/bindings/python/capstone/__init__.py:6
route_guide_pb2_grpc.RouteGuideStub
Definition: multiplex/route_guide_pb2_grpc.py:7
route_guide_resources.read_route_guide_database
def read_route_guide_database()
Definition: multiplex/route_guide_resources.py:21
route_guide_client.generate_route
def generate_route(feature_list)
Definition: route_guide_client.py:63
route_guide_pb2.Point
Point
Definition: multiplex/route_guide_pb2.py:242
route_guide_client.guide_route_chat
def guide_route_chat(stub)
Definition: route_guide_client.py:94
route_guide_client.guide_get_one_feature
def guide_get_one_feature(stub, point)
Definition: route_guide_client.py:33
route_guide_client.generate_messages
def generate_messages()
Definition: route_guide_client.py:81
route_guide_client.guide_get_feature
def guide_get_feature(stub)
Definition: route_guide_client.py:45
route_guide_client.guide_list_features
def guide_list_features(stub)
Definition: route_guide_client.py:51
route_guide_pb2.Rectangle
Rectangle
Definition: multiplex/route_guide_pb2.py:249
route_guide_client.make_route_note
def make_route_note(message, latitude, longitude)
Definition: route_guide_client.py:27
route_guide_client.guide_record_route
def guide_record_route(stub)
Definition: route_guide_client.py:70
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
route_guide_client.run
def run()
Definition: route_guide_client.py:101
route_guide_pb2.RouteNote
RouteNote
Definition: multiplex/route_guide_pb2.py:263


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:12