Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{base64} --- |
Fred Drake | 180b68b | 1999-02-22 13:45:09 +0000 | [diff] [blame] | 2 | Encode and decode MIME base64 data} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | |
Fred Drake | 180b68b | 1999-02-22 13:45:09 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{base64} |
| 5 | \modulesynopsis{Encode and decode files using the MIME base64 data.} |
| 6 | |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 7 | |
Fred Drake | 674e0fd | 1998-04-02 16:24:29 +0000 | [diff] [blame] | 8 | \indexii{base64}{encoding} |
| 9 | \index{MIME!base64 encoding} |
Guido van Rossum | e76b7a8 | 1997-04-27 21:25:52 +0000 | [diff] [blame] | 10 | |
Fred Drake | 6a50ba8 | 1998-05-22 18:19:19 +0000 | [diff] [blame] | 11 | This module performs base64 encoding and decoding of arbitrary binary |
Fred Drake | 8ee679f | 2001-07-14 02:50:55 +0000 | [diff] [blame] | 12 | strings into text strings that can be safely sent by email or included |
| 13 | as part of an HTTP POST request. The |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 14 | encoding scheme is defined in \rfc{1521} (\emph{MIME |
| 15 | (Multipurpose Internet Mail Extensions) Part One: Mechanisms for |
| 16 | Specifying and Describing the Format of Internet Message Bodies}, |
| 17 | section 5.2, ``Base64 Content-Transfer-Encoding'') and is used for |
| 18 | MIME email and various other Internet-related applications; it is not |
| 19 | the same as the output produced by the \program{uuencode} program. |
| 20 | For example, the string \code{'www.python.org'} is encoded as the |
| 21 | string \code{'d3d3LnB5dGhvbi5vcmc=\e n'}. |
Guido van Rossum | e76b7a8 | 1997-04-27 21:25:52 +0000 | [diff] [blame] | 22 | |
Fred Drake | 798654f | 1997-11-30 05:53:22 +0000 | [diff] [blame] | 23 | |
Fred Drake | 7994843 | 1998-01-07 03:47:10 +0000 | [diff] [blame] | 24 | \begin{funcdesc}{decode}{input, output} |
Guido van Rossum | e76b7a8 | 1997-04-27 21:25:52 +0000 | [diff] [blame] | 25 | Decode the contents of the \var{input} file and write the resulting |
| 26 | binary data to the \var{output} file. |
| 27 | \var{input} and \var{output} must either be file objects or objects that |
| 28 | mimic 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} |
| 33 | Decode the string \var{s}, which must contain one or more lines of |
Fred Drake | 674e0fd | 1998-04-02 16:24:29 +0000 | [diff] [blame] | 34 | base64 encoded data, and return a string containing the resulting |
Guido van Rossum | e76b7a8 | 1997-04-27 21:25:52 +0000 | [diff] [blame] | 35 | binary data. |
| 36 | \end{funcdesc} |
| 37 | |
Fred Drake | 7994843 | 1998-01-07 03:47:10 +0000 | [diff] [blame] | 38 | \begin{funcdesc}{encode}{input, output} |
Guido van Rossum | e76b7a8 | 1997-04-27 21:25:52 +0000 | [diff] [blame] | 39 | Encode the contents of the \var{input} file and write the resulting |
Fred Drake | 674e0fd | 1998-04-02 16:24:29 +0000 | [diff] [blame] | 40 | base64 encoded data to the \var{output} file. |
Guido van Rossum | e76b7a8 | 1997-04-27 21:25:52 +0000 | [diff] [blame] | 41 | \var{input} and \var{output} must either be file objects or objects that |
| 42 | mimic the file object interface. \var{input} will be read until |
Raymond Hettinger | cadc9fb | 2002-05-16 04:28:44 +0000 | [diff] [blame] | 43 | \code{\var{input}.read()} returns an empty string. \function{encode()} |
| 44 | returns the encoded data plus a trailing newline character |
| 45 | (\code{'\e n'}). |
Guido van Rossum | e76b7a8 | 1997-04-27 21:25:52 +0000 | [diff] [blame] | 46 | \end{funcdesc} |
| 47 | |
| 48 | \begin{funcdesc}{encodestring}{s} |
| 49 | Encode the string \var{s}, which can contain arbitrary binary data, |
| 50 | and return a string containing one or more lines of |
Raymond Hettinger | cadc9fb | 2002-05-16 04:28:44 +0000 | [diff] [blame] | 51 | base64-encoded data. \function{encodestring()} returns a |
| 52 | string containing one or more lines of base64-encoded data |
| 53 | always including an extra trailing newline (\code{'\e n'}). |
Guido van Rossum | e76b7a8 | 1997-04-27 21:25:52 +0000 | [diff] [blame] | 54 | \end{funcdesc} |
Fred Drake | ae35aa8 | 1999-04-23 15:52:18 +0000 | [diff] [blame] | 55 | |
Fred Drake | ae35aa8 | 1999-04-23 15:52:18 +0000 | [diff] [blame] | 56 | \begin{seealso} |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 57 | \seemodule{binascii}{Support module containing \ASCII-to-binary |
Fred Drake | ba0a989 | 2000-10-18 17:43:06 +0000 | [diff] [blame] | 58 | and binary-to-\ASCII{} conversions.} |
Fred Drake | e20bd19 | 2001-04-12 16:47:17 +0000 | [diff] [blame] | 59 | \seerfc{1521}{MIME (Multipurpose Internet Mail Extensions) Part One: |
| 60 | Mechanisms for Specifying and Describing the Format of |
| 61 | Internet Message Bodies}{Section 5.2, ``Base64 |
| 62 | Content-Transfer-Encoding,'' provides the definition of the |
| 63 | base64 encoding.} |
Fred Drake | ae35aa8 | 1999-04-23 15:52:18 +0000 | [diff] [blame] | 64 | \end{seealso} |