因此,我正在使用此公共程序来解析此字节数据,但是却收到一个错误,即我正在解析的“字节”对象没有属性“编码”,这是怎么回事以及如何解决?
62 for j in range(4):
63 b = f.read(1)
---> 64 b = b.encode('hex').upper()
65 tmpHex = b + tmpHex
66 tmpHex = ''
AttributeError: 'bytes' object has no attribute 'encode'
我试图用其他方法替换编码,但最终还是收到另一个错误,该错误在上面的第65行说b和tmpHex无法连接。我该如何解决?
我不能包含所有代码,因为stackoverflow表示我的代码太多,编写的东西也很少。
完整代码的链接如下: https://github.com/normanvolt/blockchain-parser/blob/master/blockchain-parser.py
import os
import datetime
import hashlib
def HexToInt(s):
t = ''
if s == '':
r = 0
else:
t = '0x' + s
r = int(t,16)
return r
def reverse(input):
L = len(input)
if (L % 2) != 0:
return None
else:
Res = ''
L = L // 2
for i in range(L):
T = input[i*2] + input[i*2+1]
Res = T + Res
T = ''
return (Res);
def merkle_root(lst): # https://gist.github.com/anonymous/7eb080a67398f648c1709e41890f8c44
sha256d = lambda x: hashlib.sha256(hashlib.sha256(x).digest()).digest()
hash_pair = lambda x, y: sha256d(x[::-1] + y[::-1])[::-1]
if len(lst) == 1: return lst[0]
if len(lst) % 2 == 1:
lst.append(lst[-1])
return merkle_root([hash_pair(x,y) for x, y in zip(*[iter(lst)]*2)])
dirA = 'd:/_blocks/' # Directory where blk*.dat files are stored
#dirA = sys.argv[1]
dirB = 'd:/_hash/' # Directory where to save parsing results
#dirA = sys.argv[2]
fList = os.listdir(dirA)
fList = [x for x in fList if (x.endswith('.dat') and x.startswith('blk'))]
fList.sort()
for i in fList:
nameSrc = i
nameRes = nameSrc.replace('.dat','.hash')
resList = []
a = 0
t = dirA + nameSrc
resList.append('Start ' + t + ' in ' + str(datetime.datetime.now()))
print ('Start ' + t + ' in ' + str(datetime.datetime.now()))
f = open(t,'rb')
tmpHex = ''
fSize = os.path.getsize(t)
while f.tell() != fSize:
for j in range(4):
b = f.read(1)
b = b.encode('hex').upper()
tmpHex = b + tmpHex
tmpHex = ''
for j in range(4):
b = f.read(1)
b = b.encode('hex').upper()
tmpHex = b + tmpHex
resList.append('Block size = ' + tmpHex)
tmpHex = ''
tmpPos3 = f.tell()
while f.tell() != tmpPos3 + 80:
b = f.read(1)
b = b.encode('hex').upper()
tmpHex = tmpHex + b
tmpHex = tmpHex.decode('hex')
tmpHex = hashlib.new('sha256', tmpHex).digest()
tmpHex = hashlib.new('sha256', tmpHex).digest()
tmpHex = tmpHex.encode('hex')
tmpHex = tmpHex.upper()
tmpHex = reverse(tmpHex)
resList.append('SHA256 hash of the current block hash = ' + tmpHex)
f.seek(tmpPos3,0)
tmpHex = ''
for j in range(4):
b = f.read(1)
b = b.encode('hex').upper()
tmpHex = b + tmpHex
resList.append('Version number = ' + tmpHex)
tmpHex = ''
for j in range(32):
b = f.read(1)
b = b.encode('hex').upper()
tmpHex = b + tmpHex
resList.append('SHA256 hash of the previous block hash = ' + tmpHex)
tmpHex = ''
for j in range(32):
b = f.read(1)
b = b.encode('hex').upper()
tmpHex = b + tmpHex
resList.append('MerkleRoot hash = ' + tmpHex)
MerkleRoot = tmpHex
tmpHex = ''
for j in range(4):
b = f.read(1)
b = b.encode('hex').upper()
tmpHex = b + tmpHex
resList.append('Time stamp > ' + tmpHex)
tmpHex = ''
for j in range(4):
b = f.read(1)
b = b.encode('hex').upper()
tmpHex = b + tmpHex
resList.append('Difficulty = ' + tmpHex)
tmpHex = ''
for j in range(4):
b = f.read(1)
b = b.encode('hex').upper()
tmpHex = b + tmpHex
resList.append('Random number > ' + tmpHex)
tmpHex = ''
b = f.read(1)
bInt = int(b.encode('hex'),16)
c = 0
if bInt < 253:
c = 1
tmpHex = b.encode('hex').upper()
if bInt == 253: c = 3
if bInt == 254: c = 5
if bInt == 255: c = 9
for j in range(1,c):
b = f.read(1)
b = b.encode('hex').upper()
tmpHex = b + tmpHex
txCount = int(tmpHex,16)
resList.append('Transactions count = ' + str(txCount))
resList.append('')
tmpHex = ''
tmpPos1 = 0
tmpPos2 = 0
RawTX = ''
tx_hashes = []
for k in range(txCount):
tmpPos1 = f.tell()
for j in range(4):
b = f.read(1)
b = b.encode('hex').upper()
tmpHex = b + tmpHex
resList.append('transactionVersionNumber = ' + tmpHex)
RawTX = reverse(tmpHex)
tmpHex = ''
我不得不减少代码,因为我的代码太多,细节也不够。