Created
February 13, 2020 21:22
-
-
Save Warchant/6c498d809ed67040da8ae73066d58585 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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