与TAO结合使用的Python BITCONRPC增强版本
python-taorpc的Python项目详细描述
authServiceProxy是python jsonrpc的改进版本。
它包括以下一般性改进:
- http连接在authServiceProxy对象的生命周期内持续存在
- 根据json-rpc 1.1发送协议“version”
- 发送正确的、递增的“id”
- 使用标准的python json库
- 可以选择记录所有rpc调用和结果
- json-2.0批处理支持
它还包括以下特定于比特币的详细信息:
- 发送基本的http身份验证头
- 将所有看起来像浮点数的json数字解析为十进制, 并将十进制值序列化为JSON-RPC连接。
安装
- 将setup.py的第一行更改为指向python 2安装目录。*
- 运行setup.py
注意:这将只安装taorpc。如果您还想安装jsonrpc以保留 向后兼容,您必须在setup.py中将“taorpc”替换为“jsonrpc”,然后再次运行它。
或者简单地使用pip安装库:
pip install python-taorpc
示例
fromtaorpc.authproxyimportAuthServiceProxy,JSONRPCException# rpc_user and rpc_password are set in the bitcoin.conf filerpc_connection=AuthServiceProxy("http://%s:%s@127.0.0.1:8332"%(rpc_user,rpc_password))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]forheightinrange(100)]block_hashes=rpc_connection.batch_(commands)blocks=rpc_connection.batch_([["getblock",h]forhinblock_hashes])block_times=[block["time"]forblockinblocks]print(block_times)
将所有rpc调用记录到stderr
fromtaorpc.authproxyimportAuthServiceProxy,JSONRPCExceptionimportlogginglogging.basicConfig()logging.getLogger("TaoRPC").setLevel(logging.DEBUG)rpc_connection=AuthServiceProxy("http://%s:%s@127.0.0.1:8332"%(rpc_user,rpc_password))print(rpc_connection.getinfo())
在stderr上生成输出,如
DEBUG:TaoRPC:-1-> getinfo [] DEBUG:TaoRPC:<-1- {“connections”: 8, …etc }