文件数据压缩
import zlib
import gzip
import requests
res = requests.get('http://www.*.com')
with open('*.html', 'w', encoding='utf-8') as file:
file.write(res.text)
with open('sohu_index.zip', 'wb') as file2:
file2.write(zlib.compress(res.content, 9))
with open('sohu_index.zip', 'rb') as file:
unzip_data = gzip.decompress(file.read())
print(unzip_data.decode('utf-8'))
with open('aa.jpg', 'rb') as file:
with open('aa.gz', 'wb') as file2:
file2.write(gzip.compress(file.read(), 9))
给数据生成签名
"""
给数据生成签名(signature) /指纹(fingerprint) /摘要(digest)
MD5 / SHA-1 / SHA-256 ---> 签名算法(哈希函数) ---> 摘要
特点 不同的对象几乎不可能差生相同的签名(每个对象的摘要都是独一无二的)
对象 ---> MD5 ---> 128bit ---> 32个十六进制字符
对象 ---> SHA256 ---> 256bit ---> 64个十六进制字符
hashlib ---> md5 / sha1 / sha256 ---> hexdigest()
"""
import hashlib
hashes = hashlib.md5()
with open('python-3.9.6-amd64.exe', 'rb') as file:
for data in iter(lambda: file.read(512), b''):
hashes.update(data)
print(hashes.hexdigest())