The base64 Module

The base64 encoding scheme is used to convert arbitrary binary data to plain text. To do this, the encoder stores each group of three binary bytes as a group of four characters from the following set:


In addition, the = character is used for padding at the end of the data stream.

Example 4-18 shows how the encode and decode functions work on file objects.

Example 4-18. Using the base64 Module to Encode Files


import base64

MESSAGE = "life of brian"

file = open("out.txt", "w")

base64.encode(open("out.txt"), open("out.b64", "w"))
base64.decode(open("out.b64"), open("out.txt", "w"))

print "original:", repr(MESSAGE)
print "encoded message:", repr(open("out.b64").read())
print "decoded message:", repr(open("out.txt").read())

original: 'life of brian'
encoded message: 'bGlmZSBvZiBicmlhbg==12'
decoded message: 'life of brian'

Example 4-19 shows the encodestring and decodestring functions converting between strings. The functions are currently implemented as wrappers on top of encode and decode, using StringIO objects for input and output.

Example 4-19. Using the base64 Module to Encode Strings


import base64

MESSAGE = "life of brian"

data = base64.encodestring(MESSAGE)

original_data = base64.decodestring(data)

print "original:", repr(MESSAGE)
print "encoded data:", repr(data)
print "decoded data:", repr(original_data)

original: 'life of brian'
encoded data: 'bGlmZSBvZiBicmlhbg==12'
decoded data: 'life of brian'

Example 4-20 shows how to convert a username and a password to an HTTP basic authentication string. (Note that you don't really have to work for the NSA to be able to decode this format.)

Example 4-20. Using the base64 Module for Basic Authentication


import base64

def getbasic(user, password):
 # basic authentication (according to HTTP)
 return base64.encodestring(user + ":" + password) 

print getbasic("Aladdin", "open sesame")


Finally, Example 4-21 shows a small utility that converts a GIF image to a Python script, for use with the Tkinter library.

Example 4-21. Using the base64 Module to Wrap GIF Images for Tkinter


import base64, sys

if not sys.argv[1:]:
 print "Usage: giffile >pyfile"

data = open(sys.argv[1], "rb").read()

if data[:4] != "GIF8":
 print sys.argv[1], "is not a GIF file"

print '# generated from', sys.argv[1], 'by'
print 'from Tkinter import PhotoImage' 
print 'image = PhotoImage(data="""'
print base64.encodestring(data),
print '""")'

# generated from samples/sample.gif by

from Tkinter import PhotoImage

image = PhotoImage(data="""

Core Modules

More Standard Modules

Threads and Processes

Data Representation

File Formats

Mail and News Message Processing

Network Protocols


Multimedia Modules

Data Storage

Tools and Utilities

Platform-Specific Modules

Implementation Support Modules

Other Modules

show all menu

Python Standard Library
Python Standard Library (Nutshell Handbooks) with
ISBN: 0596000960
EAN: 2147483647
Year: 2000
Pages: 252
Authors: Fredrik Lundh
Similar book on Amazon © 2008-2017.
If you may any questions please contact us: