<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <link rel="STYLESHEET" href="lib.css" type='text/css' /> <link rel="SHORTCUT ICON" href="../icons/pyfav.gif" /> <link rel='start' href='../index.html' title='Python Documentation Index' /> <link rel="first" href="lib.html" title='Python Library Reference' /> <link rel='contents' href='contents.html' title="Contents" /> <link rel='index' href='genindex.html' title='Index' /> <link rel='last' href='about.html' title='About this document...' /> <link rel='help' href='about.html' title='About this document...' /> <LINK rel="next" href="module-binascii.html"> <LINK rel="prev" href="module-rfc822.html"> <LINK rel="parent" href="netdata.html"> <LINK rel="next" href="module-binascii.html"> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <meta name='aesop' content='information' /> <META name="description" content="base64 -- Encode and decode MIME base64 data"> <META name="keywords" content="lib"> <META name="resource-type" content="document"> <META name="distribution" content="global"> <title>12.12 base64 -- Encode and decode MIME base64 data</title> </head> <body> <DIV CLASS="navigation"> <div id='top-navigation-panel'> <table align="center" width="100%" cellpadding="0" cellspacing="2"> <tr> <td class='online-navigation'><a rel="prev" title="12.11.2 AddressList Objects" href="addresslist-objects.html"><img src='../icons/previous.png' border='0' height='32' alt='Previous Page' width='32' /></A></td> <td class='online-navigation'><a rel="parent" title="12. Internet Data Handling" href="netdata.html"><img src='../icons/up.png' border='0' height='32' alt='Up One Level' width='32' /></A></td> <td class='online-navigation'><a rel="next" title="12.13 binascii " href="module-binascii.html"><img src='../icons/next.png' border='0' height='32' alt='Next Page' width='32' /></A></td> <td align="center" width="100%">Python Library Reference</td> <td class='online-navigation'><a rel="contents" title="Table of Contents" href="contents.html"><img src='../icons/contents.png' border='0' height='32' alt='Contents' width='32' /></A></td> <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' border='0' height='32' alt='Module Index' width='32' /></a></td> <td class='online-navigation'><a rel="index" title="Index" href="genindex.html"><img src='../icons/index.png' border='0' height='32' alt='Index' width='32' /></A></td> </tr></table> <div class='online-navigation'> <b class="navlabel">Previous:</b> <a class="sectref" rel="prev" href="addresslist-objects.html">12.11.2 AddressList Objects</A> <b class="navlabel">Up:</b> <a class="sectref" rel="parent" href="netdata.html">12. Internet Data Handling</A> <b class="navlabel">Next:</b> <a class="sectref" rel="next" href="module-binascii.html">12.13 binascii </A> </div> <hr /></div> </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> <A NAME="module-base64"><!--z--></A> <P> <a id='l2h-3818'><!--x--></a><a id='l2h-3823'><!--x--></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" id='rfcref-81726' 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><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-3819' class="function">decode</tt></b>(</nobr></td> <td><var>input, output</var>)</td></tr></table></dt> <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><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-3820' class="function">decodestring</tt></b>(</nobr></td> <td><var>s</var>)</td></tr></table></dt> <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><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-3821' class="function">encode</tt></b>(</nobr></td> <td><var>input, output</var>)</td></tr></table></dt> <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. <tt class="function">encode()</tt> returns the encoded data plus a trailing newline character (<code>'\n'</code>). </dl> <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-3822' class="function">encodestring</tt></b>(</nobr></td> <td><var>s</var>)</td></tr></table></dt> <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. <tt class="function">encodestring()</tt> returns a string containing one or more lines of base64-encoded data always including an extra trailing newline (<code>'\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"> <div class='online-navigation'><hr /> <table align="center" width="100%" cellpadding="0" cellspacing="2"> <tr> <td class='online-navigation'><a rel="prev" title="12.11.2 AddressList Objects" rel="prev" title="12.11.2 AddressList Objects" href="addresslist-objects.html"><img src='../icons/previous.png' border='0' height='32' alt='Previous Page' width='32' /></A></td> <td class='online-navigation'><a rel="parent" title="12. Internet Data Handling" rel="parent" title="12. Internet Data Handling" href="netdata.html"><img src='../icons/up.png' border='0' height='32' alt='Up One Level' width='32' /></A></td> <td class='online-navigation'><a rel="next" title="12.13 binascii " rel="next" title="12.13 binascii " href="module-binascii.html"><img src='../icons/next.png' border='0' height='32' alt='Next Page' width='32' /></A></td> <td align="center" width="100%">Python Library Reference</td> <td class='online-navigation'><a rel="contents" title="Table of Contents" rel="contents" title="Table of Contents" href="contents.html"><img src='../icons/contents.png' border='0' height='32' alt='Contents' width='32' /></A></td> <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' border='0' height='32' alt='Module Index' width='32' /></a></td> <td class='online-navigation'><a rel="index" title="Index" rel="index" title="Index" href="genindex.html"><img src='../icons/index.png' border='0' height='32' alt='Index' width='32' /></A></td> </tr></table> <div class='online-navigation'> <b class="navlabel">Previous:</b> <a class="sectref" rel="prev" href="addresslist-objects.html">12.11.2 AddressList Objects</A> <b class="navlabel">Up:</b> <a class="sectref" rel="parent" href="netdata.html">12. Internet Data Handling</A> <b class="navlabel">Next:</b> <a class="sectref" rel="next" href="module-binascii.html">12.13 binascii </A> </div> </div> <hr /> <span class="release-info">Release 2.3.4, documentation updated on May 20, 2004.</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>