Sophie

Sophie

distrib > Mageia > 6 > armv5tl > by-pkgid > a600cd26dfe6bfd8c11f12bce5cb0eee > files > 692

python3-docs-3.5.3-1.1.mga6.noarch.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>19.6. base64 — Base16, Base32, Base64, Base85 Data Encodings &mdash; Python 3.5.3 documentation</title>
    
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '3.5.3',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python 3.5.3 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python 3.5.3 documentation" href="../contents.html" />
    <link rel="up" title="19. Internet Data Handling" href="netdata.html" />
    <link rel="next" title="19.7. binhex — Encode and decode binhex4 files" href="binhex.html" />
    <link rel="prev" title="19.5. mimetypes — Map filenames to MIME types" href="mimetypes.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    <script type="text/javascript" src="../_static/version_switch.js"></script>
    
    
 

  </head>
  <body role="document">  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="binhex.html" title="19.7. binhex — Encode and decode binhex4 files"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="mimetypes.html" title="19.5. mimetypes — Map filenames to MIME types"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
        <li>
          <span class="version_switcher_placeholder">3.5.3</span>
          <a href="../index.html">Documentation </a> &raquo;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li class="nav-item nav-item-2"><a href="netdata.html" accesskey="U">19. Internet Data Handling</a> &raquo;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="Quick search" type="text" name="q" />
          <input type="submit" value="Go" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-base64">
<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> &#8212; Base16, Base32, Base64, Base85 Data Encodings<a class="headerlink" href="#module-base64" title="Permalink to this headline">¶</a></h1>
<p><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/3.5/Lib/base64.py">Lib/base64.py</a></p>
<hr class="docutils" id="index-0" />
<p>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 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,
and for the de-facto standard Ascii85 and Base85 encodings.</p>
<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
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
<strong class="program">uuencode</strong> program.</p>
<p>There are two interfaces provided by this module.  The modern interface
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
<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
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>.  Both base-64 alphabets
defined in <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> (normal, and URL- and filesystem-safe) are supported.</p>
<p>The legacy interface does not support decoding from strings, but it does
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
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
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>
package instead.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.3: </span>ASCII-only Unicode strings are now accepted by the decoding functions of
the modern interface.</p>
</div>
<div class="versionchanged">
<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 objects</span></a> are now accepted by all
encoding and decoding functions in this module.  Ascii85/Base85 support added.</p>
</div>
<p>The modern interface provides:</p>
<dl class="function">
<dt id="base64.b64encode">
<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>
<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
<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>
<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
length 2 (additional characters are ignored) which specifies an alternative
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.
generate URL or filesystem safe Base64 strings.  The default is <code class="docutils literal"><span class="pre">None</span></code>, for
which the standard Base64 alphabet is used.</p>
</dd></dl>

<dl class="function">
<dt id="base64.b64decode">
<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>
<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
<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>
<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
at least length 2 (additional characters are ignored) which specifies the
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>
<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
if <em>s</em> is incorrectly padded.</p>
<p>If <em>validate</em> is <code class="docutils literal"><span class="pre">False</span></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 <em>validate</em> is <code class="docutils literal"><span class="pre">True</span></code>,
these non-alphabet characters in the input result in 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>.</p>
</dd></dl>

<dl class="function">
<dt id="base64.standard_b64encode">
<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>
<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
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>
</dd></dl>

<dl class="function">
<dt id="base64.standard_b64decode">
<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>
<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
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>
</dd></dl>

<dl class="function">
<dt id="base64.urlsafe_b64encode">
<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>
<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
URL- and filesystem-safe 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 <code class="docutils literal"><span class="pre">/</span></code> in the
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
can still contain <code class="docutils literal"><span class="pre">=</span></code>.</p>
</dd></dl>

<dl class="function">
<dt id="base64.urlsafe_b64decode">
<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>
<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 URL- and filesystem-safe
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
<code class="docutils literal"><span class="pre">/</span></code> in the standard 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>
</dd></dl>

<dl class="function">
<dt id="base64.b32encode">
<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>
<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
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>
</dd></dl>

<dl class="function">
<dt id="base64.b32decode">
<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>
<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
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>
<p>Optional <em>casefold</em> is a flag specifying
whether a lowercase alphabet is acceptable as input.  For security purposes,
the default is <code class="docutils literal"><span class="pre">False</span></code>.</p>
<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
(oh), and for optional mapping of the digit 1 (one) to either the letter I (eye)
or letter L (el).  The optional argument <em>map01</em> when not <code class="docutils literal"><span class="pre">None</span></code>, specifies
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
digit 0 is always mapped to the letter O).  For security purposes the default is
<code class="docutils literal"><span class="pre">None</span></code>, so that 0 and 1 are not allowed in the input.</p>
<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
incorrectly padded or if there are non-alphabet characters present in the
input.</p>
</dd></dl>

<dl class="function">
<dt id="base64.b16encode">
<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>
<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
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>
</dd></dl>

<dl class="function">
<dt id="base64.b16decode">
<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>
<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
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>
<p>Optional <em>casefold</em> is a flag specifying whether a
lowercase alphabet is acceptable as input.  For security purposes, the default
is <code class="docutils literal"><span class="pre">False</span></code>.</p>
<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
incorrectly padded or if there are non-alphabet characters present in the
input.</p>
</dd></dl>

<dl class="function">
<dt id="base64.a85encode">
<code class="descclassname">base64.</code><code class="descname">a85encode</code><span class="sig-paren">(</span><em>b</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>
<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>b</em> using Ascii85 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>
<p><em>foldspaces</em> is an optional flag that uses the special short sequence &#8216;y&#8217;
instead of 4 consecutive spaces (ASCII 0x20) as supported by &#8216;btoa&#8217;. This
feature is not supported by the &#8220;standard&#8221; Ascii85 encoding.</p>
<p><em>wrapcol</em> controls whether the output should have newline (<code class="docutils literal"><span class="pre">b'\n'</span></code>)
characters added to it. If this is non-zero, each output line will be
at most this many characters long.</p>
<p><em>pad</em> controls whether the input is padded to a multiple of 4
before encoding. Note that the <code class="docutils literal"><span class="pre">btoa</span></code> implementation always pads.</p>
<p><em>adobe</em> controls whether the encoded byte sequence is framed with <code class="docutils literal"><span class="pre">&lt;~</span></code>
and <code class="docutils literal"><span class="pre">~&gt;</span></code>, which is used by the Adobe implementation.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.4.</span></p>
</div>
</dd></dl>

<dl class="function">
<dt id="base64.a85decode">
<code class="descclassname">base64.</code><code class="descname">a85decode</code><span class="sig-paren">(</span><em>b</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>
<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>b</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>
<p><em>foldspaces</em> is a flag that specifies whether the &#8216;y&#8217; short sequence
should be accepted as shorthand for 4 consecutive spaces (ASCII 0x20).
This feature is not supported by the &#8220;standard&#8221; Ascii85 encoding.</p>
<p><em>adobe</em> controls whether the input sequence is in Adobe Ascii85 format
(i.e. is framed with &lt;~ and ~&gt;).</p>
<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
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">
<p><span class="versionmodified">New in version 3.4.</span></p>
</div>
</dd></dl>

<dl class="function">
<dt id="base64.b85encode">
<code class="descclassname">base64.</code><code class="descname">b85encode</code><span class="sig-paren">(</span><em>b</em>, <em>pad=False</em><span class="sig-paren">)</span><a class="headerlink" href="#base64.b85encode" title="Permalink to this definition">¶</a></dt>
<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>b</em> using base85 (as used in e.g.
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>
<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
multiple of 4 bytes before encoding.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.4.</span></p>
</div>
</dd></dl>

<dl class="function">
<dt id="base64.b85decode">
<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>
<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
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
necessary.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.4.</span></p>
</div>
</dd></dl>

<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Both Base85 and Ascii85 have an expansion factor of 5 to 4 (5 Base85 or
Ascii85 characters can encode 4 binary bytes), while the better-known
Base64 has an expansion factor of 6 to 4.  They are therefore more
efficient when space expensive.  They differ by details such as the
character map used for encoding.</p>
</div>
<p>The legacy interface:</p>
<dl class="function">
<dt id="base64.decode">
<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>
<dd><p>Decode the contents of the binary <em>input</em> file and write the resulting binary
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
empty bytes object.</p>
</dd></dl>

<dl class="function">
<dt id="base64.decodebytes">
<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>
<dt id="base64.decodestring">
<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>
<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
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>.
<code class="docutils literal"><span class="pre">decodestring</span></code> is a deprecated alias.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.1.</span></p>
</div>
</dd></dl>

<dl class="function">
<dt id="base64.encode">
<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>
<dd><p>Encode the contents of the binary <em>input</em> file and write the resulting base64
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
objects</span></a>. <em>input</em> will be read until <code class="docutils literal"><span class="pre">input.read()</span></code> returns
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>)
after every 76 bytes of the output, as well as ensuring that the output
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>
</dd></dl>

<dl class="function">
<dt id="base64.encodebytes">
<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>
<dt id="base64.encodestring">
<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>
<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
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
(<code class="docutils literal"><span class="pre">b'\n'</span></code>) inserted after every 76 bytes of output, and ensuring that
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>
<p><code class="docutils literal"><span class="pre">encodestring</span></code> is a deprecated alias.</p>
</dd></dl>

<p>An example usage of the module:</p>
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">base64</span>
<span class="gp">&gt;&gt;&gt; </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">&#39;data to be encoded&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">encoded</span>
<span class="go">b&#39;ZGF0YSB0byBiZSBlbmNvZGVk&#39;</span>
<span class="gp">&gt;&gt;&gt; </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>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span>
<span class="go">b&#39;data to be encoded&#39;</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<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>
<dd>Support module containing ASCII-to-binary and binary-to-ASCII conversions.</dd>
<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>
<dd>Section 5.2, &#8220;Base64 Content-Transfer-Encoding,&#8221; provides the definition of the
base64 encoding.</dd>
</dl>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="mimetypes.html"
                        title="previous chapter">19.5. <code class="docutils literal"><span class="pre">mimetypes</span></code> &#8212; Map filenames to MIME types</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="binhex.html"
                        title="next chapter">19.7. <code class="docutils literal"><span class="pre">binhex</span></code> &#8212; Encode and decode binhex4 files</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">Report a Bug</a></li>
      <li><a href="../_sources/library/base64.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="binhex.html" title="19.7. binhex — Encode and decode binhex4 files"
             >next</a> |</li>
        <li class="right" >
          <a href="mimetypes.html" title="19.5. mimetypes — Map filenames to MIME types"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
        <li>
          <span class="version_switcher_placeholder">3.5.3</span>
          <a href="../index.html">Documentation </a> &raquo;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li class="nav-item nav-item-2"><a href="netdata.html" >19. Internet Data Handling</a> &raquo;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="Quick search" type="text" name="q" />
          <input type="submit" value="Go" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 2001-2017, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Jan 20, 2017.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
    </div>

  </body>
</html>