aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormoneromooo-monero <moneromooo-monero@users.noreply.github.com>2019-03-22 15:25:33 +0000
committermoneromooo-monero <moneromooo-monero@users.noreply.github.com>2019-03-23 18:59:47 +0000
commitc7bfdc356618fd1ccbe0f87fd5009e944cd12e50 (patch)
tree2f1154b5560f096dab5f2bbf531d83362eb8df1c
parentfunctional_tests: move RPC API to utils, it is not test specific (diff)
downloadmonero-c7bfdc356618fd1ccbe0f87fd5009e944cd12e50.tar.xz
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.
-rwxr-xr-xtests/functional_tests/functional_tests_rpc.py2
-rwxr-xr-xutils/python-rpc/console.py49
-rw-r--r--utils/python-rpc/framework/daemon.py8
-rw-r--r--utils/python-rpc/framework/wallet.py8
4 files changed, 66 insertions, 1 deletions
diff --git a/tests/functional_tests/functional_tests_rpc.py b/tests/functional_tests/functional_tests_rpc.py
index 606b19e45..f2fef7e95 100755
--- a/tests/functional_tests/functional_tests_rpc.py
+++ b/tests/functional_tests/functional_tests_rpc.py
@@ -60,7 +60,7 @@ try:
PYTHONPATH = os.environ['PYTHONPATH'] if 'PYTHONPATH' in os.environ else ''
if len(PYTHONPATH) > 0:
PYTHONPATH += ':'
- PYTHONPATH += '../../utils/python-rpc'
+ PYTHONPATH += srcdir + '/../../utils/python-rpc'
os.environ['PYTHONPATH'] = PYTHONPATH
for i in range(len(command_lines)):
#print('Running: ' + str(command_lines[i]))
diff --git a/utils/python-rpc/console.py b/utils/python-rpc/console.py
new file mode 100755
index 000000000..ab0d9f27f
--- /dev/null
+++ b/utils/python-rpc/console.py
@@ -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')
diff --git a/utils/python-rpc/framework/daemon.py b/utils/python-rpc/framework/daemon.py
index 11d5f5845..83c8eea67 100644
--- a/utils/python-rpc/framework/daemon.py
+++ b/utils/python-rpc/framework/daemon.py
@@ -196,3 +196,11 @@ class Daemon(object):
'id': '0'
}
return self.rpc.send_json_rpc_request(flush_txpool)
+
+ def get_version(self):
+ get_version = {
+ 'method': 'get_version',
+ 'jsonrpc': '2.0',
+ 'id': '0'
+ }
+ return self.rpc.send_json_rpc_request(get_version)
diff --git a/utils/python-rpc/framework/wallet.py b/utils/python-rpc/framework/wallet.py
index e9f13667b..ea683b8c5 100644
--- a/utils/python-rpc/framework/wallet.py
+++ b/utils/python-rpc/framework/wallet.py
@@ -590,3 +590,11 @@ class Wallet(object):
'id': '0'
}
return self.rpc.send_json_rpc_request(verify)
+
+ def get_version(self):
+ get_version = {
+ 'method': 'get_version',
+ 'jsonrpc': '2.0',
+ 'id': '0'
+ }
+ return self.rpc.send_json_rpc_request(get_version)