漫谈区块链

call bitcoin rpc

2018-03-11  本文已影响733人  已不再更新_转移到qiita

详细使用请看视频

配置文件

放在 /mnt/bitcoin 目录下

# /mnt/bitcoin/bitcoin.conf 

server=1
daemon=1
rpcallowip=<192.168.1.1/24> #局域网访问
rpcthreads=10
txindex=1
rpcuser=bitcoin
rpcpassword=12345

运行节点

bitcoind -datadir=/mnt/bitcoin # 确保有/mnt/bitcoin/bitcoin.conf

curl

curl --user bitcoin:12345 -H 'content-type:text/plain;' http://127.0.0.1:8332/
--data-binary '{"jsonrpc":"1.0","id":"1","method":"getblockhash","params":[1]}'

{
  "result": "00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048",
  "error": null,
  "id": "1"
}

python script

import json
import requests

url = "http://127.0.0.1:8332/"
auth=("bitcoin", "12345")
payload = json.dumps({"method": 'getblockhash', "params": [1] })

response = requests.request("POST", url, data=payload, auth=auth)
print(response.text)

参考:

https://en.bitcoin.it/wiki/API_reference_(JSON-RPC)
https://bitcoin.stackexchange.com/questions/19665/json-rpc-via-curl

上一篇 下一篇

猜你喜欢

热点阅读