python2與python3下的base64模塊

Python2的編解碼python

python2中程序數據類型默認爲ASCII,因此須要先將數據解碼(decode)成爲Unicode類型,而後再編碼(encode)成爲想要轉換的數據類型(gbk,utf-8,gb18030,gb2312),而後再解碼成爲對應的數據類型顯示在屏幕上;git

Python3的編解碼app

python3中程序默認數據類型爲Unicode,因此直接將數據編碼(encode)成爲想要轉換的數據類型(gbk,utf-8,gb18030,gb2312),而後解碼成爲對應的數據類型顯示在屏幕上。ide

base64this

Base64編碼是一種「防君子不防小人」的編碼方式。普遍應用於MIME協議,做爲電子郵件的傳輸編碼,生成的編碼可逆,後一兩位可能有「=」,生成的編碼都是ascii字符。編碼

所以對於python2來講,編解碼相對要容易一些。python3由於要從Unicode轉換一下,相對麻煩一些。一切見下例:url

  • Python2
 1 def b64encode(s, altchars=None):
 2     """Encode a string using Base64.
 3 
 4     s is the string to encode.  Optional altchars must be a string of at least
 5     length 2 (additional characters are ignored) which specifies an
 6     alternative alphabet for the '+' and '/' characters.  This allows an
 7     application to e.g. generate url or filesystem safe Base64 strings.
 8 
 9     The encoded string is returned.
10     """
11     # Strip off the trailing newline
12     encoded = binascii.b2a_base64(s)[:-1]
13     if altchars is not None:
14         return encoded.translate(string.maketrans(b'+/', altchars[:2]))
15     return encoded
 1 def b64decode(s, altchars=None):
 2     """Decode a Base64 encoded string.
 3 
 4     s is the string to decode.  Optional altchars must be a string of at least
 5     length 2 (additional characters are ignored) which specifies the
 6     alternative alphabet used instead of the '+' and '/' characters.
 7 
 8     The decoded string is returned.  A TypeError is raised if s is
 9     incorrectly padded.  Characters that are neither in the normal base-64
10     alphabet nor the alternative alphabet are discarded prior to the padding
11     check.
12     """
13     if altchars is not None:
14         s = s.translate(string.maketrans(altchars[:2], '+/'))
15     try:
16         return binascii.a2b_base64(s)
17     except binascii.Error, msg:
18         # Transform this exception for consistency
19         raise TypeError(msg)
b64decode源碼

這裏面的s是一個字符串類型的對象。spa

 1 import base64
 2 
 3 s = 'Hello, python'
 4 b = base64.b64encode(s)
 5 print 'b爲:', b
 6 
 7 c = base64.b64decode(b)
 8 print 'c爲:', c
 9 
10 
11 # output
12 b爲: SGVsbG8sIHB5dGhvbg==
13 c爲: Hello, python
  • Python3
 1 def b64encode(s, altchars=None):
 2     """Encode the bytes-like object s using Base64 and return a bytes object.
 3 
 4     Optional altchars should be a byte string of length 2 which specifies an
 5     alternative alphabet for the '+' and '/' characters.  This allows an
 6     application to e.g. generate url or filesystem safe Base64 strings.
 7     """
 8     encoded = binascii.b2a_base64(s, newline=False)
 9     if altchars is not None:
10         assert len(altchars) == 2, repr(altchars)
11         return encoded.translate(bytes.maketrans(b'+/', altchars))
12     return encoded
 1 def b64decode(s, altchars=None, validate=False):
 2     """Decode the Base64 encoded bytes-like object or ASCII string s.
 3 
 4     Optional altchars must be a bytes-like object or ASCII string of length 2
 5     which specifies the alternative alphabet used instead of the '+' and '/'
 6     characters.
 7 
 8     The result is returned as a bytes object.  A binascii.Error is raised if
 9     s is incorrectly padded.
10 
11     If validate is False (the default), characters that are neither in the
12     normal base-64 alphabet nor the alternative alphabet are discarded prior
13     to the padding check.  If validate is True, these non-alphabet characters
14     in the input result in a binascii.Error.
15     """
16     s = _bytes_from_decode_data(s)
17     if altchars is not None:
18         altchars = _bytes_from_decode_data(altchars)
19         assert len(altchars) == 2, repr(altchars)
20         s = s.translate(bytes.maketrans(altchars, b'+/'))
21     if validate and not re.match(b'^[A-Za-z0-9+/]*={0,2}$', s):
22         raise binascii.Error('Non-base64 digit found')
23     return binascii.a2b_base64(s)
b64decode源碼

這裏面的s是一個bytes對象,則字符串首先要通過編碼encode()。通過b64encode/b64decode以後的返回結果也是bytes對象,因此咱們要轉換爲Unicode對象就要再使用decode()方法去解碼。code

 1 import base64
 2 
 3 s = 'Hello, Python!'
 4 b = base64.b64encode(s.encode('utf-8')).decode('utf-8')
 5 print(b)
 6 
 7 c = base64.b64decode(b.encode('utf-8')).decode('utf-8')
 8 print(c)
 9 
10 # output
11 SGVsbG8sIFB5dGhvbiE=
12 Hello, Python!
相關文章
相關標籤/搜索