Add request timeout to JSONRPC backend

This commit is contained in:
Michał Sałaban 2019-05-11 00:41:10 +02:00
parent 64149f6323
commit 010408dcf9
8 changed files with 27 additions and 13 deletions

View file

@ -14,7 +14,7 @@ Python Monero module
A comprehensive Python module for handling Monero cryptocurrency.
* release 0.5.2
* release 0.5.3
* open source: https://github.com/monero-ecosystem/monero-python
* works with Monero 0.12.x and `the latest source`_ (at least we try to keep up)
* Python 2.x and 3.x compatible

View file

@ -55,9 +55,9 @@ author = 'Michal Salaban'
# built documents.
#
# The short X.Y version.
version = '0.5.2'
version = '0.5.3'
# The full version, including alpha/beta/rc tags.
release = '0.5.2'
release = '0.5.3'
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.

View file

@ -1,3 +1,3 @@
from . import address, account, daemon, wallet, numbers, prio, wordlists, seed
__version__ = '0.5.2'
__version__ = '0.5.3'

View file

@ -22,8 +22,10 @@ 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: request timeout
"""
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=30):
self.url = '{protocol}://{host}:{port}'.format(
protocol=protocol,
host=host,
@ -31,6 +33,7 @@ class JSONRPCDaemon(object):
_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')
@ -62,7 +65,8 @@ class JSONRPCDaemon(object):
_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,
@ -80,7 +84,9 @@ class JSONRPCDaemon(object):
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:
@ -112,11 +118,13 @@ 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: request timeout
"""
_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=30):
self.url = '{protocol}://{host}:{port}/json_rpc'.format(
protocol=protocol,
host=host,
@ -124,6 +132,7 @@ class JSONRPCWallet(object):
_log.debug("JSONRPC wallet backend URL: {url}".format(url=self.url))
self.user = user
self.password = password
self.timeout = timeout
_log.debug("JSONRPC wallet backend auth: '{user}'/'{stars}'".format(
user=user, stars=('*' * len(password)) if password else ''))
@ -311,7 +320,8 @@ class JSONRPCWallet(object):
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:

View file

@ -17,6 +17,7 @@ def get_daemon():
argsparser = argparse.ArgumentParser(description="Display daemon info")
argsparser.add_argument('daemon_rpc_url', nargs='?', type=url_data, default='127.0.0.1:18081',
help="Daemon RPC URL [host[:port]]")
argsparser.add_argument('-t', dest='timeout', type=int, default=30, help="Request timeout")
argsparser.add_argument('-v', dest='verbosity', action='count', default=0,
help="Verbosity (repeat to increase; -v for INFO, -vv for DEBUG")
args = argsparser.parse_args()
@ -26,7 +27,7 @@ def get_daemon():
elif args.verbosity > 1:
level = logging.DEBUG
logging.basicConfig(level=level, format="%(asctime)-15s %(message)s")
return Daemon(JSONRPCDaemon(**args.daemon_rpc_url))
return Daemon(JSONRPCDaemon(timeout=args.timeout, **args.daemon_rpc_url))
d = get_daemon()
info = d.info()

View file

@ -21,6 +21,7 @@ argsparser.add_argument('daemon_rpc_url', nargs='?', type=url_data, default='127
help="Daemon RPC URL [host[:port]]")
argsparser.add_argument('-v', dest='verbosity', action='count', default=0,
help="Verbosity (repeat to increase; -v for INFO, -vv for DEBUG")
argsparser.add_argument('-t', dest='timeout', type=int, default=30, help="Request timeout")
argsparser.add_argument('-i', dest='tx_filenames', nargs='+', default=None,
help="Files with transaction data. Will read from stdin if not given.")
argsparser.add_argument('--no-relay', dest='relay', action='store_false',
@ -36,7 +37,7 @@ if args.tx_filenames:
blobs = [(f, open(f, 'r').read()) for f in args.tx_filenames]
else:
blobs = [('transaction', sys.stdin.read())]
d = Daemon(JSONRPCDaemon(**args.daemon_rpc_url))
d = Daemon(JSONRPCDaemon(timeout=args.timeout, **args.daemon_rpc_url))
for name, blob in blobs:
logging.debug("Sending {}".format(name))
tx = Transaction(blob=blob)

View file

@ -28,6 +28,7 @@ argsparser.add_argument('-v', dest='verbosity', action='count', default=0,
help="Verbosity (repeat to increase; -v for INFO, -vv for DEBUG")
argsparser.add_argument('wallet_rpc_url', nargs='?', type=url_data, default='127.0.0.1:18082',
help="Daemon URL [user[:password]@]host[:port]")
argsparser.add_argument('-t', dest='timeout', type=int, default=30, help="Request timeout")
argsparser.add_argument('-a', dest='account', default=0, type=int, help="Source account index")
argsparser.add_argument('-p', dest='prio',
choices=['unimportant', 'normal', 'elevated', 'priority'],
@ -50,7 +51,7 @@ elif args.verbosity > 1:
level = logging.DEBUG
logging.basicConfig(level=level, format="%(asctime)-15s %(message)s")
w = Wallet(JSONRPCWallet(**args.wallet_rpc_url))
w = Wallet(JSONRPCWallet(timeout=args.timeout, **args.wallet_rpc_url))
txns = w.accounts[args.account].transfer_multiple(
args.destinations, priority=prio, payment_id=args.payment_id,
relay=args.outdir is None)

View file

@ -20,6 +20,7 @@ def get_wallet():
help="Wallet RPC URL [user[:password]@]host[:port]")
argsparser.add_argument('-v', dest='verbosity', action='count', default=0,
help="Verbosity (repeat to increase; -v for INFO, -vv for DEBUG")
argsparser.add_argument('-t', dest='timeout', type=int, default=30, help="Request timeout")
args = argsparser.parse_args()
level = logging.WARNING
if args.verbosity == 1:
@ -27,7 +28,7 @@ def get_wallet():
elif args.verbosity > 1:
level = logging.DEBUG
logging.basicConfig(level=level, format="%(asctime)-15s %(message)s")
return Wallet(JSONRPCWallet(**args.wallet_rpc_url))
return Wallet(JSONRPCWallet(timeout=args.timeout, **args.wallet_rpc_url))
_TXHDR = "timestamp height id/hash " \
" amount fee {dir:95s} payment_id"