2017-06-06 21:22:03 +00:00
|
|
|
import json
|
|
|
|
import socket
|
2018-03-09 15:20:34 +00:00
|
|
|
import time
|
2017-06-06 21:22:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def print_dict(d):
|
2018-03-20 00:40:09 +00:00
|
|
|
print(json.dumps(d, indent=2))
|
2017-06-06 21:22:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
class JSONRPCClient(object):
|
2018-03-09 15:20:34 +00:00
|
|
|
def __init__(self, addr, port=None, verbose=False, timeout=60.0):
|
2018-02-05 20:29:05 +00:00
|
|
|
self.verbose = verbose
|
2018-03-09 15:20:34 +00:00
|
|
|
self.timeout = timeout
|
2017-06-06 21:22:03 +00:00
|
|
|
if addr.startswith('/'):
|
|
|
|
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
|
|
self.sock.connect(addr)
|
2018-02-19 07:09:47 +00:00
|
|
|
elif ':' in addr:
|
|
|
|
for res in socket.getaddrinfo(addr, port, socket.AF_INET6, socket.SOCK_STREAM, socket.SOL_TCP):
|
|
|
|
af, socktype, proto, canonname, sa = res
|
|
|
|
self.sock = socket.socket(af, socktype, proto)
|
|
|
|
self.sock.connect(sa)
|
2017-06-06 21:22:03 +00:00
|
|
|
else:
|
|
|
|
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
self.sock.connect((addr, port))
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
self.sock.close()
|
|
|
|
|
|
|
|
def call(self, method, params={}, verbose=False):
|
|
|
|
req = {}
|
|
|
|
req['jsonrpc'] = '2.0'
|
|
|
|
req['method'] = method
|
|
|
|
req['id'] = 1
|
|
|
|
if (params):
|
|
|
|
req['params'] = params
|
|
|
|
reqstr = json.dumps(req)
|
|
|
|
|
2018-02-05 20:29:05 +00:00
|
|
|
verbose = verbose or self.verbose
|
|
|
|
|
2017-06-06 21:22:03 +00:00
|
|
|
if verbose:
|
|
|
|
print("request:")
|
|
|
|
print(json.dumps(req, indent=2))
|
|
|
|
|
2018-03-20 00:43:34 +00:00
|
|
|
self.sock.sendall(reqstr.encode("utf-8"))
|
2017-06-06 21:22:03 +00:00
|
|
|
buf = ''
|
|
|
|
closed = False
|
|
|
|
response = {}
|
2018-03-09 15:20:34 +00:00
|
|
|
start_time = time.clock()
|
|
|
|
|
2017-06-06 21:22:03 +00:00
|
|
|
while not closed:
|
|
|
|
try:
|
2018-03-09 15:20:34 +00:00
|
|
|
timeout = self.timeout - (time.clock() - start_time)
|
|
|
|
if timeout <= 0.0:
|
|
|
|
break
|
|
|
|
|
|
|
|
self.sock.settimeout(timeout)
|
|
|
|
newdata = self.sock.recv(4096)
|
|
|
|
if (newdata == b''):
|
|
|
|
closed = True
|
|
|
|
|
2018-03-20 00:43:34 +00:00
|
|
|
buf += newdata.decode("utf-8")
|
2017-06-06 21:22:03 +00:00
|
|
|
response = json.loads(buf)
|
2018-03-09 15:20:34 +00:00
|
|
|
except socket.timeout:
|
|
|
|
break
|
2017-06-06 21:22:03 +00:00
|
|
|
except ValueError:
|
|
|
|
continue # incomplete response; keep buffering
|
|
|
|
break
|
|
|
|
|
|
|
|
if not response:
|
|
|
|
if method == "kill_instance":
|
|
|
|
exit(0)
|
2018-03-09 15:20:34 +00:00
|
|
|
if closed:
|
2018-03-20 00:40:09 +00:00
|
|
|
print("Connection closed with partial response:")
|
2018-03-09 15:20:34 +00:00
|
|
|
else:
|
2018-03-20 00:40:09 +00:00
|
|
|
print("Timeout while waiting for response:")
|
|
|
|
print(buf)
|
2017-06-06 21:22:03 +00:00
|
|
|
exit(1)
|
|
|
|
|
|
|
|
if 'error' in response:
|
2018-03-20 00:40:09 +00:00
|
|
|
print("Got JSON-RPC error response")
|
|
|
|
print("request:")
|
2017-06-06 21:22:03 +00:00
|
|
|
print_dict(json.loads(reqstr))
|
2018-03-20 00:40:09 +00:00
|
|
|
print("response:")
|
2017-06-06 21:22:03 +00:00
|
|
|
print_dict(response['error'])
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
if verbose:
|
|
|
|
print("response:")
|
|
|
|
print(json.dumps(response, indent=2))
|
|
|
|
|
|
|
|
return response['result']
|