Browse Source

Reconnect automatically when bitcoind connection times out

master
James Hilliard 8 years ago
parent
commit
0a55558d9e
  1. 56
      jmclient/jmclient/jsonrpc.py

56
jmclient/jmclient/jsonrpc.py

@ -74,31 +74,37 @@ class JsonRpc(object):
body = json.dumps(obj) body = json.dumps(obj)
try: while True:
self.conn.request("POST", "", body, headers) try:
response = self.conn.getresponse() self.conn.request("POST", "", body, headers)
response = self.conn.getresponse()
if response.status == 401:
self.conn.close() if response.status == 401:
raise JsonRpcConnectionError( self.conn.close()
"authentication for JSON-RPC failed") raise JsonRpcConnectionError(
"authentication for JSON-RPC failed")
# All of the codes below are 'fine' from a JSON-RPC point of view.
if response.status not in [200, 404, 500]: # All of the codes below are 'fine' from a JSON-RPC point of view.
self.conn.close() if response.status not in [200, 404, 500]:
raise JsonRpcConnectionError("unknown error in JSON-RPC") self.conn.close()
raise JsonRpcConnectionError("unknown error in JSON-RPC")
data = response.read()
data = response.read()
return json.loads(data)
return json.loads(data)
except JsonRpcConnectionError as exc:
raise exc except JsonRpcConnectionError as exc:
except httplib.BadStatusLine: raise exc
return "CONNFAILURE" except httplib.BadStatusLine:
except Exception as exc: return "CONNFAILURE"
raise JsonRpcConnectionError("JSON-RPC connection failed. Err:" + except Exception as exc:
repr(exc)) if str(exc) == "Connection reset by peer":
self.conn.connect()
continue
else:
raise JsonRpcConnectionError("JSON-RPC connection failed. Err:" +
repr(exc))
break
def call(self, method, params): def call(self, method, params):
""" """

Loading…
Cancel
Save