1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<html xmlns="http://www.w3.org/1999/xhtml">
7
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9
<title>19.6. base64 — Base16, Base32, Base64, Base85 Data Encodings — Python 3.5.1 documentation</title>
11
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
12
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
14
<script type="text/javascript">
15
var DOCUMENTATION_OPTIONS = {
18
COLLAPSE_INDEX: false,
23
<script type="text/javascript" src="../_static/jquery.js"></script>
24
<script type="text/javascript" src="../_static/underscore.js"></script>
25
<script type="text/javascript" src="../_static/doctools.js"></script>
26
<script type="text/javascript" src="../_static/sidebar.js"></script>
27
<link rel="search" type="application/opensearchdescription+xml"
28
title="Search within Python 3.5.1 documentation"
29
href="../_static/opensearch.xml"/>
30
<link rel="author" title="About these documents" href="../about.html" />
31
<link rel="copyright" title="Copyright" href="../copyright.html" />
32
<link rel="top" title="Python 3.5.1 documentation" href="../contents.html" />
33
<link rel="up" title="19. Internet Data Handling" href="netdata.html" />
34
<link rel="next" title="19.7. binhex — Encode and decode binhex4 files" href="binhex.html" />
35
<link rel="prev" title="19.5. mimetypes — Map filenames to MIME types" href="mimetypes.html" />
36
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
37
<script type="text/javascript" src="../_static/copybutton.js"></script>
38
<script type="text/javascript" src="../_static/version_switch.js"></script>
43
<body role="document">
44
<div class="related" role="navigation" aria-label="related navigation">
47
<li class="right" style="margin-right: 10px">
48
<a href="../genindex.html" title="General Index"
49
accesskey="I">index</a></li>
51
<a href="../py-modindex.html" title="Python Module Index"
54
<a href="binhex.html" title="19.7. binhex — Encode and decode binhex4 files"
55
accesskey="N">next</a> |</li>
57
<a href="mimetypes.html" title="19.5. mimetypes — Map filenames to MIME types"
58
accesskey="P">previous</a> |</li>
59
<li><img src="../_static/py.png" alt=""
60
style="vertical-align: middle; margin-top: -1px"/></li>
61
<li><a href="https://www.python.org/">Python</a> »</li>
63
<span class="version_switcher_placeholder">3.5.1</span>
64
<a href="../index.html">Documentation </a> »
67
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
68
<li class="nav-item nav-item-2"><a href="netdata.html" accesskey="U">19. Internet Data Handling</a> »</li>
72
<div class="document">
73
<div class="documentwrapper">
74
<div class="bodywrapper">
75
<div class="body" role="main">
77
<div class="section" id="module-base64">
78
<span id="base64-base16-base32-base64-base85-data-encodings"></span><h1>19.6. <a class="reference internal" href="#module-base64" title="base64: RFC 3548: Base16, Base32, Base64 Data Encodings; Base85 and Ascii85"><code class="xref py py-mod docutils literal"><span class="pre">base64</span></code></a> — Base16, Base32, Base64, Base85 Data Encodings<a class="headerlink" href="#module-base64" title="Permalink to this headline">¶</a></h1>
79
<p id="index-0">This module provides functions for encoding binary data to printable
80
ASCII characters and decoding such encodings back to binary data.
81
It provides encoding and decoding functions for the encodings specified in
82
<span class="target" id="index-1"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc3548.html"><strong>RFC 3548</strong></a>, which defines the Base16, Base32, and Base64 algorithms,
83
and for the de-facto standard Ascii85 and Base85 encodings.</p>
84
<p>The <span class="target" id="index-2"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc3548.html"><strong>RFC 3548</strong></a> encodings are suitable for encoding binary data so that it can
85
safely sent by email, used as parts of URLs, or included as part of an HTTP
86
POST request. The encoding algorithm is not the same as the
87
<strong class="program">uuencode</strong> program.</p>
88
<p>There are two interfaces provided by this module. The modern interface
89
supports encoding <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like objects</span></a> to ASCII
90
<a class="reference internal" href="functions.html#bytes" title="bytes"><code class="xref py py-class docutils literal"><span class="pre">bytes</span></code></a>, and decoding <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like objects</span></a> or
91
strings containing ASCII to <a class="reference internal" href="functions.html#bytes" title="bytes"><code class="xref py py-class docutils literal"><span class="pre">bytes</span></code></a>. All three <span class="target" id="index-3"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc3548.html"><strong>RFC 3548</strong></a> defined
92
alphabets (normal, URL-safe, and filesystem-safe) are supported.</p>
93
<p>The legacy interface does not support decoding from strings, but it does
94
provide functions for encoding and decoding to and from <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file objects</span></a>. It only supports the Base64 standard alphabet, and it adds
95
newlines every 76 characters as per <span class="target" id="index-4"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2045.html"><strong>RFC 2045</strong></a>. Note that if you are looking
96
for <span class="target" id="index-5"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2045.html"><strong>RFC 2045</strong></a> support you probably want to be looking at the <a class="reference internal" href="email.html#module-email" title="email: Package supporting the parsing, manipulating, and generating email messages, including MIME documents."><code class="xref py py-mod docutils literal"><span class="pre">email</span></code></a>
98
<div class="versionchanged">
99
<p><span class="versionmodified">Changed in version 3.3: </span>ASCII-only Unicode strings are now accepted by the decoding functions of
100
the modern interface.</p>
102
<div class="versionchanged">
103
<p><span class="versionmodified">Changed in version 3.4: </span>Any <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a>s are now accepted by all
104
encoding and decoding functions in this module. Ascii85/Base85 support added.</p>
106
<p>The modern interface provides:</p>
107
<dl class="function">
108
<dt id="base64.b64encode">
109
<code class="descclassname">base64.</code><code class="descname">b64encode</code><span class="sig-paren">(</span><em>s</em>, <em>altchars=None</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.b64encode" title="Permalink to this definition">¶</a></dt>
110
<dd><p>Encode the <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> <em>s</em> using Base64 and return the encoded
111
<a class="reference internal" href="functions.html#bytes" title="bytes"><code class="xref py py-class docutils literal"><span class="pre">bytes</span></code></a>.</p>
112
<p>Optional <em>altchars</em> must be a <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> of at least
113
length 2 (additional characters are ignored) which specifies an alternative
114
alphabet for the <code class="docutils literal"><span class="pre">+</span></code> and <code class="docutils literal"><span class="pre">/</span></code> characters. This allows an application to e.g.
115
generate URL or filesystem safe Base64 strings. The default is <code class="docutils literal"><span class="pre">None</span></code>, for
116
which the standard Base64 alphabet is used.</p>
119
<dl class="function">
120
<dt id="base64.b64decode">
121
<code class="descclassname">base64.</code><code class="descname">b64decode</code><span class="sig-paren">(</span><em>s</em>, <em>altchars=None</em>, <em>validate=False</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.b64decode" title="Permalink to this definition">¶</a></dt>
122
<dd><p>Decode the Base64 encoded <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> or ASCII string
123
<em>s</em> and return the decoded <a class="reference internal" href="functions.html#bytes" title="bytes"><code class="xref py py-class docutils literal"><span class="pre">bytes</span></code></a>.</p>
124
<p>Optional <em>altchars</em> must be a <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> or ASCII string of
125
at least length 2 (additional characters are ignored) which specifies the
126
alternative alphabet used instead of the <code class="docutils literal"><span class="pre">+</span></code> and <code class="docutils literal"><span class="pre">/</span></code> characters.</p>
127
<p>A <a class="reference internal" href="binascii.html#binascii.Error" title="binascii.Error"><code class="xref py py-exc docutils literal"><span class="pre">binascii.Error</span></code></a> exception is raised
128
if <em>s</em> is incorrectly padded.</p>
129
<p>If <em>validate</em> is <code class="docutils literal"><span class="pre">False</span></code> (the default), non-base64-alphabet characters are
130
discarded prior to the padding check. If <em>validate</em> is <code class="docutils literal"><span class="pre">True</span></code>,
131
non-base64-alphabet characters in the input result in a
132
<a class="reference internal" href="binascii.html#binascii.Error" title="binascii.Error"><code class="xref py py-exc docutils literal"><span class="pre">binascii.Error</span></code></a>.</p>
135
<dl class="function">
136
<dt id="base64.standard_b64encode">
137
<code class="descclassname">base64.</code><code class="descname">standard_b64encode</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.standard_b64encode" title="Permalink to this definition">¶</a></dt>
138
<dd><p>Encode <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> <em>s</em> using the standard Base64 alphabet
139
and return the encoded <a class="reference internal" href="functions.html#bytes" title="bytes"><code class="xref py py-class docutils literal"><span class="pre">bytes</span></code></a>.</p>
142
<dl class="function">
143
<dt id="base64.standard_b64decode">
144
<code class="descclassname">base64.</code><code class="descname">standard_b64decode</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.standard_b64decode" title="Permalink to this definition">¶</a></dt>
145
<dd><p>Decode <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> or ASCII string <em>s</em> using the standard
146
Base64 alphabet and return the decoded <a class="reference internal" href="functions.html#bytes" title="bytes"><code class="xref py py-class docutils literal"><span class="pre">bytes</span></code></a>.</p>
149
<dl class="function">
150
<dt id="base64.urlsafe_b64encode">
151
<code class="descclassname">base64.</code><code class="descname">urlsafe_b64encode</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.urlsafe_b64encode" title="Permalink to this definition">¶</a></dt>
152
<dd><p>Encode <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> <em>s</em> using a URL-safe alphabet, which
153
substitutes <code class="docutils literal"><span class="pre">-</span></code> instead of <code class="docutils literal"><span class="pre">+</span></code> and <code class="docutils literal"><span class="pre">_</span></code> instead of <code class="docutils literal"><span class="pre">/</span></code> in the
154
standard Base64 alphabet, and return the encoded <a class="reference internal" href="functions.html#bytes" title="bytes"><code class="xref py py-class docutils literal"><span class="pre">bytes</span></code></a>. The result
155
can still contain <code class="docutils literal"><span class="pre">=</span></code>.</p>
158
<dl class="function">
159
<dt id="base64.urlsafe_b64decode">
160
<code class="descclassname">base64.</code><code class="descname">urlsafe_b64decode</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.urlsafe_b64decode" title="Permalink to this definition">¶</a></dt>
161
<dd><p>Decode <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> or ASCII string <em>s</em> using a URL-safe
162
alphabet, which substitutes <code class="docutils literal"><span class="pre">-</span></code> instead of <code class="docutils literal"><span class="pre">+</span></code> and <code class="docutils literal"><span class="pre">_</span></code> instead of
163
<code class="docutils literal"><span class="pre">/</span></code> in the standard Base64 alphabet, and return the decoded
164
<a class="reference internal" href="functions.html#bytes" title="bytes"><code class="xref py py-class docutils literal"><span class="pre">bytes</span></code></a>.</p>
167
<dl class="function">
168
<dt id="base64.b32encode">
169
<code class="descclassname">base64.</code><code class="descname">b32encode</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.b32encode" title="Permalink to this definition">¶</a></dt>
170
<dd><p>Encode the <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> <em>s</em> using Base32 and return the
171
encoded <a class="reference internal" href="functions.html#bytes" title="bytes"><code class="xref py py-class docutils literal"><span class="pre">bytes</span></code></a>.</p>
174
<dl class="function">
175
<dt id="base64.b32decode">
176
<code class="descclassname">base64.</code><code class="descname">b32decode</code><span class="sig-paren">(</span><em>s</em>, <em>casefold=False</em>, <em>map01=None</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.b32decode" title="Permalink to this definition">¶</a></dt>
177
<dd><p>Decode the Base32 encoded <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> or ASCII string <em>s</em> and
178
return the decoded <a class="reference internal" href="functions.html#bytes" title="bytes"><code class="xref py py-class docutils literal"><span class="pre">bytes</span></code></a>.</p>
179
<p>Optional <em>casefold</em> is a flag specifying
180
whether a lowercase alphabet is acceptable as input. For security purposes,
181
the default is <code class="docutils literal"><span class="pre">False</span></code>.</p>
182
<p><span class="target" id="index-6"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc3548.html"><strong>RFC 3548</strong></a> allows for optional mapping of the digit 0 (zero) to the letter O
183
(oh), and for optional mapping of the digit 1 (one) to either the letter I (eye)
184
or letter L (el). The optional argument <em>map01</em> when not <code class="docutils literal"><span class="pre">None</span></code>, specifies
185
which letter the digit 1 should be mapped to (when <em>map01</em> is not <code class="docutils literal"><span class="pre">None</span></code>, the
186
digit 0 is always mapped to the letter O). For security purposes the default is
187
<code class="docutils literal"><span class="pre">None</span></code>, so that 0 and 1 are not allowed in the input.</p>
188
<p>A <a class="reference internal" href="binascii.html#binascii.Error" title="binascii.Error"><code class="xref py py-exc docutils literal"><span class="pre">binascii.Error</span></code></a> is raised if <em>s</em> is
189
incorrectly padded or if there are non-alphabet characters present in the
193
<dl class="function">
194
<dt id="base64.b16encode">
195
<code class="descclassname">base64.</code><code class="descname">b16encode</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.b16encode" title="Permalink to this definition">¶</a></dt>
196
<dd><p>Encode the <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> <em>s</em> using Base16 and return the
197
encoded <a class="reference internal" href="functions.html#bytes" title="bytes"><code class="xref py py-class docutils literal"><span class="pre">bytes</span></code></a>.</p>
200
<dl class="function">
201
<dt id="base64.b16decode">
202
<code class="descclassname">base64.</code><code class="descname">b16decode</code><span class="sig-paren">(</span><em>s</em>, <em>casefold=False</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.b16decode" title="Permalink to this definition">¶</a></dt>
203
<dd><p>Decode the Base16 encoded <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> or ASCII string <em>s</em> and
204
return the decoded <a class="reference internal" href="functions.html#bytes" title="bytes"><code class="xref py py-class docutils literal"><span class="pre">bytes</span></code></a>.</p>
205
<p>Optional <em>casefold</em> is a flag specifying whether a
206
lowercase alphabet is acceptable as input. For security purposes, the default
207
is <code class="docutils literal"><span class="pre">False</span></code>.</p>
208
<p>A <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal"><span class="pre">TypeError</span></code></a> is raised if <em>s</em> is
209
incorrectly padded or if there are non-alphabet characters present in the
213
<dl class="function">
214
<dt id="base64.a85encode">
215
<code class="descclassname">base64.</code><code class="descname">a85encode</code><span class="sig-paren">(</span><em>s</em>, <em>*</em>, <em>foldspaces=False</em>, <em>wrapcol=0</em>, <em>pad=False</em>, <em>adobe=False</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.a85encode" title="Permalink to this definition">¶</a></dt>
216
<dd><p>Encode the <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> <em>s</em> using Ascii85 and return the
217
encoded <a class="reference internal" href="functions.html#bytes" title="bytes"><code class="xref py py-class docutils literal"><span class="pre">bytes</span></code></a>.</p>
218
<p><em>foldspaces</em> is an optional flag that uses the special short sequence ‘y’
219
instead of 4 consecutive spaces (ASCII 0x20) as supported by ‘btoa’. This
220
feature is not supported by the “standard” Ascii85 encoding.</p>
221
<p><em>wrapcol</em> controls whether the output should have newline (<code class="docutils literal"><span class="pre">b'\n'</span></code>)
222
characters added to it. If this is non-zero, each output line will be
223
at most this many characters long.</p>
224
<p><em>pad</em> controls whether the input is padded to a multiple of 4
225
before encoding. Note that the <code class="docutils literal"><span class="pre">btoa</span></code> implementation always pads.</p>
226
<p><em>adobe</em> controls whether the encoded byte sequence is framed with <code class="docutils literal"><span class="pre"><~</span></code>
227
and <code class="docutils literal"><span class="pre">~></span></code>, which is used by the Adobe implementation.</p>
228
<div class="versionadded">
229
<p><span class="versionmodified">New in version 3.4.</span></p>
233
<dl class="function">
234
<dt id="base64.a85decode">
235
<code class="descclassname">base64.</code><code class="descname">a85decode</code><span class="sig-paren">(</span><em>s</em>, <em>*</em>, <em>foldspaces=False</em>, <em>adobe=False</em>, <em>ignorechars=b' \t\n\r\v'</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.a85decode" title="Permalink to this definition">¶</a></dt>
236
<dd><p>Decode the Ascii85 encoded <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> or ASCII string <em>s</em> and
237
return the decoded <a class="reference internal" href="functions.html#bytes" title="bytes"><code class="xref py py-class docutils literal"><span class="pre">bytes</span></code></a>.</p>
238
<p><em>foldspaces</em> is a flag that specifies whether the ‘y’ short sequence
239
should be accepted as shorthand for 4 consecutive spaces (ASCII 0x20).
240
This feature is not supported by the “standard” Ascii85 encoding.</p>
241
<p><em>adobe</em> controls whether the input sequence is in Adobe Ascii85 format
242
(i.e. is framed with <~ and ~>).</p>
243
<p><em>ignorechars</em> should be a <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> or ASCII string
244
containing characters to ignore
245
from the input. This should only contain whitespace characters, and by
246
default contains all whitespace characters in ASCII.</p>
247
<div class="versionadded">
248
<p><span class="versionmodified">New in version 3.4.</span></p>
252
<dl class="function">
253
<dt id="base64.b85encode">
254
<code class="descclassname">base64.</code><code class="descname">b85encode</code><span class="sig-paren">(</span><em>s</em>, <em>pad=False</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.b85encode" title="Permalink to this definition">¶</a></dt>
255
<dd><p>Encode the <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> <em>s</em> using base85 (as used in e.g.
256
git-style binary diffs) and return the encoded <a class="reference internal" href="functions.html#bytes" title="bytes"><code class="xref py py-class docutils literal"><span class="pre">bytes</span></code></a>.</p>
257
<p>If <em>pad</em> is true, the input is padded with <code class="docutils literal"><span class="pre">b'\0'</span></code> so its length is a
258
multiple of 4 bytes before encoding.</p>
259
<div class="versionadded">
260
<p><span class="versionmodified">New in version 3.4.</span></p>
264
<dl class="function">
265
<dt id="base64.b85decode">
266
<code class="descclassname">base64.</code><code class="descname">b85decode</code><span class="sig-paren">(</span><em>b</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.b85decode" title="Permalink to this definition">¶</a></dt>
267
<dd><p>Decode the base85-encoded <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> or ASCII string <em>b</em> and
268
return the decoded <a class="reference internal" href="functions.html#bytes" title="bytes"><code class="xref py py-class docutils literal"><span class="pre">bytes</span></code></a>. Padding is implicitly removed, if
270
<div class="versionadded">
271
<p><span class="versionmodified">New in version 3.4.</span></p>
275
<div class="admonition note">
276
<p class="first admonition-title">Note</p>
277
<p class="last">Both Base85 and Ascii85 have an expansion factor of 5 to 4 (5 Base85 or
278
Ascii85 characters can encode 4 binary bytes), while the better-known
279
Base64 has an expansion factor of 6 to 4. They are therefore more
280
efficient when space expensive. They differ by details such as the
281
character map used for encoding.</p>
283
<p>The legacy interface:</p>
284
<dl class="function">
285
<dt id="base64.decode">
286
<code class="descclassname">base64.</code><code class="descname">decode</code><span class="sig-paren">(</span><em>input</em>, <em>output</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.decode" title="Permalink to this definition">¶</a></dt>
287
<dd><p>Decode the contents of the binary <em>input</em> file and write the resulting binary
288
data to the <em>output</em> file. <em>input</em> and <em>output</em> must be <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file objects</span></a>. <em>input</em> will be read until <code class="docutils literal"><span class="pre">input.readline()</span></code> returns an
289
empty bytes object.</p>
292
<dl class="function">
293
<dt id="base64.decodebytes">
294
<code class="descclassname">base64.</code><code class="descname">decodebytes</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.decodebytes" title="Permalink to this definition">¶</a></dt>
295
<dt id="base64.decodestring">
296
<code class="descclassname">base64.</code><code class="descname">decodestring</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.decodestring" title="Permalink to this definition">¶</a></dt>
297
<dd><p>Decode the <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> <em>s</em>, which must contain one or more
298
lines of base64 encoded data, and return the decoded <a class="reference internal" href="functions.html#bytes" title="bytes"><code class="xref py py-class docutils literal"><span class="pre">bytes</span></code></a>.
299
<code class="docutils literal"><span class="pre">decodestring</span></code> is a deprecated alias.</p>
300
<div class="versionadded">
301
<p><span class="versionmodified">New in version 3.1.</span></p>
305
<dl class="function">
306
<dt id="base64.encode">
307
<code class="descclassname">base64.</code><code class="descname">encode</code><span class="sig-paren">(</span><em>input</em>, <em>output</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.encode" title="Permalink to this definition">¶</a></dt>
308
<dd><p>Encode the contents of the binary <em>input</em> file and write the resulting base64
309
encoded data to the <em>output</em> file. <em>input</em> and <em>output</em> must be <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file
310
objects</span></a>. <em>input</em> will be read until <code class="docutils literal"><span class="pre">input.read()</span></code> returns
311
an empty bytes object. <a class="reference internal" href="#base64.encode" title="base64.encode"><code class="xref py py-func docutils literal"><span class="pre">encode()</span></code></a> inserts a newline character (<code class="docutils literal"><span class="pre">b'\n'</span></code>)
312
after every 76 bytes of the output, as well as ensuring that the output
313
always ends with a newline, as per <span class="target" id="index-7"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2045.html"><strong>RFC 2045</strong></a> (MIME).</p>
316
<dl class="function">
317
<dt id="base64.encodebytes">
318
<code class="descclassname">base64.</code><code class="descname">encodebytes</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.encodebytes" title="Permalink to this definition">¶</a></dt>
319
<dt id="base64.encodestring">
320
<code class="descclassname">base64.</code><code class="descname">encodestring</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.encodestring" title="Permalink to this definition">¶</a></dt>
321
<dd><p>Encode the <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> <em>s</em>, which can contain arbitrary binary
322
data, and return <a class="reference internal" href="functions.html#bytes" title="bytes"><code class="xref py py-class docutils literal"><span class="pre">bytes</span></code></a> containing the base64-encoded data, with newlines
323
(<code class="docutils literal"><span class="pre">b'\n'</span></code>) inserted after every 76 bytes of output, and ensuring that
324
there is a trailing newline, as per <span class="target" id="index-8"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2045.html"><strong>RFC 2045</strong></a> (MIME).</p>
325
<p><code class="docutils literal"><span class="pre">encodestring</span></code> is a deprecated alias.</p>
328
<p>An example usage of the module:</p>
329
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">base64</span>
330
<span class="gp">>>> </span><span class="n">encoded</span> <span class="o">=</span> <span class="n">base64</span><span class="o">.</span><span class="n">b64encode</span><span class="p">(</span><span class="n">b</span><span class="s1">'data to be encoded'</span><span class="p">)</span>
331
<span class="gp">>>> </span><span class="n">encoded</span>
332
<span class="go">b'ZGF0YSB0byBiZSBlbmNvZGVk'</span>
333
<span class="gp">>>> </span><span class="n">data</span> <span class="o">=</span> <span class="n">base64</span><span class="o">.</span><span class="n">b64decode</span><span class="p">(</span><span class="n">encoded</span><span class="p">)</span>
334
<span class="gp">>>> </span><span class="n">data</span>
335
<span class="go">b'data to be encoded'</span>
338
<div class="admonition seealso">
339
<p class="first admonition-title">See also</p>
340
<dl class="last docutils">
341
<dt>Module <a class="reference internal" href="binascii.html#module-binascii" title="binascii: Tools for converting between binary and various ASCII-encoded binary representations."><code class="xref py py-mod docutils literal"><span class="pre">binascii</span></code></a></dt>
342
<dd>Support module containing ASCII-to-binary and binary-to-ASCII conversions.</dd>
343
<dt><span class="target" id="index-9"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc1521.html"><strong>RFC 1521</strong></a> - MIME (Multipurpose Internet Mail Extensions) Part One: Mechanisms for Specifying and Describing the Format of Internet Message Bodies</dt>
344
<dd>Section 5.2, “Base64 Content-Transfer-Encoding,” provides the definition of the
345
base64 encoding.</dd>
354
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
355
<div class="sphinxsidebarwrapper">
356
<h4>Previous topic</h4>
357
<p class="topless"><a href="mimetypes.html"
358
title="previous chapter">19.5. <code class="docutils literal"><span class="pre">mimetypes</span></code> — Map filenames to MIME types</a></p>
360
<p class="topless"><a href="binhex.html"
361
title="next chapter">19.7. <code class="docutils literal"><span class="pre">binhex</span></code> — Encode and decode binhex4 files</a></p>
363
<ul class="this-page-menu">
364
<li><a href="../bugs.html">Report a Bug</a></li>
365
<li><a href="../_sources/library/base64.txt"
366
rel="nofollow">Show Source</a></li>
369
<div id="searchbox" style="display: none" role="search">
370
<h3>Quick search</h3>
371
<form class="search" action="../search.html" method="get">
372
<input type="text" name="q" />
373
<input type="submit" value="Go" />
374
<input type="hidden" name="check_keywords" value="yes" />
375
<input type="hidden" name="area" value="default" />
377
<p class="searchtip" style="font-size: 90%">
378
Enter search terms or a module, class or function name.
381
<script type="text/javascript">$('#searchbox').show(0);</script>
384
<div class="clearer"></div>
386
<div class="related" role="navigation" aria-label="related navigation">
389
<li class="right" style="margin-right: 10px">
390
<a href="../genindex.html" title="General Index"
393
<a href="../py-modindex.html" title="Python Module Index"
396
<a href="binhex.html" title="19.7. binhex — Encode and decode binhex4 files"
399
<a href="mimetypes.html" title="19.5. mimetypes — Map filenames to MIME types"
401
<li><img src="../_static/py.png" alt=""
402
style="vertical-align: middle; margin-top: -1px"/></li>
403
<li><a href="https://www.python.org/">Python</a> »</li>
405
<span class="version_switcher_placeholder">3.5.1</span>
406
<a href="../index.html">Documentation </a> »
409
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
410
<li class="nav-item nav-item-2"><a href="netdata.html" >19. Internet Data Handling</a> »</li>
414
© <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
416
The Python Software Foundation is a non-profit corporation.
417
<a href="https://www.python.org/psf/donations/">Please donate.</a>
419
Last updated on Jan 22, 2016.
420
<a href="../bugs.html">Found a bug</a>?
422
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
b'\\ No newline at end of file'