-
Notifications
You must be signed in to change notification settings - Fork 1
/
rshost.py
54 lines (42 loc) · 1.73 KB
/
rshost.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
import json, requests, argparse, sys
class rsHost:
_ip = "127.0.0.1"
_port = "9092"
_auth = ("test", "tset")
def __init__(self, debug: bool = False, parser=None):
if parser is None:
parser = argparse.ArgumentParser(description='reads standard RS json API parameters.')
# parser = argparse.ArgumentParser(
# description="reads standard RS json API parameters."
# )
parser.add_argument("--port", "-p", help="json api port")
parser.add_argument("--addr", "-a", help="json api address")
parser.add_argument("--user", "-u", help="json api user")
parser.add_argument("--pass", "-P", help="json api password", dest="pw")
args = parser.parse_args()
# print(args)
if args.addr is not None:
self._ip = args.addr
if args.port is not None:
self._port = args.port
if args.user is not None and args.pw is not None:
self._auth = (args.user, args.pw)
self._debug = debug
pass
def sendRequest(self, function, data=None):
url = "http://" + self._ip + ":" + self._port + function
self.debugDump("POST: " + url, data)
try:
resp = requests.post(url=url, json=data, auth=self._auth)
except requests.exceptions.ConnectionError:
print(f"failed to connect to {self._ip}:{self._port}")
sys.exit(-1)
# gracefully add 401 error
if resp.status_code == 401:
return {"retval": False}
self.debugDump("RESP", resp.json())
return resp.json()
def debugDump(self, label, data):
if not self._debug:
return
print(label, json.dumps(data, sort_keys=True, indent=4))