Python 2与Python 3下的base64模块
Python2的编解码
python2中程序数据类型默认为ASCII,所以需要先将数据解码(decode)成为Unicode类型,然后再编码(encode)成为想要转换的数据类型(gbk,utf-8,gb18030,gb2312),然后再解码成为对应的数据类型显示在屏幕上;
Python3的编解码
python3中程序默认数据类型为Unicode,所以直接将数据编码(encode)成为想要转换的数据类型(gbk,utf-8,gb18030,gb2312),然后解码成为对应的数据类型显示在屏幕上。
base64
Base64编码是一种“防君子不防小人”的编码方式。广泛应用于MIME协议,作为电子邮件的传输编码,生成的编码可逆,后一两位可能有“=”,生成的编码都是ascii字符。
因此对于python2来说,编解码相对要容易一些。python3因为要从Unicode转换一下,相对麻烦一些。一切见下例:
Python2
def b64encode(s, altchars=None):
"""Encode a string using Base64.
s is the string to encode. Optional altchars must be a string of at least
length 2 (additional characters are ignored) which specifies an
alternative alphabet for the '+' and '/' characters. This allows an
application to e.g. generate url or filesystem safe Base64 strings.
The encoded string is returned.
"""
# Strip off the trailing newline
encoded = binascii.b2a_base64(s)[:-1]
if altchars is not None:
return encoded.translate(string.maketrans(b'+/', altchars[:2]))
return encoded
b64decode源码
def b64decode(s, altchars=None):
"""Decode a Base64 encoded string.
s is the string to decode. Optional altchars must be a string of at least
length 2 (additional characters are ignored) which specifies the
alternative alphabet used instead of the '+' and '/' characters.
The decoded string is returned. A TypeError is raised if s is
incorrectly padded. Characters that are neither in the normal base-64
alphabet nor the alternative alphabet are discarded prior to the padding
check.
"""
if altchars is not None:
s = s.translate(string.maketrans(altchars[:2], '+/'))
try:
return binascii.a2b_base64(s)
except binascii.Error, msg:
# Transform this exception for consistency
raise TypeError(msg)
这里面的s是一个字符串类型的对象。
import base64
s = 'Hello, python'
b = base64.b64encode(s)
print 'b为:', b
c = base64.b64decode(b)
print 'c为:', c
# output
b为: SGVsbG8sIHB5dGhvbg==
c为: Hello, python
Python3
def b64encode(s, altchars=None):
"""Encode the bytes-like object s using Base64 and return a bytes object.
Optional altchars should be a byte string of length 2 which specifies an
alternative alphabet for the '+' and '/' characters. This allows an
application to e.g. generate url or filesystem safe Base64 strings.
"""
encoded = binascii.b2a_base64(s, newline=False)
if altchars is not None:
assert len(altchars) == 2, repr(altchars)
return encoded.translate(bytes.maketrans(b'+/', altchars))
return encoded
b64decode源码
def b64decode(s, altchars=None, validate=False):
"""Decode the Base64 encoded bytes-like object or ASCII string s.
Optional altchars must be a bytes-like object or ASCII string of length 2
which specifies the alternative alphabet used instead of the '+' and '/'
characters.
The result is returned as a bytes object. A binascii.Error is raised if
s is incorrectly padded.
If validate is False (the default), characters that are neither in the
normal base-64 alphabet nor the alternative alphabet are discarded prior
to the padding check. If validate is True, these non-alphabet characters
in the input result in a binascii.Error.
"""
s = _bytes_from_decode_data(s)
if altchars is not None:
altchars = _bytes_from_decode_data(altchars)
assert len(altchars) == 2, repr(altchars)
s = s.translate(bytes.maketrans(altchars, b'+/'))
if validate and not re.match(b'^[A-Za-z0-9+/]*={0,2}$', s):
raise binascii.Error('Non-base64 digit found')
return binascii.a2b_base64(s)
这里面的s是一个bytes对象,则字符串首先要经过编码encode()。经过b64encode/b64decode之后的返回结果也是bytes对象,所以我们要转换为Unicode对象就要再使用decode()方法去解码。
import base64
s = 'Hello, Python!'
b = base64.b64encode(s.encode('utf-8')).decode('utf-8')
print(b)
c = base64.b64decode(b.encode('utf-8')).decode('utf-8')
print(c)
# output
SGVsbG8sIFB5dGhvbiE=
Hello, Python!
- python中base64模块的加解密函数
- Python Base64 模块 实现NC文件 转换 文本文件
- python3下的IE自动化模块PAMIE
- python 常用内建模块(3) base64
- Python使用base64模块进行二进制数据编码详解
- 使用Python的base64 编码模块,编写去除 “=”的解码函数
- Python模块介绍之base64模块、Queue模块
- python学习笔记7.4-内建模块base64
- [Python模块学习]使用base64模块进行二进制数据编码
- Python模块——HashLib与base64
- 7.27 pyc文件反编译到Python源码及base64模块在python中的应用
- python中base64模块的加解密函数
- python模块之base64
- python之base64模块
- python3 base64模块代码分析
- python3下的IE自动化模块PAMIE
- python之模块base64
- 详解Python中使用base64模块来处理base64编码的方法
- python模块介绍- base64 使用ASCII编码二进制数据
- Base64 编码原理简介和python的base64模块的使用