-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
59 lines (48 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
import time
import grpc
import os
import sys
# Add the protobufs module directory to sys.path
parent_dir = os.path.dirname(os.path.abspath(__file__))
protobuf_dir = os.path.join(parent_dir, 'protobufs')
sys.path.append(protobuf_dir)
from protobufs import greet_pb2
from protobufs import greet_pb2_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 = "Bonjour", name = "YouTube")
hello_reply = stub.SayHello(hello_request)
print("SayHello Response Received:")
print(hello_reply)
elif rpc_call == "2":
hello_request = greet_pb2.HelloRequest(greeting = "Bonjour", name = "YouTube")
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()