“Python/docs/3.9/library/base64”的版本间差异

来自菜鸟教程
Python/docs/3.9/library/base64
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
 +
{{DISPLAYTITLE:base64 — Base16、Base32、Base64、Base85 数据编码 — Python 文档}}
 
<div id="module-base64" class="section">
 
<div id="module-base64" class="section">
  
 
<span id="base64-base16-base32-base64-base85-data-encodings"></span>
 
<span id="base64-base16-base32-base64-base85-data-encodings"></span>
= [[#module-base64|<code>base64</code>]] --- Base16, Base32, Base64, Base85 Data Encodings =
+
= base64 — Base16、Base32、Base64、Base85 数据编码 =
  
'''Source code:''' [https://github.com/python/cpython/tree/3.9/Lib/base64.py Lib/base64.py]
+
'''源代码:''' [[#id1|<span id="id2" class="problematic">:source:`Lib/base64.py`</span>]]
  
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
 
<span id="index-1" class="target"></span>[https://tools.ietf.org/html/rfc3548.html '''RFC 3548'''], which defines the Base16, Base32, and Base64 algorithms,
 
and for the de-facto standard Ascii85 and Base85 encodings.
 
  
The <span id="index-2" class="target"></span>[https://tools.ietf.org/html/rfc3548.html '''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
+
该模块提供将二进制数据编码为可打印 ASCII 字符并将此类编码解码回二进制数据的函数。 它为定义 Base16、Base32 和 Base64 算法的 <span id="index-1" class="target"></span>[https://tools.ietf.org/html/rfc3548.html RFC 3548] 中指定的编码以及事实上的标准 Ascii85 和 Base85 编码提供编码和解码功能。
supports encoding [[../../glossary#term-bytes-like-object|<span class="xref std std-term">bytes-like objects</span>]] to ASCII
 
[[../stdtypes#bytes|<code>bytes</code>]], and decoding [[../../glossary#term-bytes-like-object|<span class="xref std std-term">bytes-like objects</span>]] or
 
strings containing ASCII to [[../stdtypes#bytes|<code>bytes</code>]]. Both base-64 alphabets
 
defined in <span id="index-3" class="target"></span>[https://tools.ietf.org/html/rfc3548.html '''RFC 3548'''] (normal, and URL- and filesystem-safe) are supported.
 
  
The legacy interface does not support decoding from strings, but it does
+
<span id="index-2" class="target"></span>[https://tools.ietf.org/html/rfc3548.html RFC 3548] 编码适用于对二进制数据进行编码,以便可以安全地通过电子邮件发送、用作 URL 的一部分或作为 HTTP POST 请求的一部分包含在内。 编码算法与'''uuencode'''程序不同。
provide functions for encoding and decoding to and from [[../../glossary#term-file-object|<span class="xref std std-term">file objects</span>]]. It only supports the Base64 standard alphabet, and it adds
+
 
newlines every 76 characters as per <span id="index-4" class="target"></span>[https://tools.ietf.org/html/rfc2045.html '''RFC 2045''']. Note that if you are looking
+
该模块提供了两个接口。 现代接口支持将 [[../../glossary#term-bytes-like-object|bytes-like objects]] 编码为 ASCII [[../stdtypes#bytes|bytes]],并将 [[../../glossary#term-bytes-like-object|bytes-like objects]] 或包含 ASCII 的字符串解码为 [[../stdtypes#bytes|bytes]]。 支持 [X34X][https://tools.ietf.org/html/rfc3548.html RFC 3548](正常,以及 URL 和文件系统安全)中定义的两种 base-64 字母表。
for <span id="index-5" class="target"></span>[https://tools.ietf.org/html/rfc2045.html '''RFC 2045'''] support you probably want to be looking at the [[../email#module-email|<code>email</code>]]
+
 
package instead.
+
遗留接口不支持从字符串解码,但它确实提供了对 [[../../glossary#term-file-object|文件对象]] 进行编码和解码的功能。 它仅支持 Base64 标准字母表,并根据 <span id="index-4" class="target"></span>[https://tools.ietf.org/html/rfc2045.html RFC 2045] 每 76 个字符添加换行符。 请注意,如果您正在寻找 <span id="index-5" class="target"></span>[https://tools.ietf.org/html/rfc2045.html RFC 2045] 支持,您可能希望查看 [[../email#module-email|email]] 包。
  
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<span class="versionmodified changed">3.3 版更改: </span>ASCII-only Unicode strings are now accepted by the decoding functions of
+
<span class="versionmodified changed">3.3 版更改:</span>现代界面的解码功能现在接受仅 ASCII Unicode 字符串。
the modern interface.
 
  
  
第38行: 第26行:
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<span class="versionmodified changed">在 3.4 版更改: </span>Any [[../../glossary#term-bytes-like-object|<span class="xref std std-term">bytes-like objects</span>]] are now accepted by all
+
<span class="versionmodified changed">在 3.4 版更改:</span>任何 [[../../glossary#term-bytes-like-object|bytes-like objects]] 现在被此模块中的所有编码和解码函数接受。 添加了 Ascii85/Base85 支持。
encoding and decoding functions in this module. Ascii85/Base85 support added.
 
  
  
 
</div>
 
</div>
The modern interface provides:
+
现代界面提供:
  
 
<dl>
 
<dl>
<dt><code>base64.</code><code>b64encode</code><span class="sig-paren">(</span>''<span class="n">s</span>'', ''<span class="n">altchars</span><span class="o">=</span><span class="default_value">None</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">b64encode</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">s</span></span>'', ''<span class="n"><span class="pre">altchars</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Encode the [[../../glossary#term-bytes-like-object|<span class="xref std std-term">bytes-like object</span>]] ''s'' using Base64 and return the encoded
+
<dd><p>使用 Base64 编码 [[../../glossary#term-bytes-like-object|bytes-like object]] ''s'' 并返回编码的 [[../stdtypes#bytes|bytes]]</p>
[[../stdtypes#bytes|<code>bytes</code>]].</p>
+
<p>可选的 ''altchars'' 必须是一个 [[../../glossary#term-bytes-like-object|bytes-like object]],长度至少为 2(附加字符被忽略),它指定了 <code>+</code> 和 [ X187X] 字符。 这允许应用程序例如 生成 URL 或文件系统安全的 Base64 字符串。 默认值为 <code>None</code>,使用标准 Base64 字母表。</p></dd></dl>
<p>Optional ''altchars'' must be a [[../../glossary#term-bytes-like-object|<span class="xref std std-term">bytes-like object</span>]] of at least
 
length 2 (additional characters are ignored) which specifies an alternative
 
alphabet for the <code>+</code> and <code>/</code> characters. This allows an application to e.g.
 
generate URL or filesystem safe Base64 strings. The default is <code>None</code>, for
 
which the standard Base64 alphabet is used.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt><code>base64.</code><code>b64decode</code><span class="sig-paren">(</span>''<span class="n">s</span>'', ''<span class="n">altchars</span><span class="o">=</span><span class="default_value">None</span>'', ''<span class="n">validate</span><span class="o">=</span><span class="default_value">False</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">b64decode</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">s</span></span>'', ''<span class="n"><span class="pre">altchars</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>'', ''<span class="n"><span class="pre">validate</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Decode the Base64 encoded [[../../glossary#term-bytes-like-object|<span class="xref std std-term">bytes-like object</span>]] or ASCII string
+
<dd><p>解码 Base64 编码的 [[../../glossary#term-bytes-like-object|bytes-like object]] ASCII 字符串 ''s'' 并返回解码的 [[../stdtypes#bytes|bytes]]</p>
''s'' and return the decoded [[../stdtypes#bytes|<code>bytes</code>]].</p>
+
<p>可选的 ''altchars'' 必须是一个 [[../../glossary#term-bytes-like-object|bytes-like object]] 或至少长度为 2 的 ASCII 字符串(附加字符被忽略),它指定使用替代字母表代替 <code>+</code><code>/</code> 字符。</p>
<p>Optional ''altchars'' must be a [[../../glossary#term-bytes-like-object|<span class="xref std std-term">bytes-like object</span>]] or ASCII string of
+
<p>如果 ''s'' 填充不正确,则会引发 [[../binascii#binascii|binascii.Error]] 异常。</p>
at least length 2 (additional characters are ignored) which specifies the
+
<p>如果 ''validate'' <code>False</code>(默认值),则在填充检查之前丢弃既不在正常 base-64 字母表中也不在替代字母表中的字符。 如果 ''validate'' <code>True</code>,则输入中的这些非字母字符会导致 [[../binascii#binascii|binascii.Error]]</p></dd></dl>
alternative alphabet used instead of the <code>+</code> and <code>/</code> characters.</p>
 
<p>A [[../binascii#binascii|<code>binascii.Error</code>]] exception is raised
 
if ''s'' is incorrectly padded.</p>
 
<p>If ''validate'' is <code>False</code> (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 <code>True</code>,
 
these non-alphabet characters in the input result in a
 
[[../binascii#binascii|<code>binascii.Error</code>]].</p></dd></dl>
 
  
; <code>base64.</code><code>standard_b64encode</code><span class="sig-paren">(</span>''<span class="n">s</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">standard_b64encode</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">s</span></span>''<span class="sig-paren">)</span>
: Encode [[../../glossary#term-bytes-like-object|<span class="xref std std-term">bytes-like object</span>]] ''s'' using the standard Base64 alphabet and return the encoded [[../stdtypes#bytes|<code>bytes</code>]].
+
: 使用标准 Base64 字母表对 [[../../glossary#term-bytes-like-object|bytes-like object]] ''s'' 进行编码,并返回编码后的 [[../stdtypes#bytes|bytes]]
  
; <code>base64.</code><code>standard_b64decode</code><span class="sig-paren">(</span>''<span class="n">s</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">standard_b64decode</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">s</span></span>''<span class="sig-paren">)</span>
: Decode [[../../glossary#term-bytes-like-object|<span class="xref std std-term">bytes-like object</span>]] or ASCII string ''s'' using the standard Base64 alphabet and return the decoded [[../stdtypes#bytes|<code>bytes</code>]].
+
: 使用标准 Base64 字母表解码 [[../../glossary#term-bytes-like-object|bytes-like object]] ASCII 字符串 ''s'' 并返回解码后的 [[../stdtypes#bytes|bytes]]
  
; <code>base64.</code><code>urlsafe_b64encode</code><span class="sig-paren">(</span>''<span class="n">s</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">urlsafe_b64encode</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">s</span></span>''<span class="sig-paren">)</span>
: Encode [[../../glossary#term-bytes-like-object|<span class="xref std std-term">bytes-like object</span>]] ''s'' using the URL- and filesystem-safe alphabet, which substitutes <code>-</code> instead of <code>+</code> and <code>_</code> instead of <code>/</code> in the standard Base64 alphabet, and return the encoded [[../stdtypes#bytes|<code>bytes</code>]]. The result can still contain <code>=</code>.
+
: 使用 URL 和文件系统安全字母表编码 [[../../glossary#term-bytes-like-object|bytes-like object]] ''s'',用 <code>-</code> 代替 <code>+</code> <code>_</code> ] 而不是标准 Base64 字母表中的 <code>/</code>,并返回编码的 [[../stdtypes#bytes|字节]] 。 结果仍然可以包含 <code>=</code>
  
; <code>base64.</code><code>urlsafe_b64decode</code><span class="sig-paren">(</span>''<span class="n">s</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">urlsafe_b64decode</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">s</span></span>''<span class="sig-paren">)</span>
: Decode [[../../glossary#term-bytes-like-object|<span class="xref std std-term">bytes-like object</span>]] or ASCII string ''s'' using the URL- and filesystem-safe alphabet, which substitutes <code>-</code> instead of <code>+</code> and <code>_</code> instead of <code>/</code> in the standard Base64 alphabet, and return the decoded [[../stdtypes#bytes|<code>bytes</code>]].
+
: 使用 URL 和文件系统安全字母表解码 [[../../glossary#term-bytes-like-object|bytes-like object]] ASCII 字符串 ''s'',用 <code>-</code> 代替 <code>+</code> <code>_</code> 代替标准 Base64 字母表中的 <code>/</code>,并返回解码后的 [[../stdtypes#bytes|bytes]]
  
; <code>base64.</code><code>b32encode</code><span class="sig-paren">(</span>''<span class="n">s</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">b32encode</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">s</span></span>''<span class="sig-paren">)</span>
: Encode the [[../../glossary#term-bytes-like-object|<span class="xref std std-term">bytes-like object</span>]] ''s'' using Base32 and return the encoded [[../stdtypes#bytes|<code>bytes</code>]].
+
: 使用 Base32 对 [[../../glossary#term-bytes-like-object|bytes-like object]] ''s'' 进行编码,并返回编码后的 [[../stdtypes#bytes|bytes]]
  
 
<dl>
 
<dl>
<dt><code>base64.</code><code>b32decode</code><span class="sig-paren">(</span>''<span class="n">s</span>'', ''<span class="n">casefold</span><span class="o">=</span><span class="default_value">False</span>'', ''<span class="n">map01</span><span class="o">=</span><span class="default_value">None</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">b32decode</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">s</span></span>'', ''<span class="n"><span class="pre">casefold</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span>'', ''<span class="n"><span class="pre">map01</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Decode the Base32 encoded [[../../glossary#term-bytes-like-object|<span class="xref std std-term">bytes-like object</span>]] or ASCII string ''s'' and
+
<dd><p>解码 Base32 编码的 [[../../glossary#term-bytes-like-object|bytes-like object]] ASCII 字符串 ''s'' 并返回解码的 [[../stdtypes#bytes|bytes]]</p>
return the decoded [[../stdtypes#bytes|<code>bytes</code>]].</p>
+
<p>可选的 ''casefold'' 是一个标志,用于指定是否可以接受小写字母作为输入。 出于安全考虑,默认值为 <code>False</code></p>
<p>Optional ''casefold'' is a flag specifying
+
<p><span id="index-6" class="target"></span>[https://tools.ietf.org/html/rfc3548.html RFC 3548] 允许将数字 0(零)可选映射到字母 O(哦),以及将数字 1(一)可选映射到字母 I( eye) 或字母 L (el)。 可选参数 ''map01'' 当不是 <code>None</code> 时,指定数字 1 应该映射到哪个字母(当 ''map01'' 不是 <code>None</code> 时,数字 0总是映射到字母 O)。 出于安全考虑,默认值为 <code>None</code>,因此输入中不允许使用 0 和 1。</p>
whether a lowercase alphabet is acceptable as input. For security purposes,
+
<p>如果 ''s'' 填充不正确或输入中存在非字母字符,则会引发 [[../binascii#binascii|binascii.Error]]</p></dd></dl>
the default is <code>False</code>.</p>
 
<p><span id="index-6" class="target"></span>[https://tools.ietf.org/html/rfc3548.html '''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 <code>None</code>, specifies
 
which letter the digit 1 should be mapped to (when ''map01'' is not <code>None</code>, the
 
digit 0 is always mapped to the letter O). For security purposes the default is
 
<code>None</code>, so that 0 and 1 are not allowed in the input.</p>
 
<p>A [[../binascii#binascii|<code>binascii.Error</code>]] is raised if ''s'' is
 
incorrectly padded or if there are non-alphabet characters present in the
 
input.</p></dd></dl>
 
  
; <code>base64.</code><code>b16encode</code><span class="sig-paren">(</span>''<span class="n">s</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">b16encode</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">s</span></span>''<span class="sig-paren">)</span>
: Encode the [[../../glossary#term-bytes-like-object|<span class="xref std std-term">bytes-like object</span>]] ''s'' using Base16 and return the encoded [[../stdtypes#bytes|<code>bytes</code>]].
+
: 使用 Base16 对 [[../../glossary#term-bytes-like-object|bytes-like object]] ''s'' 进行编码,并返回编码后的 [[../stdtypes#bytes|bytes]]
  
 
<dl>
 
<dl>
<dt><code>base64.</code><code>b16decode</code><span class="sig-paren">(</span>''<span class="n">s</span>'', ''<span class="n">casefold</span><span class="o">=</span><span class="default_value">False</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">b16decode</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">s</span></span>'', ''<span class="n"><span class="pre">casefold</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Decode the Base16 encoded [[../../glossary#term-bytes-like-object|<span class="xref std std-term">bytes-like object</span>]] or ASCII string ''s'' and
+
<dd><p>解码 Base16 编码的 [[../../glossary#term-bytes-like-object|bytes-like object]] ASCII 字符串 ''s'' 并返回解码的 [[../stdtypes#bytes|bytes]]</p>
return the decoded [[../stdtypes#bytes|<code>bytes</code>]].</p>
+
<p>可选的 ''casefold'' 是一个标志,用于指定是否可以接受小写字母作为输入。 出于安全考虑,默认值为 <code>False</code></p>
<p>Optional ''casefold'' is a flag specifying whether a
+
<p>如果 ''s'' 填充不正确或输入中存在非字母字符,则会引发 [[../binascii#binascii|binascii.Error]]</p></dd></dl>
lowercase alphabet is acceptable as input. For security purposes, the default
 
is <code>False</code>.</p>
 
<p>A [[../binascii#binascii|<code>binascii.Error</code>]] is raised if ''s'' is
 
incorrectly padded or if there are non-alphabet characters present in the
 
input.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt><code>base64.</code><code>a85encode</code><span class="sig-paren">(</span>''<span class="n">b</span>'', ''<span class="o">*</span>'', ''<span class="n">foldspaces</span><span class="o">=</span><span class="default_value">False</span>'', ''<span class="n">wrapcol</span><span class="o">=</span><span class="default_value">0</span>'', ''<span class="n">pad</span><span class="o">=</span><span class="default_value">False</span>'', ''<span class="n">adobe</span><span class="o">=</span><span class="default_value">False</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">a85encode</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">b</span></span>'', ''<span class="o"><span class="pre">*</span></span>'', ''<span class="n"><span class="pre">foldspaces</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span>'', ''<span class="n"><span class="pre">wrapcol</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span>'', ''<span class="n"><span class="pre">pad</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span>'', ''<span class="n"><span class="pre">adobe</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Encode the [[../../glossary#term-bytes-like-object|<span class="xref std std-term">bytes-like object</span>]] ''b'' using Ascii85 and return the
+
<dd><p>使用 Ascii85 对 [[../../glossary#term-bytes-like-object|bytes-like object]] ''b'' 进行编码,并返回编码后的 [[../stdtypes#bytes|bytes]]</p>
encoded [[../stdtypes#bytes|<code>bytes</code>]].</p>
+
<p>''foldspaces'' 是一个可选标志,它使用特殊的短序列 'y' 而不是 'btoa' 支持的 4 个连续空格(ASCII 0x20)。 “标准” Ascii85 编码不支持此功能。</p>
<p>''foldspaces'' is an optional flag that uses the special short sequence 'y'
+
<p>''wrapcol'' 控制输出是否应添加换行符 (<code>b'\n'</code>) 字符。 如果这是非零值,则每个输出行最多只有这么多字符长。</p>
instead of 4 consecutive spaces (ASCII 0x20) as supported by 'btoa'. This
+
<p>''pad'' 控制输入是否在编码前填充为 4 的倍数。 请注意,<code>btoa</code> 实现始终填充。</p>
feature is not supported by the &quot;standard&quot; Ascii85 encoding.</p>
+
<p>''adobe'' 控制编码的字节序列是否使用 <code>&lt;~</code> <code>~&gt;</code> 进行框架化,Adobe 实现使用了它们。</p>
<p>''wrapcol'' controls whether the output should have newline (<code>b'\n'</code>)
 
characters added to it. If this is non-zero, each output line will be
 
at most this many characters long.</p>
 
<p>''pad'' controls whether the input is padded to a multiple of 4
 
before encoding. Note that the <code>btoa</code> implementation always pads.</p>
 
<p>''adobe'' controls whether the encoded byte sequence is framed with <code>&lt;~</code>
 
and <code>~&gt;</code>, which is used by the Adobe implementation.</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.4 新版功能.</span></p>
+
<p><span class="versionmodified added">3.4 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt><code>base64.</code><code>a85decode</code><span class="sig-paren">(</span>''<span class="n">b</span>'', ''<span class="o">*</span>'', ''<span class="n">foldspaces</span><span class="o">=</span><span class="default_value">False</span>'', ''<span class="n">adobe</span><span class="o">=</span><span class="default_value">False</span>'', ''<span class="n">ignorechars</span><span class="o">=</span><span class="default_value">b' \\t\\n\\r\\v'</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">a85decode</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">b</span></span>'', ''<span class="o"><span class="pre">*</span></span>'', ''<span class="n"><span class="pre">foldspaces</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span>'', ''<span class="n"><span class="pre">adobe</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span>'', ''<span class="n"><span class="pre">ignorechars</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">b'</span> <span class="pre">\\t\\n\\r\\v'</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Decode the Ascii85 encoded [[../../glossary#term-bytes-like-object|<span class="xref std std-term">bytes-like object</span>]] or ASCII string ''b'' and
+
<dd><p>解码 Ascii85 编码的 [[../../glossary#term-bytes-like-object|bytes-like object]] ASCII 字符串 ''b'' 并返回解码的 [[../stdtypes#bytes|bytes]]</p>
return the decoded [[../stdtypes#bytes|<code>bytes</code>]].</p>
+
<p>''foldspaces'' 是一个标志,用于指定是否应接受 'y' 短序列作为 4 个连续空格 (ASCII 0x20) 的简写。 “标准” Ascii85 编码不支持此功能。</p>
<p>''foldspaces'' is a flag that specifies whether the 'y' short sequence
+
<p>''adobe'' 控制输入序列是否为 Adobe Ascii85 格式(即 用 框起来)。</p>
should be accepted as shorthand for 4 consecutive spaces (ASCII 0x20).
+
<p>''ignorechars'' 应该是一个 [[../../glossary#term-bytes-like-object|bytes-like object]] 或包含要从输入中忽略的字符的 ASCII 字符串。 这应该只包含空白字符,默认情况下包含 ASCII 中的所有空白字符。</p>
This feature is not supported by the &quot;standard&quot; Ascii85 encoding.</p>
 
<p>''adobe'' controls whether the input sequence is in Adobe Ascii85 format
 
(i.e. is framed with &lt;~ and ~&gt;).</p>
 
<p>''ignorechars'' should be a [[../../glossary#term-bytes-like-object|<span class="xref std std-term">bytes-like object</span>]] 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.</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.4 新版功能.</span></p>
+
<p><span class="versionmodified added">3.4 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt><code>base64.</code><code>b85encode</code><span class="sig-paren">(</span>''<span class="n">b</span>'', ''<span class="n">pad</span><span class="o">=</span><span class="default_value">False</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">b85encode</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">b</span></span>'', ''<span class="n"><span class="pre">pad</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Encode the [[../../glossary#term-bytes-like-object|<span class="xref std std-term">bytes-like object</span>]] ''b'' using base85 (as used in e.g.
+
<dd><p>使用 base85 对 [[../../glossary#term-bytes-like-object|bytes-like object]] ''b'' 进行编码(例如 git 样式的二进制差异)并返回编码的 [[../stdtypes#bytes|字节]] </p>
git-style binary diffs) and return the encoded [[../stdtypes#bytes|<code>bytes</code>]].</p>
+
<p>如果 ''pad'' 为真,则输入用 <code>b'\0'</code> 填充,因此其长度是编码前 4 字节的倍数。</p>
<p>If ''pad'' is true, the input is padded with <code>b'\0'</code> so its length is a
 
multiple of 4 bytes before encoding.</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.4 新版功能.</span></p>
+
<p><span class="versionmodified added">3.4 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt><code>base64.</code><code>b85decode</code><span class="sig-paren">(</span>''<span class="n">b</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">b85decode</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">b</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Decode the base85-encoded [[../../glossary#term-bytes-like-object|<span class="xref std std-term">bytes-like object</span>]] or ASCII string ''b'' and
+
<dd><p>解码 base85 编码的 [[../../glossary#term-bytes-like-object|bytes-like object]] ASCII 字符串 ''b'' 并返回解码后的 [[../stdtypes#bytes|bytes]]。 如有必要,将隐式删除填充。</p>
return the decoded [[../stdtypes#bytes|<code>bytes</code>]]. Padding is implicitly removed, if
 
necessary.</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.4 新版功能.</span></p>
+
<p><span class="versionmodified added">3.4 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
The legacy interface:
+
遗留接口:
  
; <code>base64.</code><code>decode</code><span class="sig-paren">(</span>''<span class="n">input</span>'', ''<span class="n">output</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">decode</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">input</span></span>'', ''<span class="n"><span class="pre">output</span></span>''<span class="sig-paren">)</span>
: Decode the contents of the binary ''input'' file and write the resulting binary data to the ''output'' file. ''input'' and ''output'' must be [[../../glossary#term-file-object|<span class="xref std std-term">file objects</span>]]. ''input'' will be read until <code>input.readline()</code> returns an empty bytes object.
+
: 对二进制 ''input'' 文件的内容进行解码,并将生成的二进制数据写入 ''output'' 文件。 ''input'' ''output'' 必须是 [[../../glossary#term-file-object|文件对象]] ''input'' 将被读取,直到 <code>input.readline()</code> 返回一个空字节对象。
  
 
<dl>
 
<dl>
<dt><code>base64.</code><code>decodebytes</code><span class="sig-paren">(</span>''<span class="n">s</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">decodebytes</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">s</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Decode the [[../../glossary#term-bytes-like-object|<span class="xref std std-term">bytes-like object</span>]] ''s'', which must contain one or more
+
<dd><p>解码[[../../glossary#term-bytes-like-object|bytes-like object]] ''s'',其中必须包含一行或多行base64编码数据,并返回解码后的[[../stdtypes#bytes|bytes]]</p>
lines of base64 encoded data, and return the decoded [[../stdtypes#bytes|<code>bytes</code>]].</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.1 新版功能.</span></p>
+
<p><span class="versionmodified added">3.1 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
; <code>base64.</code><code>encode</code><span class="sig-paren">(</span>''<span class="n">input</span>'', ''<span class="n">output</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">encode</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">input</span></span>'', ''<span class="n"><span class="pre">output</span></span>''<span class="sig-paren">)</span>
: Encode the contents of the binary ''input'' file and write the resulting base64 encoded data to the ''output'' file. ''input'' and ''output'' must be [[../../glossary#term-file-object|<span class="xref std std-term">file objects</span>]]. ''input'' will be read until <code>input.read()</code> returns an empty bytes object. [[#base64.encode|<code>encode()</code>]] inserts a newline character (<code>b'\n'</code>) after every 76 bytes of the output, as well as ensuring that the output always ends with a newline, as per <span id="index-7" class="target"></span>[https://tools.ietf.org/html/rfc2045.html '''RFC 2045'''] (MIME).
+
: 对二进制 ''input'' 文件的内容进行编码,并将生成的 base64 编码数据写入 ''output'' 文件。 ''input'' ''output'' 必须是 [[../../glossary#term-file-object|文件对象]] ''input'' 将被读取,直到 <code>input.read()</code> 返回一个空字节对象。 [[#base64.encode|encode()]] 在输出的每 76 个字节后插入一个换行符 (<code>b'\n'</code>),并确保输出总是以换行符结尾,如 <span id="index-7" class="target"></span> [https://tools.ietf.org/html/rfc2045.html RFC 2045] (MIME)
  
 
<dl>
 
<dl>
<dt><code>base64.</code><code>encodebytes</code><span class="sig-paren">(</span>''<span class="n">s</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">encodebytes</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">s</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Encode the [[../../glossary#term-bytes-like-object|<span class="xref std std-term">bytes-like object</span>]] ''s'', which can contain arbitrary binary
+
<dd><p>[[../../glossary#term-bytes-like-object|bytes-like object]] ''s'' 进行编码,其中可以包含任意二进制数据,并返回包含 base64 编码数据的 [[../stdtypes#bytes|bytes]],使用换行符 ([ X172X]) 在每 76 个输出字节后插入,并确保有一个尾随换行符,根据 <span id="index-8" class="target"></span>[https://tools.ietf.org/html/rfc2045.html RFC 2045] (MIME)</p>
data, and return [[../stdtypes#bytes|<code>bytes</code>]] containing the base64-encoded data, with newlines
 
(<code>b'\n'</code>) inserted after every 76 bytes of output, and ensuring that
 
there is a trailing newline, as per <span id="index-8" class="target"></span>[https://tools.ietf.org/html/rfc2045.html '''RFC 2045'''] (MIME).</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.1 新版功能.</span></p>
+
<p><span class="versionmodified added">3.1 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
An example usage of the module:
+
该模块的示例用法:
  
 
<div class="doctest highlight-default notranslate">
 
<div class="doctest highlight-default notranslate">
第214行: 第151行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>&gt;&gt;&gt; import base64
+
<syntaxhighlight lang="python">>>> import base64
&gt;&gt;&gt; encoded = base64.b64encode(b'data to be encoded')
+
>>> encoded = base64.b64encode(b'data to be encoded')
&gt;&gt;&gt; encoded
+
>>> encoded
 
b'ZGF0YSB0byBiZSBlbmNvZGVk'
 
b'ZGF0YSB0byBiZSBlbmNvZGVk'
&gt;&gt;&gt; data = base64.b64decode(encoded)
+
>>> data = base64.b64decode(encoded)
&gt;&gt;&gt; data
+
>>> data
b'data to be encoded'</pre>
+
b'data to be encoded'</syntaxhighlight>
  
 
</div>
 
</div>
第227行: 第164行:
 
<div class="admonition seealso">
 
<div class="admonition seealso">
  
参见
+
也可以看看
 +
 
 +
; 模块 [[../binascii#module-binascii|binascii]]
 +
: 包含 ASCII 到二进制和二进制到 ASCII 转换的支持模块。
 +
; <span id="index-9" class="target"></span>[https://tools.ietf.org/html/rfc1521.html RFC 1521] - MIME(多用途互联网邮件扩展)第一部分:指定和描述互联网消息体格式的机制
 +
: 第 5.2 节“Base64 内容传输编码”提供了 base64 编码的定义。
  
; Module [[../binascii#module-binascii|<code>binascii</code>]]
 
: Support module containing ASCII-to-binary and binary-to-ASCII conversions.
 
; <span id="index-9" class="target"></span>[https://tools.ietf.org/html/rfc1521.html '''RFC 1521'''] - MIME (Multipurpose Internet Mail Extensions) Part One: Mechanisms for Specifying and Describing the Format of Internet Message Bodies
 
: Section 5.2, &quot;Base64 Content-Transfer-Encoding,&quot; provides the definition of the base64 encoding.
 
  
 +
</div>
  
 
</div>
 
</div>
 +
<div class="clearer">
 +
 +
  
 
</div>
 
</div>
  
[[Category:Python 3.9 中文文档]]
+
[[Category:Python 3.9 文档]]

2021年10月31日 (日) 04:51的最新版本

base64 — Base16、Base32、Base64、Base85 数据编码

源代码: :source:`Lib/base64.py`



该模块提供将二进制数据编码为可打印 ASCII 字符并将此类编码解码回二进制数据的函数。 它为定义 Base16、Base32 和 Base64 算法的 RFC 3548 中指定的编码以及事实上的标准 Ascii85 和 Base85 编码提供编码和解码功能。

RFC 3548 编码适用于对二进制数据进行编码,以便可以安全地通过电子邮件发送、用作 URL 的一部分或作为 HTTP POST 请求的一部分包含在内。 编码算法与uuencode程序不同。

该模块提供了两个接口。 现代接口支持将 bytes-like objects 编码为 ASCII bytes,并将 bytes-like objects 或包含 ASCII 的字符串解码为 bytes。 支持 [X34X]RFC 3548(正常,以及 URL 和文件系统安全)中定义的两种 base-64 字母表。

遗留接口不支持从字符串解码,但它确实提供了对 文件对象 进行编码和解码的功能。 它仅支持 Base64 标准字母表,并根据 RFC 2045 每 76 个字符添加换行符。 请注意,如果您正在寻找 RFC 2045 支持,您可能希望查看 email 包。

3.3 版更改:现代界面的解码功能现在接受仅 ASCII 的 Unicode 字符串。


在 3.4 版更改:任何 bytes-like objects 现在被此模块中的所有编码和解码函数接受。 添加了 Ascii85/Base85 支持。


现代界面提供:

base64.b64encode(s, altchars=None)

使用 Base64 编码 bytes-like object s 并返回编码的 bytes

可选的 altchars 必须是一个 bytes-like object,长度至少为 2(附加字符被忽略),它指定了 + 和 [ X187X] 字符。 这允许应用程序例如 生成 URL 或文件系统安全的 Base64 字符串。 默认值为 None,使用标准 Base64 字母表。

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

解码 Base64 编码的 bytes-like object 或 ASCII 字符串 s 并返回解码的 bytes

可选的 altchars 必须是一个 bytes-like object 或至少长度为 2 的 ASCII 字符串(附加字符被忽略),它指定使用替代字母表代替 +/ 字符。

如果 s 填充不正确,则会引发 binascii.Error 异常。

如果 validateFalse(默认值),则在填充检查之前丢弃既不在正常 base-64 字母表中也不在替代字母表中的字符。 如果 validateTrue,则输入中的这些非字母字符会导致 binascii.Error

base64.standard_b64encode(s)
使用标准 Base64 字母表对 bytes-like object s 进行编码,并返回编码后的 bytes
base64.standard_b64decode(s)
使用标准 Base64 字母表解码 bytes-like object 或 ASCII 字符串 s 并返回解码后的 bytes
base64.urlsafe_b64encode(s)
使用 URL 和文件系统安全字母表编码 bytes-like object s,用 - 代替 +_ ] 而不是标准 Base64 字母表中的 /,并返回编码的 字节 。 结果仍然可以包含 =
base64.urlsafe_b64decode(s)
使用 URL 和文件系统安全字母表解码 bytes-like object 或 ASCII 字符串 s,用 - 代替 +_ 代替标准 Base64 字母表中的 /,并返回解码后的 bytes
base64.b32encode(s)
使用 Base32 对 bytes-like object s 进行编码,并返回编码后的 bytes
base64.b32decode(s, casefold=False, map01=None)

解码 Base32 编码的 bytes-like object 或 ASCII 字符串 s 并返回解码的 bytes

可选的 casefold 是一个标志,用于指定是否可以接受小写字母作为输入。 出于安全考虑,默认值为 False

RFC 3548 允许将数字 0(零)可选映射到字母 O(哦),以及将数字 1(一)可选映射到字母 I( eye) 或字母 L (el)。 可选参数 map01 当不是 None 时,指定数字 1 应该映射到哪个字母(当 map01 不是 None 时,数字 0总是映射到字母 O)。 出于安全考虑,默认值为 None,因此输入中不允许使用 0 和 1。

如果 s 填充不正确或输入中存在非字母字符,则会引发 binascii.Error

base64.b16encode(s)
使用 Base16 对 bytes-like object s 进行编码,并返回编码后的 bytes
base64.b16decode(s, casefold=False)

解码 Base16 编码的 bytes-like object 或 ASCII 字符串 s 并返回解码的 bytes

可选的 casefold 是一个标志,用于指定是否可以接受小写字母作为输入。 出于安全考虑,默认值为 False

如果 s 填充不正确或输入中存在非字母字符,则会引发 binascii.Error

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

使用 Ascii85 对 bytes-like object b 进行编码,并返回编码后的 bytes

foldspaces 是一个可选标志,它使用特殊的短序列 'y' 而不是 'btoa' 支持的 4 个连续空格(ASCII 0x20)。 “标准” Ascii85 编码不支持此功能。

wrapcol 控制输出是否应添加换行符 (b'\n') 字符。 如果这是非零值,则每个输出行最多只有这么多字符长。

pad 控制输入是否在编码前填充为 4 的倍数。 请注意,btoa 实现始终填充。

adobe 控制编码的字节序列是否使用 <~~> 进行框架化,Adobe 实现使用了它们。

3.4 版中的新功能。

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

解码 Ascii85 编码的 bytes-like object 或 ASCII 字符串 b 并返回解码的 bytes

foldspaces 是一个标志,用于指定是否应接受 'y' 短序列作为 4 个连续空格 (ASCII 0x20) 的简写。 “标准” Ascii85 编码不支持此功能。

adobe 控制输入序列是否为 Adobe Ascii85 格式(即 用 框起来)。

ignorechars 应该是一个 bytes-like object 或包含要从输入中忽略的字符的 ASCII 字符串。 这应该只包含空白字符,默认情况下包含 ASCII 中的所有空白字符。

3.4 版中的新功能。

base64.b85encode(b, pad=False)

使用 base85 对 bytes-like object b 进行编码(例如 git 样式的二进制差异)并返回编码的 字节

如果 pad 为真,则输入用 b'\0' 填充,因此其长度是编码前 4 字节的倍数。

3.4 版中的新功能。

base64.b85decode(b)

解码 base85 编码的 bytes-like object 或 ASCII 字符串 b 并返回解码后的 bytes。 如有必要,将隐式删除填充。

3.4 版中的新功能。

遗留接口:

base64.decode(input, output)
对二进制 input 文件的内容进行解码,并将生成的二进制数据写入 output 文件。 inputoutput 必须是 文件对象input 将被读取,直到 input.readline() 返回一个空字节对象。
base64.decodebytes(s)

解码bytes-like object s,其中必须包含一行或多行base64编码数据,并返回解码后的bytes

3.1 版中的新功能。

base64.encode(input, output)
对二进制 input 文件的内容进行编码,并将生成的 base64 编码数据写入 output 文件。 inputoutput 必须是 文件对象input 将被读取,直到 input.read() 返回一个空字节对象。 encode() 在输出的每 76 个字节后插入一个换行符 (b'\n'),并确保输出总是以换行符结尾,如 RFC 2045 (MIME)。
base64.encodebytes(s)

bytes-like object s 进行编码,其中可以包含任意二进制数据,并返回包含 base64 编码数据的 bytes,使用换行符 ([ X172X]) 在每 76 个输出字节后插入,并确保有一个尾随换行符,根据 RFC 2045 (MIME)。

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 到二进制和二进制到 ASCII 转换的支持模块。
RFC 1521 - MIME(多用途互联网邮件扩展)第一部分:指定和描述互联网消息体格式的机制
第 5.2 节“Base64 内容传输编码”提供了 base64 编码的定义。