monero的python jsonrpc增强版(monerod,monero wallet rpc)。
python-monerorpc的Python项目详细描述
python monerorpc
python monerorpc是python jsonrpc for monero的改进版本(monerod rpc
,monero-wallet-rpc
)。
python monerorpc最初是从python-bitcoinrpc派生的。
它包括以下一般性改进:
- http连接在使用
requests.Session
的 - 根据json-rpc 2.0发送协议“json rpc”
- 发送正确的、递增的“id”
- 使用标准的python json库
- 可以选择记录所有rpc调用和结果
- JSON-2.0批处理支持(模拟批处理)
- json-2.0批处理似乎不适用于monero。
- 批处理功能是模拟的,只是一个接一个地请求给定的方法。
- 结果是一个字典列表。
AuthServiceProxy
对象的生命周期内持续存在
它还包括一些更具体的细节:
- 发送摘要http身份验证头
- 将所有看起来像浮点数的json数字解析为十进制, 并将十进制值序列化为JSON-RPC连接。
它是做什么的?
python monero rpc通过rpc与monero通信。
包括:
monerod rpc
以及monero-wallet-rpc
。
python monerorpc接管包含所有必要头的实际http请求。
与类似项目相比:
- moneroPython
- 该模块实现一个json-rpc后端(
monerod rpc
,monero-wallet-rpc
)。 - 它围绕这个后端实现(帐户、钱包、事务等)
- 它提供了一些有用的实用程序,比如monero钱包地址验证器。
实际差异:
- 如果rpc方法发生更改或应添加新的方法,monero python必须调整其后端及其周围的实现,而使用python monerorpc则只需修改属性或使用如下新方法:
rpc_connection.getbalance()# -> rpc_connection.get_balance()rpc_connection.new_method()
安装:
来自Pypi
要使用pip
从pypi安装python-monerorpc
,您只需要:
$ pip install python-monerorpc
来源
$ python setup.py install --user
注意:这将只安装monerorpc
。如果您还想安装jsonrpc
以保留
向后兼容,您必须用setup.py
中的jsonrpc
替换monerorpc
,然后再次运行它。
示例:
示例用法monerod
(获取信息):
frommonerorpc.authproxyimportAuthServiceProxy,JSONRPCException# initialisation, rpc_user and rpc_password are set as flags in the cli commandrpc_connection=AuthServiceProxy('http://{0}:{1}@127.0.0.1:18081/json_rpc'.format(rpc_user,rpc_password))info=rpc_connection.get_info()print(info)# rpc_user and rpc_password can also be left out (testing, develop, not recommended)rpc_connection=AuthServiceProxy('http://127.0.0.1:18081/json_rpc')
示例用法monerod
(获取网络类型):
frommonerorpc.authproxyimportAuthServiceProxy,JSONRPCExceptionrpc_connection=AuthServiceProxy('http://{0}:{1}@127.0.0.1:18081/json_rpc'.format(rpc_user,rpc_password))result=Nonenetwork_type=Nonetry:result=rpc_connection.get_info()except(requests.HTTPError,requests.ConnectionError,JSONRPCException)ase:logger.error('RPC Error on getting address'+str(e))logger.exception(e)# Check network typenetwork_type=result.get('nettype')ifnotnetwork_type:raiseValueError('Error with: {0}'.format(result))print(network_type)
示例用法monerod
(关于get block hash):
frommonerorpc.authproxyimportAuthServiceProxy,JSONRPCExceptionrpc_connection=AuthServiceProxy('http://{0}:{1}@127.0.0.1:18081/json_rpc'.format(rpc_user,rpc_password))params=[2]hash=rpc.on_get_block_hash(params)print(hash)
示例用法monero-wallet-rpc
(获取余额):
frommonerorpc.authproxyimportAuthServiceProxy,JSONRPCException# initialisation, rpc_user and rpc_password are set as flags in the cli commandrpc_connection=AuthServiceProxy('http://{0}:{1}@127.0.0.1:18083/json_rpc'.format(rpc_user,rpc_password))balance=rpc_connection.get_balance()print(balance)
示例用法monero-wallet-rpc
(进行传输):
frommonerorpc.authproxyimportAuthServiceProxy,JSONRPCException# initialisation, rpc_user and rpc_password are set as flags in the cli commandrpc_connection=AuthServiceProxy('http://{0}:{1}@127.0.0.1:18083/json_rpc'.format(rpc_user,rpc_password))destinations={"destinations":[{"address":"some_address","amount":1}],"mixin":10}result=rpc_connection.transfer(destinations)print(result)
示例用法monero-wallet-rpc
(批处理):
frommonerorpc.authproxyimportAuthServiceProxy,JSONRPCExceptionimportpprint# initialisation, rpc_user and rpc_password are set as flags in the cli commandrpc_connection=AuthServiceProxy('http://{0}:{1}@127.0.0.1:18083/json_rpc'.format(rpc_user,rpc_password))# some example batchparams={"account_index":0,"address_indices":[0,1]}result=rpc.batch_([["get_balance"],["get_balance",params]])pprint.pprint(result)# make transfer and get balance in a batchdestinations={"destinations":[{"address":"some_address","amount":1}],"mixin":10}result=rpc.batch_([["transfer",destinations],["get_balance"]])pprint.pprint(result)
日志记录:
将所有rpc调用记录到stderr:
frommonerorpc.authproxyimportAuthServiceProxy,JSONRPCExceptionimportlogginglogging.basicConfig()logging.getLogger("MoneroRPC").setLevel(logging.DEBUG)rpc_connection=AuthServiceProxy('http://{0}:{1}@127.0.0.1:18081/json_rpc'.format(rpc_user,rpc_password))print(rpc_connection.get_info())
在stderr上生成如下输出:
DEBUG:MoneroRPC:-1-> get_info [] DEBUG:MoneroRPC:<-1- {u'result': {u'incoming_connections_count': 0, ...etc }
错误:
可能的错误和错误代码:
no code
- 返回rpc响应中包含的
error
。
- 返回rpc响应中包含的
-341
could not establish a connection, original error: {}
- 包括原始异常消息
-342
missing HTTP response from server
-343
missing JSON-RPC result
-344
received HTTP status code {}
- 包括http状态代码,而不是
200
测试:
安装测试要求:
virtualenv -q venv . venv/bin/activate pip install -r requirements.txt
使用pytest
:
# virtualenv activated (see above)
pytest tests.py
作者
- norman moeschter schenck-初始工作-normoes
另请参阅参与此项目的contributors名单。