Summary of the basic concepts of the Python base64 Module

Source: Internet
Author: User

There are many useful modules in the Python programming language for us to get some help in practical applications. For example, the Python base64 module introduced today is an important module. Here we will give a detailed introduction to the basic concepts of this module.

The Python base64 module is used for base64 encoding and decoding. This encoding method is very common in email. It can encode binary data that cannot be displayed as text information to be displayed. The encoded text size increases by 1/3.

Simplest encryption/Decryption instance

 
 
  1. import base64  
  2. str1 = 'djhui' 
  3. str2 = base64.b64encode(str1)  
  4. str3 = base64.b64decode(str2) 

The Python base64 module only uses eight methods: encode, decode, encodestring, decodestring, b64encode, b64decode, urlsafe_b64decode, and urlsafe_b64encode. The eight of them can be divided into four groups: encode and decode, which are used to encode and decode files. They can also perform encoding and decoding on the data in StringIO; encodestring and decodestring, this function is used to encode and decode strings. b64encode and b64decode are used to encode and decode strings and replace symbol characters.

This function is like this: the base64 encoded characters include three letters +/=, here, the value = is only used to encode the number of characters with four integers, while the value + and/must be replaced in some cases. b64encode and b64decode provide this function. When + and/need to be replaced, the most common is the base64 encoding of the url. Urlsafe_b64encode and urlsafe_b64decode are used to encode and encode the url in base64. in fact, they are also called by the previous functions.

The following is an example of the Python base64 module:

 
 
  1. #-*-Encoding: gb2312 -*-
  2. Import base64
  3. Import StringIO
  4. A = "this is a test"
  5. B = base64.encodestring (a) # encode the string
  6. Print B
  7. Print base64.decodestring (B) # decodes a string
  8. C = StringIO. StringIO ()
  9. C. write ()
  10. D = StringIO. StringIO ()
  11. E = StringIO. StringIO ()
  12. C. seek (0)
  13. Base64.encode (c, d) # encode the data in StringIO
  14. Print d. getvalue ()
  15. D. seek (0)
  16. Base64.decode (d, e) # decode the data in StringIO
  17. Print e. getvalue ()
  18. A = "this is a + test"
  19. B = base64.urlsafe _ b64encode (a) # encode the url string
  20. Print B
  21. Print base64.urlsafe _ b64decode (B)

The above encode function and decode function parameters can also be file objects, which is like this:

 
 
  1. f1 = open('aaa.txt', 'r')  
  2. f2 = open('bbb.txt', 'w')  
  3. base64.encode(f1, f2)  
  4. f1.close()  
  5. f2.close() 

The above is the concept of the Python base64 module.

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.