-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreet_client.py
54 lines (45 loc) · 1.83 KB
/
greet_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import time
import greet_pb2_grpc
import greet_pb2
import grpc
def get_client_stream_requests():
while True:
name = input("Please enter a name (or nothing to stop chatting): ")
if name == "":
break
hello_request = greet_pb2.HelloRequest(greeting="Hello", name=name)
yield hello_request
time.sleep(1)
def run():
with grpc.insecure_channel('localhost:50051') as channel:
stub = greet_pb2_grpc.GreeterStub(channel)
print("1. SayHello - Unary")
print("2. ParrotSaysHello - Server Side Streaming")
print("3. ChattyClientSaysHello - Client Side Streaming")
print("4. InteractingHello - Both Streaming")
rpc_call = input("Which rpc would you like to make: ")
if rpc_call == "1":
hello_request = greet_pb2.HelloRequest(
greeting="Salar", name="Mohammad")
hello_reply = stub.SayHello(hello_request)
print("SayHello Response Received:")
print(hello_reply)
elif rpc_call == "2":
hello_request = greet_pb2.HelloRequest(
greeting="Salar", name="Mohammad")
hello_replies = stub.ParrotSaysHello(hello_request)
for hello_reply in hello_replies:
print("ParrotSaysHello Response Received:")
print(hello_reply)
elif rpc_call == "3":
delayed_reply = stub.ChattyClientSaysHello(
get_client_stream_requests())
print("ChattyClientSaysHello Response Received:")
print(delayed_reply)
elif rpc_call == "4":
responses = stub.InteractingHello(get_client_stream_requests())
for response in responses:
print("InteractingHello Response Received: ")
print(response)
if __name__ == "__main__":
run()