-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkv-cli.py
96 lines (84 loc) · 3.19 KB
/
kv-cli.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
import cmd
import requests
import socket
import errno
from socket import error as socket_error
server_ip = '127.0.0.1'
server_port = 12345
class KVShell(cmd.Cmd):
intro = "CLI Client to consume the KV Web API. Type help or ? to list commands. To exit press 'Ctrl + D' or fire 'quit' command."
prompt = 'kvstore> '
#CLI command to retrieve the value of a particular key.
def do_get(self, key):
if len(key.split()) == 1:
params = {'key': key}
api_url = 'http://127.0.0.1:5000/get'
response = requests.get(api_url,params=params)
print(response.content)
else:
print("Error: Incorrect Input! Please follow the syntax - get <key>")
#Description for the get function mentioned above.
def help_get(self):
print("Get the value of the provided key.")
print("Syntax - get <key>")
#CLI commnd to add value for a particular key in the KV Store.
def do_put(self,input):
global server_port
global server_ip
if len(input.split()) == 2:
key,value = [str(s) for s in input.split()]
params = {'key': key, 'value': value}
api_url = 'http://127.0.0.1:5000/add'
response = requests.post(api_url,params=params)
print(response.content)
try:
sock = socket.socket()
server_address = (server_ip,server_port)
sock.connect(server_address)
sock.sendall(response.content)
except socket_error as serr:
if serr.errno != errno.ECONNREFUSED:
raise serr
else:
print("Error: Incorrect Input! Please follow the syntax - set <key> <value>")
#Description for the set function mentioned above.
def help_put(self):
print("Set the value of the provided key.")
print("Syntax - set <key> <value>")
#CLI command to subscribe to the changes to the KV store.
def do_watch(self,line):
"""Subcribe to the changes happening in the KV store."""
global server_ip
global server_port
try:
sock = socket.socket()
sock.bind((server_ip,server_port))
sock.listen(5)
while True:
client,addr = sock.accept()
print(client.recv(1024))
client.close()
except KeyboardInterrupt:
print("\nGot keyboard interrupt, exiting!!")
#CLI command to exit from the CLI shell.
def do_quit(self, line):
"""To exit the shell."""
return True
#Ctrl + D to exit from the CLI shell.
def do_EOF(self, line):
"""To exit the shell."""
return True
#Get all items in the KV store.
def do_getall(self,line):
if len(line) > 1:
print("Incorrect Syntax. Please follow the syntax - getall")
else:
api_url = 'http://127.0.0.1:5000/get_all'
response = requests.get(api_url)
print(response.content)
#Description for the above mentioned getall command.
def help_getall(self):
print("Get all the KV pairs in the store.")
print("Syntax - getall")
if __name__ == '__main__':
KVShell().cmdloop()