-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_grpc.py
82 lines (77 loc) · 2.72 KB
/
client_grpc.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
70
71
72
73
74
75
76
77
78
79
80
81
82
import sys
import grpc
import mykvserver_pb2
import mykvserver_pb2_grpc
from debugger import debugger
def invalid_input():
print("PUT usage: python client.py PUT address 'key' 'value'")
print("GET usage: python client.py GET address 'key'")
print("DEL usage: python client.py DEL address 'key'")
print("Format: address: http://ip:port")
input()
exit()
# client put request
if __name__ == "__main__":
print(' * this is grpc client')
action = sys.argv[1]
addr = sys.argv[2]
key = sys.argv[3]
while True:
channel = grpc.insecure_channel(addr)
# print(addr)
if len(sys.argv) == 4:
# addr, key
# get,del
stub = mykvserver_pb2_grpc.KVServerStub(channel)
response = None
if action=='get' or action=='GET':
msg = mykvserver_pb2.GetMessage()
msg.type = 'get'
msg.payload.act = 'get'
msg.payload.key = key
msg.payload.value = ''
response = stub.GetRequest(msg)
if response.payload.message:
print('redirect to leader',response.payload.message)
addr = response.payload.message
else:
print(response)
break
elif action=='del' or action=='DEL':
msg = mykvserver_pb2.DelMessage()
msg.type = 'del'
msg.payload.act = 'del'
msg.payload.key = key
msg.payload.value = ''
response = stub.DelRequest(msg)
if response.payload.message:
print('redirect to leader',response.payload.message)
addr = response.payload.message
else:
print(response)
break
else:
invalid_input()
elif len(sys.argv) == 5:
# addr, key value
# put
val = sys.argv[4]
if action=='put'or action=='PUT':
stub = mykvserver_pb2_grpc.KVServerStub(channel)
msg = mykvserver_pb2.PutMessage()
msg.type = 'put'
msg.payload.act = 'put'
msg.payload.key = key
msg.payload.value = val
response = stub.PutRequest(msg)
if response.payload.message:
print('redirect to leader',response.payload.message)
addr = response.payload.message
else:
print(response)
break
#print(response)
else:
invalid_input()
else:
invalid_input()