The top level client is unchanged. This is primarily just moving code around. The client.py file is the only location with new code, which converts the old jsonrpc_call function into a class. Change-Id: I5fb7cd48f77f6affa3d9439128009bf63148acda Signed-off-by: Ben Walker <benjamin.walker@intel.com> Reviewed-on: https://review.gerrithub.io/364316 Reviewed-by: Daniel Verkamp <daniel.verkamp@intel.com> Tested-by: SPDK Automated Test System <sys_sgsw@intel.com> Reviewed-by: Jim Harris <james.r.harris@intel.com>
82 lines
2.0 KiB
Python
Executable File
82 lines
2.0 KiB
Python
Executable File
import json
|
|
import socket
|
|
|
|
try:
|
|
from shlex import quote
|
|
except ImportError:
|
|
from pipes import quote
|
|
|
|
|
|
def print_dict(d):
|
|
print json.dumps(d, indent=2)
|
|
|
|
|
|
def print_array(a):
|
|
print " ".join((quote(v) for v in a))
|
|
|
|
|
|
def int_arg(arg):
|
|
return int(arg, 0)
|
|
|
|
|
|
class JSONRPCClient(object):
|
|
def __init__(self, addr, port=None):
|
|
if addr.startswith('/'):
|
|
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
self.sock.connect(addr)
|
|
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)
|
|
|
|
if verbose:
|
|
print("request:")
|
|
print(json.dumps(req, indent=2))
|
|
|
|
self.sock.sendall(reqstr)
|
|
buf = ''
|
|
closed = False
|
|
response = {}
|
|
while not closed:
|
|
newdata = self.sock.recv(4096)
|
|
if (newdata == b''):
|
|
closed = True
|
|
buf += newdata
|
|
try:
|
|
response = json.loads(buf)
|
|
except ValueError:
|
|
continue # incomplete response; keep buffering
|
|
break
|
|
|
|
if not response:
|
|
if method == "kill_instance":
|
|
exit(0)
|
|
print "Connection closed with partial response:"
|
|
print buf
|
|
exit(1)
|
|
|
|
if 'error' in response:
|
|
print "Got JSON-RPC error response"
|
|
print "request:"
|
|
print_dict(json.loads(reqstr))
|
|
print "response:"
|
|
print_dict(response['error'])
|
|
exit(1)
|
|
|
|
if verbose:
|
|
print("response:")
|
|
print(json.dumps(response, indent=2))
|
|
|
|
return response['result']
|