<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>12.12 base64 -- Encode and decode MIME base64 data</title> <META NAME="description" CONTENT="12.12 base64 -- Encode and decode MIME base64 data"> <META NAME="keywords" CONTENT="lib"> <META NAME="resource-type" CONTENT="document"> <META NAME="distribution" CONTENT="global"> <meta http-equiv="Content-Type" content="text/html; charset="> <link rel="STYLESHEET" href="lib.css"> <link rel="first" href="lib.html"> <link rel="contents" href="contents.html" title="Contents"> <link rel="index" href="genindex.html" title="Index"> <LINK REL="next" href="module-binascii.html"> <LINK REL="previous" href="module-rfc822.html"> <LINK REL="up" href="netdata.html"> <LINK REL="next" href="module-binascii.html"> </head> <body> <DIV CLASS="navigation"> <table align="center" width="100%" cellpadding="0" cellspacing="2"> <tr> <td><A href="addresslist-objects.html"><img src="../icons/previous.gif" border="0" height="32" alt="Previous Page" width="32"></A></td> <td><A href="netdata.html"><img src="../icons/up.gif" border="0" height="32" alt="Up One Level" width="32"></A></td> <td><A href="module-binascii.html"><img src="../icons/next.gif" border="0" height="32" alt="Next Page" width="32"></A></td> <td align="center" width="100%">Python Library Reference</td> <td><A href="contents.html"><img src="../icons/contents.gif" border="0" height="32" alt="Contents" width="32"></A></td> <td><a href="modindex.html" title="Module Index"><img src="../icons/modules.gif" border="0" height="32" alt="Module Index" width="32"></a></td> <td><A href="genindex.html"><img src="../icons/index.gif" border="0" height="32" alt="Index" width="32"></A></td> </tr></table> <b class="navlabel">Previous:</b> <a class="sectref" href="addresslist-objects.html">12.11.2 AddressList Objects</A> <b class="navlabel">Up:</b> <a class="sectref" href="netdata.html">12. Internet Data Handling</A> <b class="navlabel">Next:</b> <a class="sectref" href="module-binascii.html">12.13 binascii </A> <br><hr> </DIV> <!--End of Navigation Panel--> <H1><A NAME="SECTION00141200000000000000000"> 12.12 <tt class="module">base64</tt> -- Encode and decode MIME base64 data</A> </H1> <P> <P> <a name="l2h-3016"> </a> <P> This module performs base64 encoding and decoding of arbitrary binary strings into text strings that can be safely sent by email or included as part of an HTTP POST request. The encoding scheme is defined in <a class="rfc" name="rfcref-64040" href="http://www.faqs.org/rfcs/rfc1521.html">RFC 1521</a> (<i>MIME (Multipurpose Internet Mail Extensions) Part One: Mechanisms for Specifying and Describing the Format of Internet Message Bodies</i>, section 5.2, ``Base64 Content-Transfer-Encoding'') and is used for MIME email and various other Internet-related applications; it is not the same as the output produced by the <b class="program">uuencode</b> program. For example, the string <code>'www.python.org'</code> is encoded as the string <code>'d3d3LnB5dGhvbi5vcmc=\n'</code>. <P> <dl><dt><b><a name="l2h-3012"><tt class="function">decode</tt></a></b>(<var>input, output</var>) <dd> Decode the contents of the <var>input</var> file and write the resulting binary data to the <var>output</var> file. <var>input</var> and <var>output</var> must either be file objects or objects that mimic the file object interface. <var>input</var> will be read until <code><var>input</var>.read()</code> returns an empty string. </dl> <P> <dl><dt><b><a name="l2h-3013"><tt class="function">decodestring</tt></a></b>(<var>s</var>) <dd> Decode the string <var>s</var>, which must contain one or more lines of base64 encoded data, and return a string containing the resulting binary data. </dl> <P> <dl><dt><b><a name="l2h-3014"><tt class="function">encode</tt></a></b>(<var>input, output</var>) <dd> Encode the contents of the <var>input</var> file and write the resulting base64 encoded data to the <var>output</var> file. <var>input</var> and <var>output</var> must either be file objects or objects that mimic the file object interface. <var>input</var> will be read until <code><var>input</var>.read()</code> returns an empty string. If the last input character is not a newline (<code>'\n'</code>), a newline will be added to the input data. </dl> <P> <dl><dt><b><a name="l2h-3015"><tt class="function">encodestring</tt></a></b>(<var>s</var>) <dd> Encode the string <var>s</var>, which can contain arbitrary binary data, and return a string containing one or more lines of base64-encoded data. If the last character of <var>s</var> is not a newline (<code>'\n'</code>), a newline will be added. This causes <code>encodestring('hello!')</code> to return the same value as <code>encodestring('hello!\n')</code>. </dl> <P> <div class="seealso"> <p class="heading"><b>See Also:</b></p> <dl compact class="seemodule"> <dt>Module <b><tt class="module"><a href="module-binascii.html">binascii</a></tt>:</b> <dd>Support module containing ASCII-to-binary and binary-to-ASCII conversions. </dl> <dl compact class="seerfc"> <dt><a href="http://www.faqs.org/rfcs/rfc1521.html" title="MIME (Multipurpose Internet Mail Extensions) Part One: Mechanisms for Specifying and Describing the Format of Internet Message Bodies" >RFC 1521, <em>MIME (Multipurpose Internet Mail Extensions) Part One: Mechanisms for Specifying and Describing the Format of Internet Message Bodies</em></a> <dd>Section 5.2, ``Base64 Content-Transfer-Encoding,'' provides the definition of the base64 encoding. </dl> </div> <DIV CLASS="navigation"> <p><hr> <table align="center" width="100%" cellpadding="0" cellspacing="2"> <tr> <td><A href="addresslist-objects.html"><img src="../icons/previous.gif" border="0" height="32" alt="Previous Page" width="32"></A></td> <td><A href="netdata.html"><img src="../icons/up.gif" border="0" height="32" alt="Up One Level" width="32"></A></td> <td><A href="module-binascii.html"><img src="../icons/next.gif" border="0" height="32" alt="Next Page" width="32"></A></td> <td align="center" width="100%">Python Library Reference</td> <td><A href="contents.html"><img src="../icons/contents.gif" border="0" height="32" alt="Contents" width="32"></A></td> <td><a href="modindex.html" title="Module Index"><img src="../icons/modules.gif" border="0" height="32" alt="Module Index" width="32"></a></td> <td><A href="genindex.html"><img src="../icons/index.gif" border="0" height="32" alt="Index" width="32"></A></td> </tr></table> <b class="navlabel">Previous:</b> <a class="sectref" href="addresslist-objects.html">12.11.2 AddressList Objects</A> <b class="navlabel">Up:</b> <a class="sectref" href="netdata.html">12. Internet Data Handling</A> <b class="navlabel">Next:</b> <a class="sectref" href="module-binascii.html">12.13 binascii </A> <hr> <span class="release-info">Release 2.2, documentation updated on December 21, 2001.</span> </DIV> <!--End of Navigation Panel--> <ADDRESS> See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. </ADDRESS> </BODY> </HTML>