This article uses Mac OS Sierra 10.12.4. I have summarized the information when building an environment to access bitcoind from python.
It is assumed that you have bitcoind installed. Reference: Build an environment for bitcoind with docker
Use python-bitcoinrpc.
pip install python-bitcoinrpc
Establish an rpc (remote procedure call) connection used by bitcoin. Follow quick_start to get started.
from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
rpc_user="bitcoinrpc"
rpc_password='passwordxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
rpc_connection = AuthServiceProxy("http://%s:%[email protected]:8332"%(rpc_user, rpc_password))
For rpc_user and rpc_password, use the ones set when building the bitcoind environment. If you don't know, check bitcoin.conf and it should be there. In addition, as of June 24, 2017, in AuthServiceProxy, if "/" is included in the password, an error will occur. (Because urllib cannot identify the delimiter when decomposing rpc_user and rpc_password well) It is necessary to devise such as making the password not mixed with /.
After defining rpc_connection, let's take a block
blhash = rpc_connection.getblockhash(0) #blhash is a block hash string
bl = rpc_connection.getblock(blhash) #bl is block information
Check the result
bl
{'bits': '1d00ffff',
'chainwork': '0000000000000000000000000000000000000000000000000000000100010001',
'confirmations': 1,
'difficulty': 1,
'hash': '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f',
'height': 0,
'mediantime': 1231006505,
'merkleroot': '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b',
'nonce': 2083236893,
'size': 285,
'strippedsize': 285,
'time': 1231006505,
'tx': ['4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b'],
'version': 1,
'versionHex': '00000001',
'weight': 1140}
genesis block information is available.
Next time, we will write the code to get the transaction information in the block.
Recommended Posts