19.6. base64 — Base16 Base32 Base64 Base85 数据编码

源代码: Lib/base64.py


This module provides functions for encoding binary data to printable ASCII characters and decoding such encodings back to binary data. It provides encoding and decoding functions for the encodings specified in RFC 3548 , which defines the Base16, Base32, and Base64 algorithms, and for the de-facto standard Ascii85 and Base85 encodings.

RFC 3548 encodings are suitable for encoding binary data so that it can safely sent by email, used as parts of URLs, or included as part of an HTTP POST request. The encoding algorithm is not the same as the uuencode program.

There are two interfaces provided by this module. The modern interface supports encoding 像字节对象 到 ASCII bytes ,和解码 像字节对象 or strings containing ASCII to bytes . Both base-64 alphabets defined in RFC 3548 (normal, and URL- and filesystem-safe) are supported.

The legacy interface does not support decoding from strings, but it does provide functions for encoding and decoding to and from 文件对象 . It only supports the Base64 standard alphabet, and it adds newlines every 76 characters as per RFC 2045 . Note that if you are looking for RFC 2045 support you probably want to be looking at the email package instead.

3.3 版改变: 现在,现代接口解码函数可以接受仅 ASCII 的Unicode 字符串。

3.4 版改变: Any 像字节对象 are now accepted by all encoding and decoding functions in this module. Ascii85/Base85 support added.

现代接口提供:

base64. b64encode ( s , altchars=None )

编码 像字节对象 s 使用 Base64,并返回编码 bytes .

可选 altchars 必须是 像字节对象 of at least length 2 (additional characters are ignored) which specifies an alternative alphabet for the + and / characters. This allows an application to e.g. generate URL or filesystem safe Base64 strings. The default is None ,使用标准 Base64 字母。

base64. b64decode ( s , altchars=None , validate=False )

解码 Base64 编码 像字节对象 或 ASCII 字符串 s 并返回解码 bytes .

可选 altchars 必须是 像字节对象 or ASCII string of at least length 2 (additional characters are ignored) which specifies the alternative alphabet used instead of the + and / characters.

A binascii.Error 异常被引发,若 s 被不正确填充。

validate is False (the default), characters that are neither in the normal base-64 alphabet nor the alternative alphabet are discarded prior to the padding check. If validate is True , these non-alphabet characters in the input result in a binascii.Error .

base64. standard_b64encode ( s )

编码 像字节对象 s 使用标准 Base64 字母并返回编码 bytes .

base64. standard_b64decode ( s )

解码 像字节对象 或 ASCII 字符串 s 使用标准 Base64 字母并返回解码 bytes .

base64. urlsafe_b64encode ( s )

编码 像字节对象 s using the URL- and filesystem-safe alphabet, which substitutes - 而不是 + and _ 而不是 / in the standard Base64 alphabet, and return the encoded bytes 。结果仍可包含 = .

base64. urlsafe_b64decode ( s )

解码 像字节对象 或 ASCII 字符串 s using the URL- and filesystem-safe alphabet, which substitutes - 而不是 + and _ 而不是 / in the standard Base64 alphabet, and return the decoded bytes .

base64. b32encode ( s )

编码 像字节对象 s 使用 Base32 并返回编码 bytes .

base64. b32decode ( s , casefold=False , map01=None )

解码 Base32 编码 像字节对象 或 ASCII 字符串 s 并返回解码 bytes .

可选 casefold is a flag specifying whether a lowercase alphabet is acceptable as input. For security purposes, the default is False .

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 argument map01 when not None , specifies which letter the digit 1 should be mapped to (when map01 不是 None , the digit 0 is always mapped to the letter O). For security purposes the default is None , so that 0 and 1 are not allowed in the input.

A binascii.Error is raised if s is incorrectly padded or if there are non-alphabet characters present in the input.

base64. b16encode ( s )

编码 像字节对象 s 使用 Base16 并返回编码 bytes .

base64. b16decode ( s , casefold=False )

解码 Base16 编码 像字节对象 或 ASCII 字符串 s 并返回解码 bytes .

可选 casefold is a flag specifying whether a lowercase alphabet is acceptable as input. For security purposes, the default is False .

A binascii.Error is raised if s is incorrectly padded or if there are non-alphabet characters present in the input.

base64. a85encode ( b , * , foldspaces=False , wrapcol=0 , pad=False , adobe=False )

编码 像字节对象 b 使用 Ascii85 并返回编码 bytes .

foldspaces is an optional flag that uses the special short sequence ‘y’ instead of 4 consecutive spaces (ASCII 0x20) as supported by ‘btoa’. This feature is not supported by the “standard” Ascii85 encoding.

wrapcol controls whether the output should have newline ( b'\n' ) characters added to it. If this is non-zero, each output line will be at most this many characters long.

pad controls whether the input is padded to a multiple of 4 before encoding. Note that the btoa implementation always pads.

adobe controls whether the encoded byte sequence is framed with <~ and ~> , which is used by the Adobe implementation.

3.4 版新增。

base64. a85decode ( b , * , foldspaces=False , adobe=False , ignorechars=b' \t\n\r\v' )

解码 Ascii85 编码 像字节对象 或 ASCII 字符串 b 并返回解码 bytes .

foldspaces is a flag that specifies whether the ‘y’ short sequence should be accepted as shorthand for 4 consecutive spaces (ASCII 0x20). This feature is not supported by the “standard” Ascii85 encoding.

adobe controls whether the input sequence is in Adobe Ascii85 format (i.e. is framed with <~ and ~>).

ignorechars should be a 像字节对象 or ASCII string containing characters to ignore from the input. This should only contain whitespace characters, and by default contains all whitespace characters in ASCII.

3.4 版新增。

base64. b85encode ( b , pad=False )

编码 像字节对象 b using base85 (as used in e.g. git-style binary diffs) and return the encoded bytes .

pad 为 True,填充输入采用 b'\0' so its length is a multiple of 4 bytes before encoding.

3.4 版新增。

base64. b85decode ( b )

解码 base85 编码 像字节对象 或 ASCII 字符串 b 并返回解码 bytes . Padding is implicitly removed, if necessary.

3.4 版新增。

传统接口:

base64. decode ( input , output )

Decode the contents of the binary input file and write the resulting binary data to the output 文件。 input and output 必须是 文件对象 . input will be read until input.readline() returns an empty bytes object.

base64. decodebytes ( s )

解码 像字节对象 s , which must contain one or more lines of base64 encoded data, and return the decoded bytes .

3.1 版新增。

base64. decodestring ( s )

弃用 decodebytes() .

从 3.1 版起弃用:

base64. encode ( input , output )

Encode the contents of the binary input file and write the resulting base64 encoded data to the output 文件。 input and output 必须是 文件对象 . input will be read until input.read() returns an empty bytes object. encode() inserts a newline character ( b'\n' ) after every 76 bytes of the output, as well as ensuring that the output always ends with a newline, as per RFC 2045 (MIME).

base64. encodebytes ( s )

编码 像字节对象 s , which can contain arbitrary binary data, and return bytes containing the base64-encoded data, with newlines ( b'\n' ) inserted after every 76 bytes of output, and ensuring that there is a trailing newline, as per RFC 2045 (MIME).

3.1 版新增。

base64. encodestring ( s )

弃用 encodebytes() .

从 3.1 版起弃用:

模块的用法范例:

>>> import base64
>>> encoded = base64.b64encode(b'data to be encoded')
>>> encoded
b'ZGF0YSB0byBiZSBlbmNvZGVk'
>>> data = base64.b64decode(encoded)
>>> data
b'data to be encoded'
							

另请参阅

模块 binascii
支持模块包含 ASCII-to-binary 和 binary-to-ASCII 转换。
RFC 1521 - MIME (Multipurpose Internet Mail Extensions) Part One: Mechanisms for Specifying and Describing the Format of Internet Message Bodies
Section 5.2, “Base64 Content-Transfer-Encoding,” provides the definition of the base64 encoding.

上一话题

19.5. mimetypes — 把文件名映射到 MIME 类型

下一话题

19.7. binhex — 编码和解码 binhex4 文件

本页