Skip to content

Instantly share code, notes, and snippets.

@humbdrag
Created January 17, 2022 22:40
Show Gist options
  • Save humbdrag/100e0618626010b02c8ba96f2acc3a99 to your computer and use it in GitHub Desktop.
Save humbdrag/100e0618626010b02c8ba96f2acc3a99 to your computer and use it in GitHub Desktop.
BTC Minimal RPC
import base64
import json
import random
import urllib.request
import urllib.error
import urllib.parse
# Define parameters for the RPC.
parameters = {
"host": "127.0.0.1",
"port": "8332",
"rpcuser": "Your Username",
"rpcpass": "Your Password",
"rpcurl": "http://127.0.0.1:8332"
}
# Construct the RPC request data.
rpc_id = random.getrandbits(32)
data = json.dumps({
"id": rpc_id,
"method": "getblocktemplate",
"params": [{"rules": ["segwit"]}]
}).encode()
auth = base64.b64encode(bytes(
parameters["rpcuser"] + ":" + parameters["rpcpass"],
"utf8"
))
request = urllib.request.Request(
parameters["rpcurl"],
data,
{"Authorization": b"Basic " + auth}
)
# Send the RPC and parse response.
with urllib.request.urlopen(request) as f:
response = json.loads(f.read())
print(response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment