From e17fb2f1cb32f9e22f72cbb7ab2b071b34bcc38c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Falc=C3=A3o?= Date: Mon, 29 Apr 2019 16:25:36 +0100 Subject: [PATCH 1/2] add timeout to JSON RPC backends --- monero/backends/jsonrpc.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/monero/backends/jsonrpc.py b/monero/backends/jsonrpc.py index a78c89e..9e65352 100644 --- a/monero/backends/jsonrpc.py +++ b/monero/backends/jsonrpc.py @@ -25,8 +25,11 @@ class JSONRPCDaemon(object): :param host: host name or IP :param port: port number :param path: path for JSON RPC requests (should not be changed) + :param timeout: optional timeout for requests (seconds). Please note that + a timeout does not mean that the underlying operation was not executed. """ - def __init__(self, protocol='http', host='127.0.0.1', port=18081, path='/json_rpc', user='', password=''): + def __init__(self, protocol='http', host='127.0.0.1', port=18081, + path='/json_rpc', user='', password='', timeout=None): self.url = '{protocol}://{host}:{port}'.format( protocol=protocol, host=host, @@ -34,6 +37,7 @@ def __init__(self, protocol='http', host='127.0.0.1', port=18081, path='/json_rp _log.debug("JSONRPC daemon backend URL: {url}".format(url=self.url)) self.user = user self.password = password + self.timeout = timeout def info(self): info = self.raw_jsonrpc_request('get_info') @@ -65,7 +69,8 @@ def raw_request(self, path, data): _log.debug(u"Request: {path}\nData: {data}".format( path=path, data=json.dumps(data, indent=2, sort_keys=True))) - rsp = requests.post(self.url + path, headers=hdr, data=json.dumps(data)) + rsp = requests.post(self.url + path, headers=hdr, data=json.dumps(data), + timeout=self.timeout) if rsp.status_code != 200: raise RPCError("Invalid HTTP status {code} for path {path}.".format( code=rsp.status_code, @@ -83,7 +88,8 @@ def raw_jsonrpc_request(self, method, params=None): method=method, params=json.dumps(params, indent=2, sort_keys=True))) auth = requests.auth.HTTPDigestAuth(self.user, self.password) - rsp = requests.post(self.url + '/json_rpc', headers=hdr, data=json.dumps(data), auth=auth) + rsp = requests.post(self.url + '/json_rpc', headers=hdr, data=json.dumps(data), + auth=auth, timeout=self.timeout) if rsp.status_code == 401: raise Unauthorized("401 Unauthorized. Invalid RPC user name or password.") elif rsp.status_code != 200: @@ -115,11 +121,14 @@ class JSONRPCWallet(object): :param path: path for JSON RPC requests (should not be changed) :param user: username to authenticate with over RPC :param password: password to authenticate with over RPC + :param timeout: optional timeout for requests (seconds). Please note that + a timeout does not mean that the underlying operation was not executed. """ _master_address = None _addresses = None - def __init__(self, protocol='http', host='127.0.0.1', port=18088, path='/json_rpc', user='', password=''): + def __init__(self, protocol='http', host='127.0.0.1', port=18088, + path='/json_rpc', user='', password='', timeout=None): self.url = '{protocol}://{host}:{port}/json_rpc'.format( protocol=protocol, host=host, @@ -129,6 +138,7 @@ def __init__(self, protocol='http', host='127.0.0.1', port=18088, path='/json_rp self.password = password _log.debug("JSONRPC wallet backend auth: '{user}'/'{stars}'".format( user=user, stars=('*' * len(password)) if password else '')) + self.timeout = timeout def height(self): return self.raw_request('getheight')['height'] @@ -401,7 +411,8 @@ def raw_request(self, method, params=None, squelch_error_logging=False): method=method, params=json.dumps(params, indent=2, sort_keys=True))) auth = requests.auth.HTTPDigestAuth(self.user, self.password) - rsp = requests.post(self.url, headers=hdr, data=json.dumps(data), auth=auth) + rsp = requests.post(self.url, headers=hdr, data=json.dumps(data), + auth=auth, timeout=self.timeout) if rsp.status_code == 401: raise Unauthorized("401 Unauthorized. Invalid RPC user name or password.") elif rsp.status_code != 200: From 9ff4e9bf0faa7a4e8ca49c964b491b62a8280c35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Falc=C3=A3o?= Date: Mon, 29 Apr 2019 17:10:54 +0100 Subject: [PATCH 2/2] add timeout description to README --- README.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.rst b/README.rst index 8406887..b437073 100644 --- a/README.rst +++ b/README.rst @@ -21,6 +21,8 @@ This fork contains the following changes: * Dropped support for python 2. * Added: ``get_unspent_outputs`` and ``get_incoming_transactions`` to the wallet. * Added: ``address_index`` to instances of ``SubAddress``. +* Added: optional ``timeout`` to ``JSONRPCWallet`` and ``JSONRPCDaemon``. Please note that + a timeout does not mean that the underlying operation was not executed. For documentation about how to use the package please check the original repository.