2017-02-15 7 views
0

getblocktemplate 메소드를 사용하여 비트 코인 블록 템플릿을 가져 오려고합니다. 응답을 받았지만 바이트에서 디코딩하려고하면 오류가 발생합니다. 확인해보세요.UnicodeDecodeError : 'utf-8'코덱이 바이트 (파이썬)를 디코딩 할 수 없습니다.

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 

어떻게 생각하세요? 나는 웹 전체를 조사 해왔고 제대로 작동하는 픽스를 찾지 못했습니다.

예를 들어, 데이터는 다음과 같습니다 :

+0

's.recv'에 의해 수신 된 데이터가 적절하게'utf-8'로 인코딩되지 않은 것 같습니다. 서버의 api를 두 번 확인하는 것이 좋습니다. –

+0

getblocktemplate 메서드는 꽤 json-rpc 표준입니다. 나는 어딘가에서 데이터 형식으로 실수를하고 있는지 궁금해. 내가하고 싶은 것은 getblocktemplate 메소드를 사용하여 최신 블록 템플릿을 가져 오는 것입니다. 이것은 나를 미치게합니다. – Engine

답변

1

즉각적인 솔루션은 이런 식으로 같은 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#[email protected]<[email protected]: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);^[email protected]\![[email protected]#:E,K0_LJN9NSVB;]QM0^;ZTV7!I;F< 
7   @ M.,D#U[P/R<(=,S8 
+0

고맙지 만 아직 이해할 수없는 결과를 낳는 것은 아닙니다. 나는 어딘가에서 데이터 형식으로 실수를하고 있는지 궁금해. – Engine

+0

디코딩 된 출력 데이터에 대한 아이디어가 있다면 공유하십시오. 그렇지 않으면 Vitalik이 만든 bitcoin 패키지는 binascii도 사용합니다. –

+1

귀하의 답변이 적용 ​​가능하다고 생각합니다. 나는 데이터 형식을 출력 할 때 오류가 있다고 생각합니다. 응답은 json 객체 여야하고 json로드 및 utf-8로 인코딩 할 수없는 바이트 문제를 처리해야했습니다. 나가서 배우기. – Engine

0

나는 파이썬 bitcoinrpc라는 비트 코인에 대한 파이썬 RPC 라이브러리를 발견 - 내 문제 해결 :

rpc_connection = AuthServiceProxy ('http://%s:%[email protected]:8332'% ('rpc_username', 'rpc_password'))

block_template = rpc_connection합니다. getblocktemplate()

인쇄 (block_template)

친절한 API와 환상적으로 작동합니다.