Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
UnicodeDecodeError:“utf-8”编解码器无法解码字节(python)_Python_Utf 8_Byte_Decoding_Bitcoin - Fatal编程技术网

UnicodeDecodeError:“utf-8”编解码器无法解码字节(python)

UnicodeDecodeError:“utf-8”编解码器无法解码字节(python),python,utf-8,byte,decoding,bitcoin,Python,Utf 8,Byte,Decoding,Bitcoin,我在玩游戏,尝试使用getblocktemplate方法提取比特币块模板。我得到了一个响应,但当我试图从字节中解码它时,我得到了一个错误。请查看: 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)

我在玩游戏,尝试使用getblocktemplate方法提取比特币块模板。我得到了一个响应,但当我试图从字节中解码它时,我得到了一个错误。请查看:

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

你觉得怎么样?我一直在网上搜索,找不到有效的修复方法。

一个直接的解决方案是像这样使用binascii:

例如,您的数据是:

a = 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'
您可以这样做:

import binascii

# binascii.b2a_uu takes at most 45 bytes at once
# this why i'm splitting the data into chunks of 45 bytes at most
b = [a[k:k+45] for k in range(0, len(a), 45)]

# You can also use:
# binascii.b2a_uu(k).decode('UTF8')
final = "".join(binascii.b2a_uu(k).decode() for k in b)

print(final)
输出:

M^;ZTV79E<F%C:P            !=]N#B^;ZTV6%L97)T         *@    ;
M^:KJ8 $              /___W\     ____?_[__W\!____?P    #___]_
M /___W\ +U521T5.5#H@06QE<G0@:V5Y(&-O;7!R;VUI<V5D+"!U<&=R861E
M(')E<75I<F5D $8P1 (@93_KUD$/1P]KKA'*T9Q(03OLL:PL%_D(_0_5.]PZ
MO5("(&T.G);^B-2@\![9WMKBMOG@#:E,K0_LJN9NSVB;]QM0^;ZTV7!I;F< 
7          @    M.,D#U[P/R<(=,S8 

我发现了一个用于比特币的python rpc库,名为

python-bitcoinrpc
它解决了我的问题:

rpc_connection = AuthServiceProxy('http://%s:%s@127.0.0.1:8332'%('rpc_username','rpc_password'))

block_template = rpc_connection.getblocktemplate()

print(block_template)

友好的API,工作非常出色。

s.recv接收的数据似乎没有正确的utf-8编码,您可能需要仔细检查服务器的API。getblocktemplate方法非常标准,带有json rpc。我想知道我在数据格式方面是否犯了错误。我真正想做的就是使用getblocktemplate方法拉取最新的块模板。这让我快发疯了。谢谢,但它似乎仍然没有产生一个可以理解的结果。我想知道我是否在数据格式方面犯了错误。如果你对解码后的输出数据有什么想法,请分享。另外,Vitalik构建的比特币包也使用binascii。我相信你的答案是适用的。我想我在输出数据类型时出错了。响应需要是一个json对象,我需要处理json负载和一个无法编码为utf-8的字节问题。边走边学。