7.1 base64 -- 编码和解码MIME Base64数据 Encode and decode MIME base64 data

This module performs base64 encoding and decoding of arbitrary binary strings into text strings that can be safely sent by email or included as part of an HTTP POST request. The encoding scheme is defined in 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'') and is used for MIME email and various other Internet-related applications; it is not the same as the output produced by the uuencode program. For example, the string 'www.python.org' is encoded as the string 'd3d3LnB5dGhvbi5vcmc=\n'.

这个模块执行任意二进制串到文本串的Base64编码和解码,从而该数据能够被安全的由email传输 或者包含并且作为HTTP POST请求的一部分。编码的模式在 RFC 1521 (MIME(Multipurpose Internet Mail Extensions) 第一部分:Mechanisms for Specifying and Describing the Format of Internet Message Bodies, 5.2节,"Base64 Base64 Content-Transfer-Encoding") 中定义, 并且在MIME邮件以及各种其它Internet相关的应用程序中使用;它不同于 uuencode 程序的输出。 例如,字符串 'www.python.org' 编码为字符串 'd3d3LnB5dGhvbi5vcmc=\n'

decode( input, output)
Decode the contents of the input file and write the resulting binary data to the output file. input and output must either be file objects or objects that mimic the file object interface. input will be read until input.read() returns an empty string.

decode( input, output)
解码 input 文件的内容,输出结果二进制数据到 output 文件。 inputoutput 必须都为文件对象或模拟文件对象接口的对象。input 将一直读取,直至 input.read() 返回一个空串。

decodestring( s)
Decode the string s, which must contain one or more lines of base64 encoded data, and return a string containing the resulting binary data.

decodestring( s)
解码字符串 s,必须包含一行或多行Base64编码数据,返回一个包含结果二进制数据的字符串。

encode( input, output)
Encode the contents of the input file and write the resulting base64 encoded data to the output file. input and output must either be file objects or objects that mimic the file object interface. input will be read until input.read() returns an empty string. encode() returns the encoded data plus a trailing newline character ('\n').

encode( input, output)
编码 input 文件的内容,输出结果Base64编码数据到 output 文件。 inputoutput 必须都为文件对象或模拟文件对象接口的对象。input 将一直读取,直至 input.read() 返回一个空串。encode() 返回编码后的数据外加一个结尾换行符('\n')。

encodestring( s)
Encode the string s, which can contain arbitrary binary data, and return a string containing one or more lines of base64-encoded data. encodestring() returns a string containing one or more lines of base64-encoded data always including an extra trailing newline ('\n').

encodestring( s)
编码字符串 s,能够包含任意的二进制数据,返回一个包含一行或多行Base64编码数据。 encodestring() 返回一个包含一行或多行Base64编码数据的字符串, 而且总是包含一个额外的结尾换行符 ('\n').

See Also:

Module binascii:
Support module containing ASCII-to-binary and binary-to-ASCII conversions.
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.