Search code examples
python-3.xrpcbitcoin

how to connect to bitcoin core over rpc with python3?


with python3, i found this example but i got "error 405"

script.py

import json
import requests

rpcPort = 9337
rpcUser = 'rpcuser'
rpcPassword = 'rpcpassword'
rpcIp = '127.0.0.1'

serverURL = 'http://' + str(rpcUser) + ':' + str(rpcPassword)+ '@' + str(rpcIp)+":" + str(rpcPort)

headers = {'content-type: text/plain'}
#payload = json.dumps({"method": 'getblockhash', "params": ["0"], "jsonrpc": "2.0"})
payload = json.dumps({"method": 'getblock', "params": ["0000000000005e5fd51f764d230441092f1b69d1a1eeab334c5bb32412e8dc51"]})
response = requests.get(serverURL, headers=headers, data=payload)
print(response)
#print(response.json()['result'])

bitcoin.conf

server=1
txindex=1
whitelist=0.0.0.0
rpcallowip=0.0.0.0/0
rpcconnect=127.0.0.1
rpcbind=127.0.0.1
rpcport =9337
rpcuser=rpcuser    
rpcpassword=rpcpassword


test-rpc.py
<Response [405]>

i dont know what im doing wrong for this 405


Solution

  • From this question here: bitcoin json rpc with python requests module?, It looks like you are expected to send the request using POST.

    response = requests.post(serverURL, headers=headers, data=payload)