Python Built-In Implementation of MIME Base64

This section provides a tutorial example on how to perform Base64 encoding on binary files with both standard and MIME options using the default Python implementation of the Base64 encoding algorithm.

Python also offers a variation of the Base64 algorithm called MIME (Multipurpose Internet Mail Extensions) Encoder, which wraps the encoded output into 76 characters to meet the MIME specification.

To perform MIME Base64 encoding, you need to call the base64.encodebytes() method instead of base64.b64encode().

Here is a sample program that allows you to perform Base64 encoding on any given file with both standard and MIME options:

# Base64_Mime_Encoder.py
# Copyright (c) 2010 HerongYang.com. All Rights Reserved.
#
import sys
import base64

if (len(sys.argv)<3):
   print("Usage:")
   print("python Base64_Mime_Encoder.py input_file mime")
   exit
    
fileName = sys.argv[1]
mime = sys.argv[2]
inputStream = open(fileName, "rb")
inputBytes = inputStream.read()
inputStream.close()

encodedBytes = b""
if (mime=="mime"):
  encodedBytes = base64.encodebytes(inputBytes)
else:
  encodedBytes = base64.b64encode(inputBytes)

print(encodedBytes.decode())

To try the above program, you can download the binary image file from my Website:

herong> curl http://herongyang.com/_logo.png > icon.png  

Now encode this binary file with both MIME and standard option:

herong> python3 Base64_Mime_Encoder.py icon.png mime 
iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAAAXNSR0IArs4c6QAAAARnQU1BAACx
jwv8YQUAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAAAEYSURBVEhL7ZbLDcIwDIYdBGfYADahm1E24QRr
9AxLNBvAuUjgJE7ruI5S8RAXPslqaltxfsdSax4IfJEZPQeMSa2EyLdmTwHCKUhwLmYt1BTIIPIl
eovkyZUUj8zrrgDzJb0EQovuN64SbFt7d4/cCLE2bYWFCsxi2e9xutxCwOvormOV3OEMcxJkXLiO
55A/vuSIy+MsVrTA0+5TRfbQ0ErBl9EUIO2hGpxobUUXznzeCO6KCkI0U8DDA5oxuLvcooibjAy2
Zq3BQdEoF8CxsxWtBevdllZ51AJx1KJtGlQtwU4keTiiGmUFGdymU3i5wFTUAuk8BJOMcrpXL/lN
/gWKfK6A+A5EpheQYzORr7foB38VHwXgCfzQYOFCEQYGAAAAAElFTkSuQmCC

herong> python3 Base64_Mime_Encoder.py icon.png standard
iVBORw0KGgoAAAANSUhEUgAAABgAAAAY ... HwXgCfzQYOFCEQYGAAAAAElFTkSuQmCC

As you can see, the MIME Base64 encoding is identical to the standard Base64 encoding, except that it adds line breaks to keep the output in 76-character line format.

According the Base64 specification, adding line breaks and other whitespaces in the encoded output is allowed.

Table of Contents

 About This Book

 Base64 Encoding

Base64 Encoding and Decoding Tools

 Base64.Guru - Base64 Online Tool

 Windows Command - "certutil -encode/-decode"

 Linux Command - "base64"

 macOS Command - "base64"

 Java Built-In Implementation of Base64

 Java Built-In Implementation of MIME Base64

 Python Built-In Implementation of Base64

Python Built-In Implementation of MIME Base64

 PHP Built-In Implementation of Base64

 PHP Built-In Implementation of MIME Base64

 Perl Built-In Implementation of Base64

 Perl Built-In Implementation of MIME Base64

 Base64URL - URL Safe Base64 Encoding

 Base32 Encoding

 URL Encoding, URI Encoding, or Percent Encoding

 UUEncode Encoding

 References

 Full Version in PDF/EPUB