Simple_Wallet 使用Python来操控钱包

夏飞跃
2023-12-01

上一篇文章里讲到我们可以使用命令行来控制比特币钱包,接下来我们使用编程语言来实现操控

Run bitcoind or bitcoin-qt -server. You can control it via the command-line bitcoin-cli utility or by HTTP JSON-RPC commands.

具体可以参考比特币官方wiki 这个页面中提供了大部分语言的使用方法,这里我们使用Python来实现

上文那个页面中本身有提供python的接口,但是其实很难用,这里推荐一下GitHub上的python-bitcoinrpc这个项目。

想要使用这个项目,你需要如下操作

wget https://github.com/jgarzik/python-bitcoinrpc/archive/master.zip
apt-get update
apt-get install unzip #下载一个解压缩软件
unzip master.zip
cd python-bitcoinrpc-master/ #进入文件夹
python setup.py install #安装python-bitcoinrpc包

接下来我们可以安装一个ipython(网上教程很多这里不赘述)

还记得当初安装钱包时我们设定的那个rpc以及rpc密码么?这里就是使用这两个参数来链接钱包

from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
conn =AuthServiceProxy("http://rpcuser:rpcpassword@localhost:8332") #链接钱包
balance = conn.getbalance()
print balance

程序会显示“ Decimal(‘0E-8’)”,这个功能实现了python调用getbalance命令读取余额功能

接下来可以愉快的使用python-bitcoinrpc来编程了

引用官方文档

AuthServiceProxy is an improved version of python-jsonrpc.

It includes the following generic improvements:

  • HTTP connections persist for the life of the AuthServiceProxy object
  • sends protocol ‘version’, per JSON-RPC 1.1
  • sends proper, incrementing ‘id’
  • uses standard Python json lib
  • can optionally log all RPC calls and results
  • JSON-2.0 batch support

It also includes the following bitcoin-specific details:

  • sends Basic HTTP authentication headers
  • parses all JSON numbers that look like floats as Decimal,
    and serializes Decimal values to JSON-RPC connections.

Installation:

  • change the first line of setup.py to point to the directory of your installation of python 2.*
  • run setup.py

Note: This will only install bitcoinrpc. If you also want to install jsonrpc to preserve
backwards compatibility, you have to replace ‘bitcoinrpc’ with ‘jsonrpc’ in setup.py and run it again.

Example usage:

from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException

# rpc_user and rpc_password are set in the bitcoin.conf file
rpc_connection = AuthServiceProxy("http://%s:%s@127.0.0.1:8332"%(rpc_user, rpc_password))q
best_block_hash = rpc_connection.getbestblockhash()
print(rpc_connection.getblock(best_block_hash))

# batch support : print timestamps of blocks 0 to 99 in 2 RPC round-trips:
commands = [ [ "getblockhash", height] for height in range(100) ]
block_hashes = rpc_connection.batch_(commands)
blocks = rpc_connection.batch_([ [ "getblock", h ] for h in block_hashes ])
block_times = [ block["time"] for block in blocks ]
print(block_times)

Logging all RPC calls to stderr:

from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
import logging

logging.basicConfig()
logging.getLogger("BitcoinRPC").setLevel(logging.DEBUG)

rpc_connection = AuthServiceProxy("http://%s:%s@127.0.0.1:8332"%(rpc_user, rpc_password))
print(rpc_connection.getinfo())

Produces output on stderr like:

DEBUG:BitcoinRPC:-1-> getinfo []
DEBUG:BitcoinRPC:<-1- {"connections": 8, ...etc }

版权说明:此文章所有权归本博客所有,转载请联系本人邮箱lee@pengfei.ga征得同意

 类似资料: