-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
69 lines (53 loc) · 2.03 KB
/
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import time
import grpc
import greet_pb2
import greet_pb2_grpc
def get_client_requests_stream():
"""
Function that yeild different requests because multiple requests are to be made
to the server before the client gets final response from the sever.
"""
while True:
name = input("Please enter a name (leave blank to stop chat): ")
if name == "":
break
hello_request = greet_pb2.HelloRequest(greeting="Hello", name=name)
yield hello_request
time.sleep(1)
def run():
print(
"""-- Welcome to gRPC application ---
1. SayHello - Unary Call
2. ParrotSaysHello - Server Side Streaming
3. ChattySaysHello - Client Side Streaming
4. InteractingHello - Binary Streaming
"""
)
rpc_call = input("Which RPC would you like to call: ")
if rpc_call == "1":
hello_request = greet_pb2.HelloRequest(greeting="Namaste!", name="gRPC")
hello_reply = stub.SayHello(hello_request)
print("Unary Call Made.\nSayHello Responded: \n", hello_reply)
elif rpc_call == "2":
hello_request = greet_pb2.HelloRequest(greeting="Namaste!", name="gRPC")
hello_replies = stub.ParrotSaysHello(hello_request)
for hello_reply in hello_replies:
print("Server Side Stream Response Received: ")
print(hello_reply)
elif rpc_call == "3":
delayed_reply = stub.ChattyClientSaysHello(get_client_requests_stream())
print("Client Side Stream Response Received: ")
print(delayed_reply)
elif rpc_call == "4":
responses = stub.InteractingHello(get_client_requests_stream())
for response in responses:
print("Binary Streaming Response Received: ")
print(response)
else:
print("Invalid Option")
return False
if __name__ == "__main__":
with grpc.insecure_channel("localhost:50051") as channel:
stub = greet_pb2_grpc.GreeterStub(channel)
while True:
run()