Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-updates > by-pkgid > e1011ddec34cda34f3a002b121247943 > files > 691

python-docs-2.7.17-1.1.mga7.noarch.rpm


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta charset="utf-8" />
    <title>18.12. base64 — RFC 3548: Base16, Base32, Base64 Data Encodings &#8212; Python 2.7.17 documentation</title>
    <link rel="stylesheet" href="../_static/classic.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></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/language_data.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python 2.7.17 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="next" title="18.13. binhex — Encode and decode binhex4 files" href="binhex.html" />
    <link rel="prev" title="18.11. rfc822 — Parse RFC 2822 mail headers" href="rfc822.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/2/library/base64.html" />
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
 
    

  </head><body>  
    <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="18.13. binhex — Encode and decode binhex4 files"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="rfc822.html" title="18.11. rfc822 — Parse RFC 2822 mail headers"
             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> &#187;</li>
        <li>
          <a href="../index.html">Python 2.7.17 documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="netdata.html" accesskey="U">18. Internet Data Handling</a> &#187;</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-rfc-3548-base16-base32-base64-data-encodings"></span><h1>18.12. <a class="reference internal" href="#module-base64" title="base64: RFC 3548: Base16, Base32, Base64 Data Encodings"><code class="xref py py-mod docutils literal notranslate"><span class="pre">base64</span></code></a> — RFC 3548: Base16, Base32, Base64 Data Encodings<a class="headerlink" href="#module-base64" title="Permalink to this headline">¶</a></h1>
<p id="index-0">This module provides data encoding and decoding as 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>.
This standard defines the Base16, Base32, and Base64 algorithms for encoding and
decoding arbitrary binary strings into text strings that can be 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 and decoding string objects using both base-64 alphabets defined
in <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> (normal, and URL- and filesystem-safe).  The legacy
interface provides for encoding and decoding to and from file-like objects as
well as strings, but only using the Base64 standard alphabet.</p>
<p>The modern interface, which was introduced in Python 2.4, 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><span class="optional">[</span>, <em>altchars</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#base64.b64encode" title="Permalink to this definition">¶</a></dt>
<dd><p>Encode a string using Base64.</p>
<p><em>s</em> is the string to encode.  Optional <em>altchars</em> must be a string of at least
length 2 (additional characters are ignored) which specifies an alternative
alphabet for the <code class="docutils literal notranslate"><span class="pre">+</span></code> and <code class="docutils literal notranslate"><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 notranslate"><span class="pre">None</span></code>, for
which the standard Base64 alphabet is used.</p>
<p>The encoded string is returned.</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><span class="optional">[</span>, <em>altchars</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#base64.b64decode" title="Permalink to this definition">¶</a></dt>
<dd><p>Decode a Base64 encoded string.</p>
<p><em>s</em> is the string to decode.  Optional <em>altchars</em> must be a string of at least
length 2 (additional characters are ignored) which specifies the alternative
alphabet used instead of the <code class="docutils literal notranslate"><span class="pre">+</span></code> and <code class="docutils literal notranslate"><span class="pre">/</span></code> characters.</p>
<p>The decoded string is returned.  A <a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised if <em>s</em> is
incorrectly padded.  Characters that are neither
in the normal base-64 alphabet nor the alternative alphabet are
discarded prior to the padding check.</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 string <em>s</em> using the standard Base64 alphabet.</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 string <em>s</em> using the standard Base64 alphabet.</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 string <em>s</em> using the URL- and filesystem-safe
alphabet, which substitutes <code class="docutils literal notranslate"><span class="pre">-</span></code> instead of
<code class="docutils literal notranslate"><span class="pre">+</span></code> and <code class="docutils literal notranslate"><span class="pre">_</span></code> instead of <code class="docutils literal notranslate"><span class="pre">/</span></code> in the standard Base64 alphabet.  The result
can still contain <code class="docutils literal notranslate"><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 string <em>s</em> using the URL- and filesystem-safe
alphabet, which substitutes <code class="docutils literal notranslate"><span class="pre">-</span></code> instead of
<code class="docutils literal notranslate"><span class="pre">+</span></code> and <code class="docutils literal notranslate"><span class="pre">_</span></code> instead of <code class="docutils literal notranslate"><span class="pre">/</span></code> in the standard Base64 alphabet.</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 a string using Base32.  <em>s</em> is the string to encode.  The encoded string
is returned.</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><span class="optional">[</span>, <em>casefold</em><span class="optional">[</span>, <em>map01</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#base64.b32decode" title="Permalink to this definition">¶</a></dt>
<dd><p>Decode a Base32 encoded string.</p>
<p><em>s</em> is the string to decode.  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 notranslate"><span class="pre">False</span></code>.</p>
<p><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> 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 notranslate"><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 notranslate"><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 notranslate"><span class="pre">None</span></code>, so that 0 and 1 are not allowed in the input.</p>
<p>The decoded string is returned.  A <a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised if <em>s</em> is
incorrectly padded or if there are non-alphabet characters present in the
string.</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 a string using Base16.</p>
<p><em>s</em> is the string to encode.  The encoded string is returned.</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><span class="optional">[</span>, <em>casefold</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#base64.b16decode" title="Permalink to this definition">¶</a></dt>
<dd><p>Decode a Base16 encoded string.</p>
<p><em>s</em> is the string to decode.  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 notranslate"><span class="pre">False</span></code>.</p>
<p>The decoded string is returned.  A <a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised if <em>s</em> were
incorrectly padded or if there are non-alphabet characters present in the
string.</p>
</dd></dl>

<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 <em>input</em> file and write the resulting binary data to
the <em>output</em> file. <em>input</em> and <em>output</em> must either be file objects or objects
that mimic the file object interface. <em>input</em> will be read until
<code class="docutils literal notranslate"><span class="pre">input.read()</span></code> returns an empty string.</p>
</dd></dl>

<dl class="function">
<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 string <em>s</em>, which must contain one or more lines of base64 encoded
data, and return a string containing the resulting binary data.</p>
</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 <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 either be file objects or
objects that mimic the file object interface. <em>input</em> will be read until
<code class="docutils literal notranslate"><span class="pre">input.read()</span></code> returns an empty string.  <a class="reference internal" href="#base64.encode" title="base64.encode"><code class="xref py py-func docutils literal notranslate"><span class="pre">encode()</span></code></a> returns the encoded
data plus a trailing newline character (<code class="docutils literal notranslate"><span class="pre">'\n'</span></code>).</p>
</dd></dl>

<dl class="function">
<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 string <em>s</em>, which can contain arbitrary binary data, and return a
string containing one or more lines of base64-encoded data.
<a class="reference internal" href="#base64.encodestring" title="base64.encodestring"><code class="xref py py-func docutils literal notranslate"><span class="pre">encodestring()</span></code></a> returns a string containing one or more lines of
base64-encoded data always including an extra trailing newline (<code class="docutils literal notranslate"><span class="pre">'\n'</span></code>).</p>
</dd></dl>

<p>An example usage of the module:</p>
<div class="highlight-default notranslate"><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="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">&#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">&#39;data to be encoded&#39;</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<dl class="simple">
<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 notranslate"><span class="pre">binascii</span></code></a></dt><dd><p>Support module containing ASCII-to-binary and binary-to-ASCII conversions.</p>
</dd>
<dt><span class="target" id="index-4"></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><p>Section 5.2, “Base64 Content-Transfer-Encoding,” provides the definition of the
base64 encoding.</p>
</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="rfc822.html"
                        title="previous chapter">18.11. <code class="xref py py-mod docutils literal notranslate"><span class="pre">rfc822</span></code> — Parse RFC 2822 mail headers</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="binhex.html"
                        title="next chapter">18.13. <code class="xref py py-mod docutils literal notranslate"><span class="pre">binhex</span></code> — 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="../_sources/library/base64.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </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="18.13. binhex — Encode and decode binhex4 files"
             >next</a> |</li>
        <li class="right" >
          <a href="rfc822.html" title="18.11. rfc822 — Parse RFC 2822 mail headers"
             >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> &#187;</li>
        <li>
          <a href="../index.html">Python 2.7.17 documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="netdata.html" >18. Internet Data Handling</a> &#187;</li> 
      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2019, 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 Oct 19, 2019.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.0.1.
    </div>

  </body>
</html>