The Base64 module in Python

Source: Internet
Author: User
Tags base64



This article describes the Base64 module in Python 2.7, which provides an interface based on the rfc3548 Base16, 32, 64 codec. Official documentation, refer here.



The module provides two sets of interfaces, the traditional interface is based on the rfc1521 Base64, the current interface is based on the rfc3548 BASE16/32/64 Coding specification, this article only describes the current interface.



The current interface is introduced in Python 2.4, and the BASE64 encoding format provides the following six interfaces for efficient and flexible implementation of the required codec work.


1.b64encode(s, altchars=None)2.b64decode(s, altchars=None)3.standard_b64encode(s)4.standard_b64decode(s)5.urlsafe_b64encode(s)6.urlsafe_b64decode(s)





  Where the method ending with "*encode" is used to convert a binary string into a base64 encoded string, the method ending with "*decode" is used to re-convert the Base64-formatted string into a binary string.






We look at the first two methods in detail, noting that B64encode () and B64decode () receive the same form of parameters. where S is the string to encode/decode, the optional value of the default parameter Altchars must be a string of at least two bytes in length (the content after the second character is ignored), which means that the first two characters in the parameter altchars are replaced in the in/decode process with the ' + ' in the standard Base64 character set. ' and '/'.



Therefore Base64.standard_b64encode (s) and Base64.standard_b64decode (s) in Methods 3 and 4 are equivalent to Base64.b64encode (s) and Base64.b64decode (s). The Base64.urlsafe_b64encode (s) and Base64.urlsafe_b64decode (s) in methods 5 and 6 are respectively equivalent to Base64.b64encode (S, '-_ ') and Base64.b64decode ( S, '-_ '), which uses '-' and ' _ ' instead of ' + ' and '/' in the standard Base64 character set during/decoding, generating Base64 formatted text that can be used in the URL.



Examples of Use:


 
 1 >>> import base64
 2 >>> print base64.b64encode(‘Hello, I am Darren!‘) 
 3 SGVsbG8sIEkgYW0gRGFycmVuIQ==
 4 >>>
 5 >>> print base64.b64decode(‘SGVsbG8sIEkgYW0gRGFycmVuIQ==‘)
 6 Hello, I am Darren!
 7 >>>
 8 >>> print base64.b64encode(‘i\xb7\x1d\xfb\xef\xff‘)
 9 abcd++// 
10 >>> 
11 >>> print base64.b64encode(‘i\xb7\x1d\xfb\xef\xff‘, ‘-_‘) 
12 abcd--__ 
13 >>> 
14 >>> print base64.urlsafe_b64encode(‘i\xb7\x1d\xfb\xef\xff‘) 
15 abcd--__ 
16 >>>
17 >>> base64.urlsafe_b64decode(‘adcd--__‘) 
18 ‘i\xb7\x1d\xfb\xef\xff‘





The module also provides the BASE32 and Base16 codec interfaces:


1.b32encode(s)    2.b32decode(s, casefold=False, map01=None)  


BASE16 Codec:


1.b16encode(s)2.b16decode(s, casefold=False)


Where the parameter s are the strings to be encoded/decoded, regarding the BASE16/32 encoding specification, refer to rfc4648 or rfc3548, this article only focuses on Base64.



The Base64 module in Python


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.