python中的base64模块
程序员文章站
2022-06-08 10:43:31
...
base64模块,不是加密算法,是用来将非ascll字符的数据转换成ascll字符的一种方法
A-Z,a-z,0-9,+ / 一共64个符号
import base64
var = b"This is the only way to the python peak road..."
var_encode = base64.b64encode(var) #对二进制数据进行base64编码
print(var_encode,len(var_encode))
var_decode = base64.b64decode(var_encode) #对编码过后的二进制数据进行解码,但是只能是同一种格式
print(var_decode)
var1 = b'https://translate.google.cn/#view=home&op=translate&sl=auto&tl=zh-CN&text=a%20bytes-like%20object%20is%20required%2C%20not%20'
url_encode = base64.urlsafe_b64encode(var1) #对url进行bs64编码
print(url_encode)
url_decode = base64.urlsafe_b64decode(url_encode) #对url进行bs64解码
print(url_decode)
运行结果
b'VGhpcyBpcyB0aGUgb25seSB3YXkgdG8gdGhlIHB5dGhvbiBwZWFrIHJvYWQuLi4=' 64
b'This is the only way to the python peak road...'
b'aHR0cHM6Ly90cmFuc2xhdGUuZ29vZ2xlLmNuLyN2aWV3PWhvbWUmb3A9dHJhbnNsYXRlJnNsPWF1dG8mdGw9emgtQ04mdGV4dD1hJTIwYnl0ZXMtbGlrZSUyMG9iamVjdCUyMGlzJTIwcmVxdWlyZWQlMkMlMjBub3QlMjA='
b'https://translate.google.cn/#view=home&op=translate&sl=auto&tl=zh-CN&text=a%20bytes-like%20object%20is%20required%2C%20not%20'