This module provides data encoding and decoding as specified inRFC 3548.This standard defines the Base16, Base32, and Base64 algorithms for encodingand decoding arbitrary binary strings into ASCII-only byte strings that can besafely sent by email, used as parts of URLs, or included as part of an HTTPPOST request. The encoding algorithm is not the same as theuuencode program.
There are two interfaces provided by this module. The modern interfacesupports encoding and decoding ASCII byte string objects using all threealphabets. Additionally, the decoding functions of the modern interface alsoaccept Unicode strings containing only ASCII characters. The legacy interfaceprovides for encoding and decoding to and from file-like objects as well asbyte strings, but only using the Base64 standard alphabet.
Changed in version 3.3:ASCII-only Unicode strings are now accepted by the decoding functions ofthe modern interface.
The modern interface provides:
Encode a byte string using Base64.
s is the string to encode. Optionalaltchars must be a string of at leastlength 2 (additional characters are ignored) which specifies an alternativealphabet for the+ and/ characters. This allows an application to e.g.generate URL or filesystem safe Base64 strings. The default isNone, forwhich the standard Base64 alphabet is used.
The encoded byte string is returned.
Decode a Base64 encoded byte string.
s is the byte string to decode. Optionalaltchars must be a string ofat least length 2 (additional characters are ignored) which specifies thealternative alphabet used instead of the+ and/ characters.
The decoded string is returned. Abinascii.Error exception is raisedifs is incorrectly padded.
Ifvalidate isFalse (the default), non-base64-alphabet characters arediscarded prior to the padding check. Ifvalidate isTrue,non-base64-alphabet characters in the input result in abinascii.Error.
Encode byte strings using the standard Base64 alphabet.
Decode byte strings using the standard Base64 alphabet.
Encode byte strings using a URL-safe alphabet, which substitutes- instead of+ and_ instead of/ in the standard Base64 alphabet. The resultcan still contain=.
Decode byte strings using a URL-safe alphabet, which substitutes- instead of+ and_ instead of/ in the standard Base64 alphabet.
Encode a byte string using Base32.s is the string to encode. The encoded stringis returned.
Decode a Base32 encoded byte string.
s is the byte string to decode. Optionalcasefold is a flag specifyingwhether a lowercase alphabet is acceptable as input. For security purposes,the default isFalse.
RFC 3548 allows for optional mapping of the digit 0 (zero) to the letter O(oh), and for optional mapping of the digit 1 (one) to either the letter I (eye)or letter L (el). The optional argumentmap01 when notNone, specifieswhich letter the digit 1 should be mapped to (whenmap01 is notNone, thedigit 0 is always mapped to the letter O). For security purposes the default isNone, so that 0 and 1 are not allowed in the input.
The decoded byte string is returned. Abinascii.Error is raised ifs isincorrectly padded or if there are non-alphabet characters present in thestring.
Encode a byte string using Base16.
s is the string to encode. The encoded byte string is returned.
Decode a Base16 encoded byte string.
s is the string to decode. Optionalcasefold is a flag specifying whether alowercase alphabet is acceptable as input. For security purposes, the defaultisFalse.
The decoded byte string is returned. ATypeError is raised ifs wereincorrectly padded or if there are non-alphabet characters present in thestring.
The legacy interface:
Decode the contents of the binaryinput file and write the resulting binarydata to theoutput file.input andoutput must befile objects.input will be read untilinput.read() returns an emptybytes object.
Decode the byte strings, which must contain one or more lines of base64encoded data, and return a byte string containing the resulting binary data.decodestring is a deprecated alias.
New in version 3.1.
Encode the contents of the binaryinput file and write the resulting base64encoded data to theoutput file.input andoutput must befileobjects.input will be read untilinput.read() returnsan empty bytes object.encode() returns the encoded data plus a trailingnewline character (b'\n').
Encode the byte strings, which can contain arbitrary binary data, andreturn a byte string containing one or more lines of base64-encoded data.encodebytes() returns a string containing one or more lines ofbase64-encoded data always including an extra trailing newline (b'\n').encodestring is a deprecated alias.
An example usage of the module:
>>>importbase64>>>encoded=base64.b64encode(b'data to be encoded')>>>encodedb'ZGF0YSB0byBiZSBlbmNvZGVk'>>>data=base64.b64decode(encoded)>>>datab'data to be encoded'
See also
19.5.mimetypes — Map filenames to MIME types
19.7.binhex — Encode and decode binhex4 files
Enter search terms or a module, class or function name.