scripts/rpc.py: Handle socket connection error
Handle socket exception, print it and exit cleanly. Change-Id: I7ac899dfe329c7512339549991abea8a6cb09608 Signed-off-by: Karol Latecki <karol.latecki@intel.com> Reviewed-on: https://review.gerrithub.io/407308 Tested-by: SPDK Automated Test System <sys_sgsw@intel.com> Reviewed-by: Daniel Verkamp <daniel.verkamp@intel.com> Reviewed-by: Jim Harris <james.r.harris@intel.com> Reviewed-by: Ben Walker <benjamin.walker@intel.com> Reviewed-by: Dariusz Stojaczyk <dariuszx.stojaczyk@intel.com>
This commit is contained in:
parent
b066126b0b
commit
3142d978c3
@ -932,5 +932,9 @@ if __name__ == "__main__":
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
args.client = rpc.client.JSONRPCClient(args.server_addr, args.port, args.verbose, args.timeout)
|
||||
try:
|
||||
args.client = rpc.client.JSONRPCClient(args.server_addr, args.port, args.verbose, args.timeout)
|
||||
except JSONRPCException as ex:
|
||||
print(ex.message)
|
||||
exit(1)
|
||||
args.func(args)
|
||||
|
@ -16,17 +16,21 @@ class JSONRPCClient(object):
|
||||
def __init__(self, addr, port=None, verbose=False, timeout=60.0):
|
||||
self.verbose = verbose
|
||||
self.timeout = timeout
|
||||
if addr.startswith('/'):
|
||||
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
self.sock.connect(addr)
|
||||
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)
|
||||
else:
|
||||
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
self.sock.connect((addr, port))
|
||||
try:
|
||||
if addr.startswith('/'):
|
||||
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
self.sock.connect(addr)
|
||||
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)
|
||||
else:
|
||||
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
self.sock.connect((addr, port))
|
||||
except socket.error as ex:
|
||||
raise JSONRPCException("Error while connecting to %s\n"
|
||||
"Error details: %s" % (addr, ex))
|
||||
|
||||
def __del__(self):
|
||||
self.sock.close()
|
||||
|
Loading…
Reference in New Issue
Block a user