mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-08-03 08:02:34 +02:00
Make linearize scripts Python 3-compatible.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
#!/usr/bin/env python
|
||||
#!/usr/bin/env python3
|
||||
#
|
||||
# linearize-hashes.py: List blocks in a linear, no-fork version of the chain.
|
||||
#
|
||||
@@ -8,11 +8,14 @@
|
||||
#
|
||||
|
||||
from __future__ import print_function
|
||||
try: # Python 3
|
||||
import http.client as httplib
|
||||
except ImportError: # Python 2
|
||||
import httplib
|
||||
import json
|
||||
import struct
|
||||
import re
|
||||
import base64
|
||||
import httplib
|
||||
import sys
|
||||
|
||||
settings = {}
|
||||
@@ -20,26 +23,32 @@ settings = {}
|
||||
##### Switch endian-ness #####
|
||||
def hex_switchEndian(s):
|
||||
""" Switches the endianness of a hex string (in pairs of hex chars) """
|
||||
pairList = [s[i]+s[i+1] for i in range(0,len(s),2)]
|
||||
return ''.join(pairList[::-1])
|
||||
pairList = [s[i:i+2].encode() for i in range(0, len(s), 2)]
|
||||
return b''.join(pairList[::-1]).decode()
|
||||
|
||||
class BitcoinRPC:
|
||||
def __init__(self, host, port, username, password):
|
||||
authpair = "%s:%s" % (username, password)
|
||||
self.authhdr = "Basic %s" % (base64.b64encode(authpair))
|
||||
self.conn = httplib.HTTPConnection(host, port, False, 30)
|
||||
authpair = authpair.encode('utf-8')
|
||||
self.authhdr = b"Basic " + base64.b64encode(authpair)
|
||||
self.conn = httplib.HTTPConnection(host, port=port, timeout=30)
|
||||
|
||||
def execute(self, obj):
|
||||
self.conn.request('POST', '/', json.dumps(obj),
|
||||
{ 'Authorization' : self.authhdr,
|
||||
'Content-type' : 'application/json' })
|
||||
try:
|
||||
self.conn.request('POST', '/', json.dumps(obj),
|
||||
{ 'Authorization' : self.authhdr,
|
||||
'Content-type' : 'application/json' })
|
||||
except ConnectionRefusedError:
|
||||
print('RPC connection refused. Check RPC settings and the server status.',
|
||||
file=sys.stderr)
|
||||
return None
|
||||
|
||||
resp = self.conn.getresponse()
|
||||
if resp is None:
|
||||
print("JSON-RPC: no response", file=sys.stderr)
|
||||
return None
|
||||
|
||||
body = resp.read()
|
||||
body = resp.read().decode('utf-8')
|
||||
resp_obj = json.loads(body)
|
||||
return resp_obj
|
||||
|
||||
@@ -70,6 +79,9 @@ def get_block_hashes(settings, max_blocks_per_call=10000):
|
||||
batch.append(rpc.build_request(x, 'getblockhash', [height + x]))
|
||||
|
||||
reply = rpc.execute(batch)
|
||||
if reply is None:
|
||||
print('Cannot continue. Program will halt.')
|
||||
return None
|
||||
|
||||
for x,resp_obj in enumerate(reply):
|
||||
if rpc.response_is_error(resp_obj):
|
||||
|
Reference in New Issue
Block a user