Skip to content

Instantly share code, notes, and snippets.

@Warchant
Created February 13, 2020 21:22
Show Gist options
  • Select an option

  • Save Warchant/6c498d809ed67040da8ae73066d58585 to your computer and use it in GitHub Desktop.

Select an option

Save Warchant/6c498d809ed67040da8ae73066d58585 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import requests
from requests.auth import HTTPBasicAuth
import random
class BtcApi:
def __init__(self):
self.apiurl = 'http://127.0.0.1:12001'
self.user = 'user1'
self.password = 'password1'
def getblockcount(self):
return self.req("getblockcound")
def getblock(self, hash: str, verbosity: int):
return self.req('getblock', [hash, verbosity])
def getblockhash(self, height: int):
return self.req('getblockhash', [height])
def req(self, method: str, params=None):
req = {
"jsonrpc": "2.0",
"method": method,
"params": params if params else [],
"id": random.randint(0, 2147000000)
}
res = requests.post(
url=self.apiurl,
auth=HTTPBasicAuth(self.user, self.password),
json=req
)
if res.status_code == 200:
return res.json()['result']
else:
raise Exception(res)
api = BtcApi()
total = 10000
f = open("btc_blockheaders_0_{}.txt".format(total), "w")
for i in range(total):
h = api.getblockhash(i)
block = api.getblock(h, 0)
header = block[:160]
print(i, header)
f.write(header)
f.write('\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment