blob: 612da4c52aed0c1ca838398781de55b6abb9f783 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{base64} ---
Fred Drake180b68b1999-02-22 13:45:09 +00002 Encode and decode MIME base64 data}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drake180b68b1999-02-22 13:45:09 +00004\declaremodule{standard}{base64}
5\modulesynopsis{Encode and decode files using the MIME base64 data.}
6
Fred Drakeb91e9341998-07-23 17:59:49 +00007
Fred Drake674e0fd1998-04-02 16:24:29 +00008\indexii{base64}{encoding}
9\index{MIME!base64 encoding}
Guido van Rossume76b7a81997-04-27 21:25:52 +000010
Fred Drake6a50ba81998-05-22 18:19:19 +000011This module performs base64 encoding and decoding of arbitrary binary
Fred Drake8ee679f2001-07-14 02:50:55 +000012strings into text strings that can be safely sent by email or included
13as part of an HTTP POST request. The
Fred Drake38e5d272000-04-03 20:13:55 +000014encoding scheme is defined in \rfc{1521} (\emph{MIME
15(Multipurpose Internet Mail Extensions) Part One: Mechanisms for
16Specifying and Describing the Format of Internet Message Bodies},
17section 5.2, ``Base64 Content-Transfer-Encoding'') and is used for
18MIME email and various other Internet-related applications; it is not
19the same as the output produced by the \program{uuencode} program.
20For example, the string \code{'www.python.org'} is encoded as the
21string \code{'d3d3LnB5dGhvbi5vcmc=\e n'}.
Guido van Rossume76b7a81997-04-27 21:25:52 +000022
Fred Drake798654f1997-11-30 05:53:22 +000023
Fred Drake79948431998-01-07 03:47:10 +000024\begin{funcdesc}{decode}{input, output}
Guido van Rossume76b7a81997-04-27 21:25:52 +000025Decode the contents of the \var{input} file and write the resulting
26binary data to the \var{output} file.
27\var{input} and \var{output} must either be file objects or objects that
28mimic the file object interface. \var{input} will be read until
29\code{\var{input}.read()} returns an empty string.
30\end{funcdesc}
31
32\begin{funcdesc}{decodestring}{s}
33Decode the string \var{s}, which must contain one or more lines of
Fred Drake674e0fd1998-04-02 16:24:29 +000034base64 encoded data, and return a string containing the resulting
Guido van Rossume76b7a81997-04-27 21:25:52 +000035binary data.
36\end{funcdesc}
37
Fred Drake79948431998-01-07 03:47:10 +000038\begin{funcdesc}{encode}{input, output}
Guido van Rossume76b7a81997-04-27 21:25:52 +000039Encode the contents of the \var{input} file and write the resulting
Fred Drake674e0fd1998-04-02 16:24:29 +000040base64 encoded data to the \var{output} file.
Guido van Rossume76b7a81997-04-27 21:25:52 +000041\var{input} and \var{output} must either be file objects or objects that
42mimic the file object interface. \var{input} will be read until
Fred Drakeb3878602001-09-28 16:01:46 +000043\code{\var{input}.read()} returns an empty string. If the last input
44character is not a newline (\code{'\e n'}), a newline will be added to
45the input data.
Guido van Rossume76b7a81997-04-27 21:25:52 +000046\end{funcdesc}
47
48\begin{funcdesc}{encodestring}{s}
49Encode the string \var{s}, which can contain arbitrary binary data,
50and return a string containing one or more lines of
Fred Drakeb3878602001-09-28 16:01:46 +000051base64-encoded data. If the last character of \var{s} is not a
52newline (\code{'\e n'}), a newline will be added. This causes
53\code{encodestring('hello!')} to return the same value as
54\code{encodestring('hello!\e n')}.
Guido van Rossume76b7a81997-04-27 21:25:52 +000055\end{funcdesc}
Fred Drakeae35aa81999-04-23 15:52:18 +000056
57
58\begin{seealso}
Fred Drakec37b65e2001-11-28 07:26:15 +000059 \seemodule{binascii}{Support module containing \ASCII-to-binary
Fred Drakeba0a9892000-10-18 17:43:06 +000060 and binary-to-\ASCII{} conversions.}
Fred Drakee20bd192001-04-12 16:47:17 +000061 \seerfc{1521}{MIME (Multipurpose Internet Mail Extensions) Part One:
62 Mechanisms for Specifying and Describing the Format of
63 Internet Message Bodies}{Section 5.2, ``Base64
64 Content-Transfer-Encoding,'' provides the definition of the
65 base64 encoding.}
Fred Drakeae35aa81999-04-23 15:52:18 +000066\end{seealso}