Access bitcoind from python

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

Install python-rpc

Use python-bitcoinrpc.

pip install python-bitcoinrpc

Establishing rpc connection

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 /.

Check rpc connection

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

Access bitcoind from python
Access Oracle DB from Python
How to access wikipedia from python
Access spreadsheets using OAuth 2.0 from Python
Access Blender Shader Nodes from Python
sql from python
MeCab from Python
Revived from "no internet access" in Python
How to access RDS from Lambda (python)
Use thingsspeak from python
Touch MySQL from Python 3
Operate Filemaker from Python
Use fluentd from python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Python from or import
Use MySQL from Python
Run python from excel
Install python from source
Execute command from Python
Operate neutron from Python!
Use MySQL from Python
Operate LXC from Python
Manipulate riak from python
Force Python from Fortran
Use BigQuery from python.
Execute command from python
[Python] Read From Stdin
Use mecab-ipadic-neologd from python
Get data from database via ODBC with Python (Access)
ODBC access to SQL Server from Linux with Python
Deep Python learned from DEAP
Grammar features added from Python3.6
Cheating from PHP to Python
Make MeCab available from Python3
Information obtained from tweet_id (Python)
OCR from PDF in Python
Run illustrator script from python
Use MySQL from Anaconda (python)
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
Study from Python Hour4: Object-oriented ②
Query Athena from Lambda Python
Study from Python Hour3: Functions
Start / stop GCE from python
Stop Omxplayer from Python code
Switch from python2.7 to python3.6 (centos7)
Connect to sqlite from python
Install pyenv from Homebrew, install Python from pyenv
Study from Python Hour4: Object-oriented ①
Python naming convention (from PEP8)
Access Google Drive with Python
With skype, notify with skype from python!
Use e-Stat API from Python
Register redmine issue from Python
Call Matlab from Python to optimize
Execute Python script from batch file
Call a Python function from p5.js.
Python: Exclude tags from html data
Use Stanford Core NLP from Python
Try IAM Database Authentication from Python
Call C from Python with DragonFFI