-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
103 lines (83 loc) · 2.74 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import logging
import requests
logging.basicConfig(format='%(asctime)s,%(msecs)d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s',
datefmt='%Y-%m-%d:%H:%M:%S', level=logging.INFO)
def post(node, key, value, sync=False):
msg = {
"type": "set",
"key": key,
"value": value,
"sync": sync
}
return requests.post(node, params=msg)
def get(node, key, sync=False):
msg = {
"type": "get",
"key": key,
"sync": sync
}
return requests.get(node, params=msg)
def lock(node, lock_type, key, sync=True):
msg = {
"type": lock_type,
"key": key,
"sync": sync
}
return requests.post(node, params=msg)
def txn(node, commands):
data = []
for cmd in commands[1:]:
data.append(cmd.strip())
msg = {
"type": "transaction",
"client_id": commands[0].split()[1],
"commands": data
}
return requests.get(node, params=msg)
def main():
while True:
try:
inp = input(">> ")
cmd = inp.split()
args = []
if ';' in inp:
args.extend(list(filter(None, inp.split(";"))))
else:
args.append('txn 0')
args.append(inp)
result = txn("http://localhost:8000", args)
#
# if not cmd:
# continue
# elif cmd[0] == 'set':
# result = post("http://localhost:8000", cmd[1], cmd[2])
# elif cmd[0] == 'get':
# result = get("http://localhost:8000", cmd[1])
# elif cmd[0] == 'lock' or cmd[0] == 'unlock':
# result = lock("http://localhost:8000", cmd[0], cmd[1])
# elif cmd[0] == 'txn':
# raw_comm = list(filter(None, inp.split(";")))
#
# result = txn("http://localhost:8000", raw_comm[1:])
# else:
# print('Usage: set <key> <value>')
# print('\t get <key>')
# continue
if result.status_code == 200:
print(result.text)
elif result.status_code == 201 or result.status_code == 202:
print("Done!")
elif result.status_code == 404:
print(f"Key: {cmd[1]} not found!")
elif result.status_code == 409:
print(f"Unable to lock {cmd[1]}!")
else:
print(f"Error looking up key: {cmd[1]}")
except Exception as e:
logging.error("Encountered an error: %s", e)
# running in background
while True:
# just wait for commands to _g_kvstorage
continue
if __name__ == '__main__':
main()