我正在尝试压缩字典以访问API 我读了一些用JavaScript压缩数据的代码和一个名为" pako"并亲自尝试过。它完美地运作:
var myDictionary = {...}
var b = pako.deflate(JSON.stringify(a), {
to: "string",
gzip: !0
});
return b = btoa(b)
}
var compressed = b(n)
现在我想对Python做同样的事情: 我尝试了以下方法,但结果不同,并且不起作用:
my_dictionary = {...}
data_json = json.dumps(my_dictionary, ensure_ascii=False)
data_gzip = zlib.compress(bytes(data_json, "utf-8"))
compressed = base64.b64encode(bytes(str(data_gzip), "utf-8"))
有没有人理想如何用Python解决这个问题?是否有与Python相似的pako库?
答案 0 :(得分:0)
如果有人还在python 3中寻找pako等效方法(未经python 2测试)。
pako.deflate()方法等效于python:
def pako_deflate(data):
compress = zlib.compressobj(zlib.Z_DEFAULT_COMPRESSION, zlib.DEFLATED, 15,
memLevel=8, strategy=zlib.Z_DEFAULT_STRATEGY)
compressed_data = compress.compress(js_string_to_byte(js_encode_uri_component(data)))
compressed_data += compress.flush()
return compressed_data
python中的pako.deflateRaw()等效项:
def pako_deflate_raw(data):
compress = zlib.compressobj(
zlib.Z_DEFAULT_COMPRESSION, zlib.DEFLATED, -15, memLevel=8,
strategy=zlib.Z_DEFAULT_STRATEGY)
compressed_data = compress.compress(js_string_to_byte(js_encode_uri_component(data)))
compressed_data += compress.flush()
return compressed_data
pako.inflate()方法等效项:
def pako_inflate(data):
decompress = zlib.decompressobj(15)
decompressed_data = decompress.decompress(data)
decompressed_data += decompress.flush()
return decompressed_data
pako.inflateRaw()方法等效项:
def pako_inflate_raw(data):
decompress = zlib.decompressobj(-15)
decompressed_data = decompress.decompress(data)
decompressed_data += decompress.flush()
return decompressed_data
答案 1 :(得分:0)
from urllib import quote, unquote
import base64
import zlib
def js_encode_uri_component(data):
return quote(data)
def js_string_to_byte(data):
return bytes(data)
def js_bytes_to_string(data):
return data.decode('iso-8859-1')
def js_btoa(data):
return base64.b64encode(data)
def js_atob(data):
return base64.b64decode(data)
def pako_inflate_raw(data):
decompress = zlib.decompressobj(-15)
decompressed_data = decompress.decompress(data)
decompressed_data += decompress.flush()
return decompressed_data
def pako_deflate_raw(data):
compress = zlib.compressobj(
zlib.Z_DEFAULT_COMPRESSION, zlib.DEFLATED, -15, 8,
zlib.Z_DEFAULT_STRATEGY)
compressed_data = compress.compress(js_string_to_byte(js_encode_uri_component(data)))
compressed_data += compress.flush()
return compressed_data
data = "0b cb 49 cc 4b 67 30 64 28 c8 2f 4f 2d 2a ce 48 cd c9 61 70 d3 4b ce 4f 49 d5 2b c9 cc 67 30 34 35 60 48 4d ce c8 57 50 37 a8 48 31 30 a8 b0 1e 08 00 76 80 52 5a 9a a5 a1 a1 a1 25 18 28 55 a8 bb e9 65 e6 15 94 96 80 5d 69 c0 10 96 58 94 5e 0c a4 83 00".replace(" ","").decode("hex")
data_main = """Vlang\x001\x00powershell\x00F.code.tio\x0038\x00echo \'0xd00\xe2\x80\x83;echo "ff9111999999"\xe2\x80\x83\'F.input.tio\x000\x00Vargs\x000\x00R"""
print repr(pako_inflate_raw(data))
print repr(pako_deflate_raw(data_main))