Simple_Wallet 使用Python来操控钱包
2015-03-10 15:00
405 查看
上一篇文章里讲到我们可以使用命令行来控制比特币钱包,接下来我们使用编程语言来实现操控
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这个项目。
想要使用这个项目,你需要如下操作
接下来我们可以安装一个ipython(网上教程很多这里不赘述)
还记得当初安装钱包时我们设定的那个rpc以及rpc密码么?这里就是使用这两个参数来链接钱包
程序会显示“ 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:
Logging all RPC calls to stderr:
Produces output on stderr like:
版权说明:此文章所有权归本博客所有,转载请联系本人邮箱lee@pengfei.ga征得同意
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征得同意
相关文章推荐
- python simplejson模块的使用方法
- Windows Phone中Wallet钱包的使用
- 记录一下:使用 python -m SimpleHTTPServer 快速搭建http服务
- Python-Jenkins API使用 —— 在后端代码中操控Jenkins
- Python内置的HTTP协议服务器SimpleHTTPServer使用指南
- 使用 Python SimpleHTTPServer 快速共享文件
- 使用 python -m SimpleHTTPServer 快速搭建http服务
- python simplejson and json 使用及区别
- 使用python SimpleHTTPServer 快速搭建Web服务器
- Python-Jenkins API使用 —— 在后端代码中操控Jenkins
- Windows Phone中Wallet钱包的使用
- Simple_Wallet 之钱包服务器调教
- 【脚本】使用 Python SimpleHTTPServer 快速共享文件
- 使用Python操控MySQL数据库
- python 使用 simplejson 将字符串转换成字典dict
- python simplejson模块的使用方法
- 使用 Python SimpleHTTPServer 快速共享文件
- Simple_Wallet 在服务器端通过RPC操纵钱包
- 详解Python使用simplejson模块解析JSON的方法
- python SimpleHTTPServer的使用