mirror of
https://github.com/monero-project/monero.git
synced 2025-05-02 09:56:07 -04:00
python-rpc: add console.py
It allows one to connect to a running daemon or wallet, and use its RPC API from python. Usage: python -i console.py <port> It will detect whether it's talking to a daemon or wallet and initialize itself accordingly.
This commit is contained in:
parent
22b644f47e
commit
c7bfdc3566
4 changed files with 66 additions and 1 deletions
49
utils/python-rpc/console.py
Executable file
49
utils/python-rpc/console.py
Executable file
|
@ -0,0 +1,49 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
import subprocess
|
||||
import socket
|
||||
from framework import rpc
|
||||
from framework import wallet
|
||||
from framework import daemon
|
||||
|
||||
USAGE = 'usage: python -i console.py <port>'
|
||||
try:
|
||||
port = int(sys.argv[1])
|
||||
except:
|
||||
print(USAGE)
|
||||
sys.exit(1)
|
||||
|
||||
# check for open port
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
s.settimeout(1)
|
||||
if s.connect_ex(('127.0.0.1', port)) != 0:
|
||||
print('No wallet or daemon RPC on port ' + str(port))
|
||||
sys.exit(1)
|
||||
s.close()
|
||||
|
||||
# both wallet and daemon have a get_version JSON RPC
|
||||
rpc = rpc.JSONRPC('{protocol}://{host}:{port}'.format(protocol='http', host='127.0.0.1', port=port))
|
||||
get_version = {
|
||||
'method': 'get_version',
|
||||
'jsonrpc': '2.0',
|
||||
'id': '0'
|
||||
}
|
||||
try:
|
||||
res = rpc.send_json_rpc_request(get_version)
|
||||
except Exception, e:
|
||||
print('Failed to call version RPC: ' + str(e))
|
||||
sys.exit(1)
|
||||
|
||||
if 'version' not in res:
|
||||
print('Server is not a monero process')
|
||||
sys.exit(1)
|
||||
|
||||
if 'status' in res:
|
||||
rpc = daemon.Daemon(port=port)
|
||||
else:
|
||||
rpc = wallet.Wallet(port=port)
|
||||
|
||||
print('Connected to %s RPC on port %u' % ('daemon' if 'status' in res else 'wallet', port))
|
||||
print('The \'rpc\' object may now be used to use the API')
|
Loading…
Add table
Add a link
Reference in a new issue