console.py: can now connect to several daemons/wallets

Also throw exceptions instead of print+exit, since that makes
the error print last, below the python stack trace, where it's
much less easy to miss it.
This commit is contained in:
moneromooo-monero 2019-04-06 11:34:59 +00:00
parent 9f9571aa3d
commit 047af5c343
No known key found for this signature in database
GPG key ID: 686F07454D6CEFC3
3 changed files with 69 additions and 53 deletions

View file

@ -5,63 +5,62 @@ import sys
import subprocess import subprocess
import socket import socket
import urlparse import urlparse
from framework import rpc import framework.rpc
from framework import wallet import framework.daemon
from framework import daemon import framework.wallet
scheme='http' USAGE = 'usage: python -i console.py [[[scheme]<host>:]<port> [[[scheme]<host>:]<port>...]]'
host='127.0.0.1' daemons = []
port=None wallets = []
rpcs = []
USAGE = 'usage: python -i console.py [[scheme]<host>:]<port>' for n in range(1, len(sys.argv)):
try: scheme='http'
host='127.0.0.1'
port=None
try: try:
port = int(sys.argv[1]) try:
port = int(sys.argv[n])
except: except:
t = urlparse.urlparse(sys.argv[1], allow_fragments = False) t = urlparse.urlparse(sys.argv[n], allow_fragments = False)
scheme = t.scheme or scheme scheme = t.scheme or scheme
host = t.hostname or host host = t.hostname or host
port = t.port or port port = t.port or port
if scheme != 'http' and scheme != 'https': if scheme != 'http' and scheme != 'https':
print(USAGE) raise Exception(USAGE)
sys.exit(1)
if port <= 0 or port > 65535: if port <= 0 or port > 65535:
print(USAGE) raise Exception(USAGE)
sys.exit(1) except Exception, e:
except Exception, e:
print('Error: ' + str(e)) print('Error: ' + str(e))
print(USAGE) raise Exception(USAGE)
sys.exit(1)
# check for open port # check for open port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(1) s.settimeout(1)
if s.connect_ex((host, port)) != 0: if s.connect_ex((host, port)) != 0:
print('No wallet or daemon RPC on port ' + str(port)) raise Exception('No wallet or daemon RPC on port ' + str(port))
sys.exit(1) s.close()
s.close()
# both wallet and daemon have a get_version JSON RPC # both wallet and daemon have a get_version JSON RPC
rpc = rpc.JSONRPC('{protocol}://{host}:{port}'.format(protocol=scheme, host=host, port=port)) rpc = framework.rpc.JSONRPC('{protocol}://{host}:{port}'.format(protocol=scheme, host=host, port=port))
get_version = { get_version = {
'method': 'get_version', 'method': 'get_version',
'jsonrpc': '2.0', 'jsonrpc': '2.0',
'id': '0' 'id': '0'
} }
try: try:
res = rpc.send_json_rpc_request(get_version) res = rpc.send_json_rpc_request(get_version)
except Exception, e: except Exception, e:
print('Failed to call version RPC: ' + str(e)) raise Exception('Failed to call version RPC: ' + str(e))
sys.exit(1)
if 'version' not in res: if 'version' not in res:
print('Server is not a monero process') raise Exception('Server is not a Monero process')
sys.exit(1)
if 'status' in res: if 'status' in res:
rpc = daemon.Daemon(port=port) daemons.append(framework.daemon.Daemon(port=port))
else: rpcs.append(daemons[-1])
rpc = wallet.Wallet(port=port) else:
wallets.append(framework.wallet.Wallet(port=port))
rpcs.append(wallets[-1])
# add tab completion if we can: https://stackoverflow.com/questions/246725 # add tab completion if we can: https://stackoverflow.com/questions/246725
try: try:
@ -72,5 +71,18 @@ else:
import rlcompleter import rlcompleter
readline.parse_and_bind('tab: complete') readline.parse_and_bind('tab: complete')
print('Connected to %s RPC on port %u' % ('daemon' if 'status' in res else 'wallet', port)) if len(daemons) == 1:
print('The \'rpc\' object may now be used to use the API') daemon = daemons[0]
if len(wallets) == 1:
wallet = wallets[0]
didx = 0
widx = 0
for rpc in rpcs:
if type(rpc) == framework.daemon.Daemon:
var = "daemon" if len(daemons) == 1 else "daemons[" + str(didx) + "]"
didx += 1
else:
var = "wallet" if len(wallets) == 1 else "wallets[" + str(widx) + "]"
widx += 1
print('Variable \'%s\' connected to %s RPC on %s:%u' % (var, 'daemon' if type(rpc) == framework.daemon.Daemon else 'wallet', rpc.host ,rpc.port))

View file

@ -33,6 +33,8 @@ from .rpc import JSONRPC
class Daemon(object): class Daemon(object):
def __init__(self, protocol='http', host='127.0.0.1', port=0, idx=0): def __init__(self, protocol='http', host='127.0.0.1', port=0, idx=0):
self.host = host
self.port = port
self.rpc = JSONRPC('{protocol}://{host}:{port}'.format(protocol=protocol, host=host, port=port if port else 18180+idx)) self.rpc = JSONRPC('{protocol}://{host}:{port}'.format(protocol=protocol, host=host, port=port if port else 18180+idx))
def getblocktemplate(self, address, prev_block = ""): def getblocktemplate(self, address, prev_block = ""):

View file

@ -33,6 +33,8 @@ from .rpc import JSONRPC
class Wallet(object): class Wallet(object):
def __init__(self, protocol='http', host='127.0.0.1', port=0, idx=0): def __init__(self, protocol='http', host='127.0.0.1', port=0, idx=0):
self.host = host
self.port = port
self.rpc = JSONRPC('{protocol}://{host}:{port}'.format(protocol=protocol, host=host, port=port if port else 18090+idx)) self.rpc = JSONRPC('{protocol}://{host}:{port}'.format(protocol=protocol, host=host, port=port if port else 18090+idx))
def make_uniform_destinations(self, address, transfer_amount, transfer_number_of_destinations=1): def make_uniform_destinations(self, address, transfer_amount, transfer_number_of_destinations=1):