我在对websocket接口进行请求时,无法解析服务器发送的字段进行解析,在网上找了好久才实现
服务器发送字段是用JavaScript写的,他们用的在这个进行解包
/* pako压缩 */
function unzip(str){
return pako.ungzip(str);
}
/* pako解压 */
function zip(str){
var bytes = pako.gzip(str, { to: ‘String’ });
return bytes;
}
使用Python实现对服务器发送的字段进行解析
import base64
import gzip
from io import BytesIO
s = bytes(
“H4sIAAAAAAAAAKvmUlBQSs5PSVVSsFJQsjQwMDQwU9IBCeYWp4PFns1Z9Xz+0uebdz/fPf/llJkv1q+HyBcklmSAFSSWlmTogQiIeFFqcYFnCljGIiXRwMIwOU3XxNDCSNfEzNhE19LSKFk3JSnFwDjZLNnA0NhSiasWAPb7xfSEAAAA”,
encoding=“utf-8”
)
def zip_data(content):
z_buf = BytesIO()
z_file = gzip.GzipFile(mode=‘wb’, compresslevel=9, fileobj=z_buf)
z_file.write(content)
z_file.close()
return base64.b64encode(z_buf.getvalue())
def unzip_data(content):
compressed_data = base64.b64decode(content)
compressed_stream = BytesIO(compressed_data)
gzip_per = gzip.GzipFile(fileobj=compressed_stream)
data = gzip_per.read()
return data
s1 = unzip_data(s)
print(str(s1, encoding=“utf-8”))
s2 = zip_data(s1)
print(s2)
##打印的结果`