| 1 |
|
| 2 |
|
| 3 |
from __future__ import print_function |
| 4 |
import sys |
| 5 |
import subprocess |
| 6 |
import socket |
| 7 |
try: |
| 8 |
import urllib.parse |
| 9 |
url_parser = urllib.parse.urlparse |
| 10 |
except: |
| 11 |
try: |
| 12 |
import urlparse |
| 13 |
url_parser = urlparse.urlparse |
| 14 |
except: |
| 15 |
print('urllib or urlparse is needed') |
| 16 |
sys.exit(1) |
| 17 |
import framework.rpc |
| 18 |
import framework.daemon |
| 19 |
import framework.wallet |
| 20 |
|
| 21 |
USAGE = 'usage: python -i console.py [[[scheme]<host>:]<port> [[[scheme]<host>:]<port>...]]' |
| 22 |
daemons = [] |
| 23 |
wallets = [] |
| 24 |
rpcs = [] |
| 25 |
for n in range(1, len(sys.argv)): |
| 26 |
scheme='http' |
| 27 |
host='127.0.0.1' |
| 28 |
port=None |
| 29 |
try: |
| 30 |
try: |
| 31 |
port = int(sys.argv[n]) |
| 32 |
except: |
| 33 |
t = url_parser(sys.argv[n], allow_fragments = False) |
| 34 |
scheme = t.scheme or scheme |
| 35 |
host = t.hostname or host |
| 36 |
port = t.port or port |
| 37 |
if scheme != 'http' and scheme != 'https': |
| 38 |
raise Exception(USAGE) |
| 39 |
if port <= 0 or port > 65535: |
| 40 |
raise Exception(USAGE) |
| 41 |
except Exception as e: |
| 42 |
print('Error: ' + str(e)) |
| 43 |
raise Exception(USAGE) |
| 44 |
|
| 45 |
|
| 46 |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 47 |
s.settimeout(1) |
| 48 |
if s.connect_ex((host, port)) != 0: |
| 49 |
raise Exception('No wallet or daemon RPC on port ' + str(port)) |
| 50 |
s.close() |
| 51 |
|
| 52 |
|
| 53 |
rpc = framework.rpc.JSONRPC('{protocol}://{host}:{port}'.format(protocol=scheme, host=host, port=port)) |
| 54 |
get_version = { |
| 55 |
'method': 'get_version', |
| 56 |
'jsonrpc': '2.0', |
| 57 |
'id': '0' |
| 58 |
} |
| 59 |
try: |
| 60 |
res = rpc.send_json_rpc_request(get_version) |
| 61 |
except Exception as e: |
| 62 |
raise Exception('Failed to call version RPC: ' + str(e)) |
| 63 |
|
| 64 |
if 'version' not in res: |
| 65 |
raise Exception('Server is not a Monero process') |
| 66 |
|
| 67 |
if 'status' in res: |
| 68 |
daemons.append(framework.daemon.Daemon(port=port)) |
| 69 |
rpcs.append(daemons[-1]) |
| 70 |
else: |
| 71 |
wallets.append(framework.wallet.Wallet(port=port)) |
| 72 |
rpcs.append(wallets[-1]) |
| 73 |
|
| 74 |
|
| 75 |
try: |
| 76 |
import readline |
| 77 |
except: |
| 78 |
pass |
| 79 |
else: |
| 80 |
import rlcompleter |
| 81 |
readline.parse_and_bind('tab: complete') |
| 82 |
|
| 83 |
if len(daemons) == 1: |
| 84 |
daemon = daemons[0] |
| 85 |
if len(wallets) == 1: |
| 86 |
wallet = wallets[0] |
| 87 |
|
| 88 |
didx = 0 |
| 89 |
widx = 0 |
| 90 |
for rpc in rpcs: |
| 91 |
if type(rpc) == framework.daemon.Daemon: |
| 92 |
var = "daemon" if len(daemons) == 1 else "daemons[" + str(didx) + "]" |
| 93 |
didx += 1 |
| 94 |
else: |
| 95 |
var = "wallet" if len(wallets) == 1 else "wallets[" + str(widx) + "]" |
| 96 |
widx += 1 |
| 97 |
print('Variable \'%s\' connected to %s RPC on %s:%u' % (var, 'daemon' if type(rpc) == framework.daemon.Daemon else 'wallet', rpc.host ,rpc.port)) |