bitcoin: 透過 rpc 請求節點資料

shooter發表於2018-03-19

文章來自 簡書

影片

配置檔案

放在 /mnt/bitcoin 目錄下

#  /mnt/bitcoin/bitcoin.conf 
server=1
daemon=1
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_(...)
https://bitcoin.stackexchange.com/question...

本作品採用《CC 協議》,轉載必須註明作者和本文連結
www.jianshu.com/c/dd87e0b9cd3c

相關文章