我在玩耍,试图用getblocktemplate方法拉比特币块模板。我得到了一个响应,但当我尝试从字节解码时,我收到一个错误。检查出来:UnicodeDecodeError:'utf-8'编解码器无法解码字节(python)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
HOST = "184.70.15.166" # choose a node from blockchain.info
PORT = 8333
s.connect((HOST, PORT))
s.send(msg)
s.recv(1024)
x = random.randrange(320)
print(x)
data = json.dumps({'version': '2.0', 'id': 'x', 'method': 'getblocktemplate'}).encode('utf-8').strip()
s.send(data)
resp = s.recv(2048).decode('utf8')
print (resp)
# prints id number -
315
# prints response (in bytes) -
b'\xf9\xbe\xb4\xd9verack\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00]\xf6\xe0\xe2\xf9\xbe\xb4\xd9alert\x00\x00\x00\x00\x00\x00\x00\xa8\x00\x00\x00\x1b\xf9\xaa\xea`\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\x7f\x00\x00\x00\x00\xff\xff\xff\x7f\xfe\xff\xff\x7f\x01\xff\xff\xff\x7f\x00\x00\x00\x00\xff\xff\xff\x7f\x00\xff\xff\xff\x7f\x00/URGENT: Alert key compromised, upgrade required\x00F0D\x02 e?\xeb\xd6A\x0fG\x0fk\xae\x11\xca\xd1\x9cHA;\xec\xb1\xac,\x17\xf9\x08\xfd\x0f\xd5;\xdc:\xbdR\x02 m\x0e\x9c\x96\xfe\x88\xd4\xa0\xf0\x1e\xd9\xde\xda\xe2\xb6\xf9\xe0\r\xa9L\xad\x0f\xec\xaa\xe6n\xcfh\x9b\xf7\x1bP\xf9\xbe\xb4\xd9ping\x00\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00-8\xc9\x03\xd7\xbc\x0f\xc9\xc2\x1d36'
# try to decode from bytes and get this error -
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 699, in runfile
execfile(filename, namespace)
File "/usr/lib/python3/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 88, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
File "/home/myfolder/Desktop/bitcoin/bitcoin 2017/connection.py", line 66, in <module>
print (resp.decode('utf8'))
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf9 in position 0: invalid start byte
你怎么看?我一直在寻找所有的网络,并找不到修复工程。
似乎's.recv'收到的数据不是正确的'utf-8'编码,你可能要仔细检查服务器的api。 –
getblocktemplate方法与json-rpc相当标准。我想知道我是否在数据格式上犯了一个错误。我真正想要做的就是使用getblocktemplate方法拖动最新的块模板。这让我疯狂。 – Engine