As documented in https://ww.getmonero.org/resources/developer-guides/daemon-rpc.html and https://ww.getmonero.org/resources/developer-guides/wallet-rpc.html, Monero can be used programmatically using daemon and wallet RPC.

If I wanted to use Python to extract data from the blockchain, for example nonce values over a block range to run statistics on, what is the best way to do it ?


The Monero team recently added a tool that makes it easy to integrate Monero RPC with Python. It can connect to running daemon and wallet and exposes their RPC interfaces:

The following command will connect to a monerod running on mainnet on default settings:

utils/python-rpc/console 18081 

You'll get this output:

Variable 'daemon' connected to daemon RPC on

You're now in a python interpreter, and the daemon variable is set to an object which can call daemon RPC directly, eg:

>>> daemon.get_version()
{u'status': u'OK', u'untrusted': False, u'version': 131078}

For the use case above (retrieving 21 nonce values from the blockchain from height 1000):

>>> [x.nonce for x in daemon.getblockheadersrange(1000, 1020).headers]
[3419004817, 3320605335, 295465941, 3696734864, 2221411748, 2201153093, 148086550, 524920481, 1766174771, 1831485859, 2409397405, 804573540, 535538542, 1487558207, 3340140970, 4029873826, 3890252333, 586366003, 1681558754, 1323530723, 240084243]

You can also connect the console to several processes, daemon and wallet, so you can interact with both from the same interpreter. The console will detect whether it's connecting to a node or a wallet, and will create the RPC proxy objects as daemon or wallet accordingly. If you connect to more than one demon or more than one wallet, the RPC objects will be daemons and/or wallets arrays instead of single variables. For instance, if you have a wallet running on port 8080:

$ utils/python-rpc/console 18081 8080
Variable 'daemon' connected to daemon RPC on
Variable 'wallet' connected to wallet RPC on

The console tool accepts full URLs if the daemon/wallet does not run on

To use that framework in other Python programs, you should import the module(s) you need:

from framework.daemon import Daemon
from framework.wallet import Wallet
| improve this answer | |
  • The answer shows using the new console tool, but doesn't show how to mix with other python code. Perhaps also reference: moneroexamples.github.io/python-json-rpc which shows calling the RPC interfaces directly from standalone python scripts. – jtgrassie Apr 28 '19 at 12:25
  • You can add that as a new answer. – user36303 Apr 28 '19 at 14:23
  • Reading that link you posted, I actually thought this was a separate Python library, as I know of a few that exist (and not having used them before). Anyway, I added the import statements at the end for completeness as you suggested. – user36303 Apr 28 '19 at 14:58
  • Yeah there are a few libraries floating around. None are really what I would call complete though. Even this new utility doesn't implement every method. – jtgrassie Apr 28 '19 at 15:02

In addition to @user36303's answer, it's worth also noting you can call the RPC interfaces directly from Python of course. Example code: https://moneroexamples.github.io/python-json-rpc/

This is essentially what the new Python utility referenced does under-the-hood.

| improve this answer | |

The easiest way is to take the command you want from the RPC documentation:

$ curl -X POST -d '{"jsonrpc":"2.0","id":"0","method":"get_block_header_by_height","params":{"height":912345}}' -H 'Content-Type: application/json'

Paste it into https://curl.trillworks.com:

Copy the output:

import requests

headers = {
    'Content-Type': 'application/json',

data = '{"jsonrpc":"2.0","id":"0","method":"get_block_header_by_height","params":{"height":912345}}'

response = requests.post('http://curl', headers=headers, data=data)

Note, if you want to do something more than just data crunching, I highly recommend the super simple Monero-python library.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.