简体   繁体   English

如何 base64 编码 python 数组,而不是双重编码?

[英]How to base64 encode a python array, not double-encode it?

I want to encode an array using base64 in order to transform it into JSON for sending over websocket.我想使用 base64 对数组进行编码,以便将其转换为 JSON 以通过 websocket 发送。 I followed the advice of ssubotin in this SO question in my Python code.我在我的 Python 代码中的这个 SO 问题中遵循了 ssubotin 的建议。 On the receiving end, in javascript, I use window.atob() to decode the string.在接收端,在 javascript 中,我使用 window.atob() 来解码字符串。 Trouble is, I have to use window.atob() twice.麻烦的是,我必须使用 window.atob() 两次。 This suggests that I somehow double-encoded my data and thus made the string 33% longer than it needed to be.这表明我以某种方式对我的数据进行了双重编码,从而使字符串比需要的长 33%。 Some output is shown below the code.一些 output 显示在代码下方。

# based on https://websockets.readthedocs.io/en/9.0.1/intro.html
import asyncio
import json
from base64 import b64encode, b64decode
import array

myarray = array.array('H', [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]) 

class Base64Encoder(json.JSONEncoder):
    # ssubotin, https://stackoverflow.com/questions/37225035/serialize-in-json-a-base64-encoded-data
    def default(self, o):
        if isinstance(o, bytes):
            return b64encode(o).decode()
        return json.JSONEncoder.default(self, o)
        

# array_event() creates a message which will be sent over websocket

def array_event():
    bytextnd = bytearray()
    for x in range(len(myarray)):
        bytextnd.extend(myarray[x].to_bytes(2, byteorder='big'))
    print(format(b64decode(b64encode(bytextnd)))) # gives bytes en/de-coded
    print(json.dumps({"type": "array", "array": b64encode(bytextnd)}, cls=Base64Encoder))
    return json.dumps({"type": "array", "array": b64encode(bytextnd)}, cls=Base64Encoder)

Python output: b'\x00\x01\x00\x02\x00\x03\x00\x04 [...] \x00\x0f\x00\x10' Python output: b'\x00\x01\x00\x02\x00\x03\x00\x04 [...] \x00\x0f\x00\x10'

{"type": "array", "array": "QUFFQUFnQURBQVFBQlFBR0FBY0FDQUFKQUFvQUN3QU1BQTBBRGdBUEFCQT0="} {“类型”:“数组”,“数组”:“QUFFQUFnQURBQVFBQlFBR0FBY0FDQUFKQUFvQUN3QU1BQTBBRGdBUEFCQT0="}

Javascript yields this as the result of the first window.atob(): AAEAAgADAAQABQAGAAcACAAJAAoACwAMAA0ADgAPABA= which correctly yields the array after a second window.atob() and some DataView and charCodeAt() magic that I borrowed from Nina Scholz. Javascript yields this as the result of the first window.atob(): AAEAAgADAAQABQAGAAcACAAJAAoACwAMAA0ADgAPABA= which correctly yields the array after a second window.atob() and some DataView and charCodeAt() magic that I borrowed from Nina Scholz.

your doing too much work...你做的工作太多...

a = array.array('H',range(1,17))
packed_bytes = struct.pack(f">{len(a)}H",*a)
base64_bytes = base64.b64encode(packed_bytes)
print(base64_bytes)
# b'AAEAAgADAAQABQAGAAcACAAJAAoACwAMAA0ADgAPABA='

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM