Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="next" href="module-binascii.html" /> | |
13 | <link rel="prev" href="module-rfc822.html" /> | |
14 | <link rel="parent" href="netdata.html" /> | |
15 | <link rel="next" href="module-binascii.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>12.12 base64 -- RFC 3548: Base16, Base32, Base64 Data Encodings</title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="12.11.2 AddressList Objects" | |
25 | href="addresslist-objects.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="12. Internet Data Handling" | |
28 | href="netdata.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="12.13 binascii " | |
31 | href="module-binascii.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Library Reference</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
38 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="addresslist-objects.html">12.11.2 AddressList Objects</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="netdata.html">12. Internet Data Handling</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="module-binascii.html">12.13 binascii </A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H1><A NAME="SECTION00141200000000000000000"> | |
56 | 12.12 <tt class="module">base64</tt> -- | |
57 | RFC 3548: Base16, Base32, Base64 Data Encodings</A> | |
58 | </H1> | |
59 | ||
60 | <P> | |
61 | <A NAME="module-base64"></A> | |
62 | ||
63 | <P> | |
64 | <a id='l2h-4108' xml:id='l2h-4108'></a><a id='l2h-4123' xml:id='l2h-4123'></a> | |
65 | ||
66 | <P> | |
67 | This module provides data encoding and decoding as specified in | |
68 | <a class="rfc" id='rfcref-91230' xml:id='rfcref-91230' | |
69 | href="http://www.faqs.org/rfcs/rfc3548.html">RFC 3548</a>. This standard defines the Base16, Base32, and Base64 | |
70 | algorithms for encoding and decoding arbitrary binary strings into | |
71 | text strings that can be safely sent by email, used as parts of URLs, | |
72 | or included as part of an HTTP POST request. The encoding algorithm is | |
73 | not the same as the <b class="program">uuencode</b> program. | |
74 | ||
75 | <P> | |
76 | There are two interfaces provided by this module. The modern | |
77 | interface supports encoding and decoding string objects using all | |
78 | three alphabets. The legacy interface provides for encoding and | |
79 | decoding to and from file-like objects as well as strings, but only | |
80 | using the Base64 standard alphabet. | |
81 | ||
82 | <P> | |
83 | The modern interface provides: | |
84 | ||
85 | <P> | |
86 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
87 | <td><nobr><b><tt id='l2h-4109' xml:id='l2h-4109' class="function">b64encode</tt></b>(</nobr></td> | |
88 | <td><var>s</var><big>[</big><var>, altchars</var><big>]</big><var></var>)</td></tr></table></dt> | |
89 | <dd> | |
90 | Encode a string use Base64. | |
91 | ||
92 | <P> | |
93 | <var>s</var> is the string to encode. Optional <var>altchars</var> must be a | |
94 | string of at least length 2 (additional characters are ignored) which | |
95 | specifies an alternative alphabet for the <code>+</code> and <code>/</code> | |
96 | characters. This allows an application to e.g. generate URL or | |
97 | filesystem safe Base64 strings. The default is <code>None</code>, for which | |
98 | the standard Base64 alphabet is used. | |
99 | ||
100 | <P> | |
101 | The encoded string is returned. | |
102 | </dl> | |
103 | ||
104 | <P> | |
105 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
106 | <td><nobr><b><tt id='l2h-4110' xml:id='l2h-4110' class="function">b64decode</tt></b>(</nobr></td> | |
107 | <td><var>s</var><big>[</big><var>, altchars</var><big>]</big><var></var>)</td></tr></table></dt> | |
108 | <dd> | |
109 | Decode a Base64 encoded string. | |
110 | ||
111 | <P> | |
112 | <var>s</var> is the string to decode. Optional <var>altchars</var> must be a | |
113 | string of at least length 2 (additional characters are ignored) which | |
114 | specifies the alternative alphabet used instead of the <code>+</code> and | |
115 | <code>/</code> characters. | |
116 | ||
117 | <P> | |
118 | The decoded string is returned. A <tt class="exception">TypeError</tt> is raised if | |
119 | <var>s</var> were incorrectly padded or if there are non-alphabet | |
120 | characters present in the string. | |
121 | </dl> | |
122 | ||
123 | <P> | |
124 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
125 | <td><nobr><b><tt id='l2h-4111' xml:id='l2h-4111' class="function">standard_b64encode</tt></b>(</nobr></td> | |
126 | <td><var>s</var>)</td></tr></table></dt> | |
127 | <dd> | |
128 | Encode string <var>s</var> using the standard Base64 alphabet. | |
129 | </dl> | |
130 | ||
131 | <P> | |
132 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
133 | <td><nobr><b><tt id='l2h-4112' xml:id='l2h-4112' class="function">standard_b64decode</tt></b>(</nobr></td> | |
134 | <td><var>s</var>)</td></tr></table></dt> | |
135 | <dd> | |
136 | Decode string <var>s</var> using the standard Base64 alphabet. | |
137 | </dl> | |
138 | ||
139 | <P> | |
140 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
141 | <td><nobr><b><tt id='l2h-4113' xml:id='l2h-4113' class="function">urlsafe_b64encode</tt></b>(</nobr></td> | |
142 | <td><var>s</var>)</td></tr></table></dt> | |
143 | <dd> | |
144 | Encode string <var>s</var> using a URL-safe alphabet, which substitutes | |
145 | <code>-</code> instead of <code>+</code> and <code>_</code> instead of <code>/</code> in the | |
146 | standard Base64 alphabet. | |
147 | </dl> | |
148 | ||
149 | <P> | |
150 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
151 | <td><nobr><b><tt id='l2h-4114' xml:id='l2h-4114' class="function">urlsafe_b64decode</tt></b>(</nobr></td> | |
152 | <td><var>s</var>)</td></tr></table></dt> | |
153 | <dd> | |
154 | Decode string <var>s</var> using a URL-safe alphabet, which substitutes | |
155 | <code>-</code> instead of <code>+</code> and <code>_</code> instead of <code>/</code> in the | |
156 | standard Base64 alphabet. | |
157 | </dl> | |
158 | ||
159 | <P> | |
160 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
161 | <td><nobr><b><tt id='l2h-4115' xml:id='l2h-4115' class="function">b32encode</tt></b>(</nobr></td> | |
162 | <td><var>s</var>)</td></tr></table></dt> | |
163 | <dd> | |
164 | Encode a string using Base32. <var>s</var> is the string to encode. The | |
165 | encoded string is returned. | |
166 | </dl> | |
167 | ||
168 | <P> | |
169 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
170 | <td><nobr><b><tt id='l2h-4116' xml:id='l2h-4116' class="function">b32decode</tt></b>(</nobr></td> | |
171 | <td><var>s</var><big>[</big><var>, casefold</var><big>[</big><var>, map01</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> | |
172 | <dd> | |
173 | Decode a Base32 encoded string. | |
174 | ||
175 | <P> | |
176 | <var>s</var> is the string to decode. Optional <var>casefold</var> is a flag | |
177 | specifying whether a lowercase alphabet is acceptable as input. For | |
178 | security purposes, the default is <code>False</code>. | |
179 | ||
180 | <P> | |
181 | <a class="rfc" id='rfcref-91232' xml:id='rfcref-91232' | |
182 | href="http://www.faqs.org/rfcs/rfc3548.html">RFC 3548</a> allows for optional mapping of the digit 0 (zero) to the | |
183 | letter O (oh), and for optional mapping of the digit 1 (one) to either | |
184 | the letter I (eye) or letter L (el). The optional argument | |
185 | <var>map01</var> when not <code>None</code>, specifies which letter the digit 1 should | |
186 | be mapped to (when map01 is not <var>None</var>, the digit 0 is always | |
187 | mapped to the letter O). For security purposes the default is | |
188 | <code>None</code>, so that 0 and 1 are not allowed in the input. | |
189 | ||
190 | <P> | |
191 | The decoded string is returned. A <tt class="exception">TypeError</tt> is raised if | |
192 | <var>s</var> were incorrectly padded or if there are non-alphabet characters | |
193 | present in the string. | |
194 | </dl> | |
195 | ||
196 | <P> | |
197 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
198 | <td><nobr><b><tt id='l2h-4117' xml:id='l2h-4117' class="function">b16encode</tt></b>(</nobr></td> | |
199 | <td><var>s</var>)</td></tr></table></dt> | |
200 | <dd> | |
201 | Encode a string using Base16. | |
202 | ||
203 | <P> | |
204 | <var>s</var> is the string to encode. The encoded string is returned. | |
205 | </dl> | |
206 | ||
207 | <P> | |
208 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
209 | <td><nobr><b><tt id='l2h-4118' xml:id='l2h-4118' class="function">b16decode</tt></b>(</nobr></td> | |
210 | <td><var>s</var><big>[</big><var>, casefold</var><big>]</big><var></var>)</td></tr></table></dt> | |
211 | <dd> | |
212 | Decode a Base16 encoded string. | |
213 | ||
214 | <P> | |
215 | <var>s</var> is the string to decode. Optional <var>casefold</var> is a flag | |
216 | specifying whether a lowercase alphabet is acceptable as input. For | |
217 | security purposes, the default is <code>False</code>. | |
218 | ||
219 | <P> | |
220 | The decoded string is returned. A <tt class="exception">TypeError</tt> is raised if | |
221 | <var>s</var> were incorrectly padded or if there are non-alphabet | |
222 | characters present in the string. | |
223 | </dl> | |
224 | ||
225 | <P> | |
226 | The legacy interface: | |
227 | ||
228 | <P> | |
229 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
230 | <td><nobr><b><tt id='l2h-4119' xml:id='l2h-4119' class="function">decode</tt></b>(</nobr></td> | |
231 | <td><var>input, output</var>)</td></tr></table></dt> | |
232 | <dd> | |
233 | Decode the contents of the <var>input</var> file and write the resulting | |
234 | binary data to the <var>output</var> file. | |
235 | <var>input</var> and <var>output</var> must either be file objects or objects that | |
236 | mimic the file object interface. <var>input</var> will be read until | |
237 | <code><var>input</var>.read()</code> returns an empty string. | |
238 | </dl> | |
239 | ||
240 | <P> | |
241 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
242 | <td><nobr><b><tt id='l2h-4120' xml:id='l2h-4120' class="function">decodestring</tt></b>(</nobr></td> | |
243 | <td><var>s</var>)</td></tr></table></dt> | |
244 | <dd> | |
245 | Decode the string <var>s</var>, which must contain one or more lines of | |
246 | base64 encoded data, and return a string containing the resulting | |
247 | binary data. | |
248 | </dl> | |
249 | ||
250 | <P> | |
251 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
252 | <td><nobr><b><tt id='l2h-4121' xml:id='l2h-4121' class="function">encode</tt></b>(</nobr></td> | |
253 | <td><var>input, output</var>)</td></tr></table></dt> | |
254 | <dd> | |
255 | Encode the contents of the <var>input</var> file and write the resulting | |
256 | base64 encoded data to the <var>output</var> file. | |
257 | <var>input</var> and <var>output</var> must either be file objects or objects that | |
258 | mimic the file object interface. <var>input</var> will be read until | |
259 | <code><var>input</var>.read()</code> returns an empty string. <tt class="function">encode()</tt> | |
260 | returns the encoded data plus a trailing newline character | |
261 | (<code>'\n'</code>). | |
262 | </dl> | |
263 | ||
264 | <P> | |
265 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
266 | <td><nobr><b><tt id='l2h-4122' xml:id='l2h-4122' class="function">encodestring</tt></b>(</nobr></td> | |
267 | <td><var>s</var>)</td></tr></table></dt> | |
268 | <dd> | |
269 | Encode the string <var>s</var>, which can contain arbitrary binary data, | |
270 | and return a string containing one or more lines of | |
271 | base64-encoded data. <tt class="function">encodestring()</tt> returns a | |
272 | string containing one or more lines of base64-encoded data | |
273 | always including an extra trailing newline (<code>'\n'</code>). | |
274 | </dl> | |
275 | ||
276 | <P> | |
277 | <div class="seealso"> | |
278 | <p class="heading">See Also:</p> | |
279 | ||
280 | <dl compact="compact" class="seemodule"> | |
281 | <dt>Module <b><tt class="module"><a href="module-binascii.html">binascii</a></tt>:</b> | |
282 | <dd>Support module containing ASCII-to-binary | |
283 | and binary-to-ASCII conversions. | |
284 | </dl> | |
285 | <dl compact="compact" class="seerfc"> | |
286 | <dt><a href="http://www.faqs.org/rfcs/rfc1521.html" | |
287 | title="MIME (Multipurpose Internet Mail Extensions) Part One: | |
288 | Mechanisms for Specifying and Describing the Format of | |
289 | Internet Message Bodies" | |
290 | >RFC 1521, <em>MIME (Multipurpose Internet Mail Extensions) Part One: | |
291 | Mechanisms for Specifying and Describing the Format of | |
292 | Internet Message Bodies</em></a> | |
293 | <dd>Section 5.2, ``Base64 | |
294 | Content-Transfer-Encoding,'' provides the definition of the | |
295 | base64 encoding. | |
296 | </dl> | |
297 | </div> | |
298 | ||
299 | <DIV CLASS="navigation"> | |
300 | <div class='online-navigation'> | |
301 | <p></p><hr /> | |
302 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
303 | <tr> | |
304 | <td class='online-navigation'><a rel="prev" title="12.11.2 AddressList Objects" | |
305 | href="addresslist-objects.html"><img src='../icons/previous.png' | |
306 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
307 | <td class='online-navigation'><a rel="parent" title="12. Internet Data Handling" | |
308 | href="netdata.html"><img src='../icons/up.png' | |
309 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
310 | <td class='online-navigation'><a rel="next" title="12.13 binascii " | |
311 | href="module-binascii.html"><img src='../icons/next.png' | |
312 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
313 | <td align="center" width="100%">Python Library Reference</td> | |
314 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
315 | href="contents.html"><img src='../icons/contents.png' | |
316 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
317 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
318 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
319 | <td class='online-navigation'><a rel="index" title="Index" | |
320 | href="genindex.html"><img src='../icons/index.png' | |
321 | border='0' height='32' alt='Index' width='32' /></A></td> | |
322 | </tr></table> | |
323 | <div class='online-navigation'> | |
324 | <b class="navlabel">Previous:</b> | |
325 | <a class="sectref" rel="prev" href="addresslist-objects.html">12.11.2 AddressList Objects</A> | |
326 | <b class="navlabel">Up:</b> | |
327 | <a class="sectref" rel="parent" href="netdata.html">12. Internet Data Handling</A> | |
328 | <b class="navlabel">Next:</b> | |
329 | <a class="sectref" rel="next" href="module-binascii.html">12.13 binascii </A> | |
330 | </div> | |
331 | </div> | |
332 | <hr /> | |
333 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
334 | </DIV> | |
335 | <!--End of Navigation Panel--> | |
336 | <ADDRESS> | |
337 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
338 | </ADDRESS> | |
339 | </BODY> | |
340 | </HTML> |