Brotli是一种开源的新型压缩算法,压缩效率性能优秀。通过压缩后进行网络数据传输,可以大大减少网络负载,加快数据传输速度。
python中可以使用pip install Brotli安装br库,目前版本为1.0.9
pip list
......
Brotli 1.0.9
简单的使用br.py,测试如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import brotli
data = "Bash is intended to be a conformant implementation of the Shell and Utilities portion of the IEEE POSIX specification (IEEE Standard 1003.1). Bash can be configured to be POSIX-conformant by default."
print("ori_data:\n%s" % data)
print("data size: %s" % len(data))
compress_data = brotli.compress(data.encode('utf-8'))
print("compress size: %s" % len(compress_data))
decompress_data = brotli.decompress(compress_data)
print("decompress:\n%s" % decompress_data.decode('utf-8'))
执行br.py结果:
# python br.py
ori_data:
Bash is intended to be a conformant implementation of the Shell and Utilities portion of the IEEE POSIX specification (IEEE Standard 1003.1). Bash can be configured to be POSIX-conformant by default.
data size: 201
compress size: 107
decompress:
Bash is intended to be a conformant implementation of the Shell and Utilities portion of the IEEE POSIX specification (IEEE Standard 1003.1). Bash can be configured to be POSIX-conformant by default.
可以看出长度为201的字符串,压缩后长度仅为107,压缩比优秀。