Commit | Line | Data |
---|---|---|
86530b38 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-MimeWriter.html" /> | |
13 | <link rel="prev" href="module-mimetools.html" /> | |
14 | <link rel="parent" href="netdata.html" /> | |
15 | <link rel="next" href="mimetypes-objects.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>12.7 mimetypes -- Map filenames to MIME types</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.6.1 Additional Methods of" | |
25 | href="mimetools-message-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.7.1 MimeTypes Objects" | |
31 | href="mimetypes-objects.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="mimetools-message-objects.html">12.6.1 Additional Methods of</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="mimetypes-objects.html">12.7.1 MimeTypes Objects</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H1><A NAME="SECTION0014700000000000000000"> | |
56 | 12.7 <tt class="module">mimetypes</tt> -- | |
57 | Map filenames to MIME types</A> | |
58 | </H1> | |
59 | ||
60 | <P> | |
61 | <A NAME="module-mimetypes"></A> | |
62 | ||
63 | <P> | |
64 | <a id='l2h-4019' xml:id='l2h-4019'></a> | |
65 | <P> | |
66 | The <tt class="module">mimetypes</tt> module converts between a filename or URL and | |
67 | the MIME type associated with the filename extension. Conversions are | |
68 | provided from filename to MIME type and from MIME type to filename | |
69 | extension; encodings are not supported for the latter conversion. | |
70 | ||
71 | <P> | |
72 | The module provides one class and a number of convenience functions. | |
73 | The functions are the normal interface to this module, but some | |
74 | applications may be interested in the class as well. | |
75 | ||
76 | <P> | |
77 | The functions described below provide the primary interface for this | |
78 | module. If the module has not been initialized, they will call | |
79 | <tt class="function">init()</tt> if they rely on the information <tt class="function">init()</tt> | |
80 | sets up. | |
81 | ||
82 | <P> | |
83 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
84 | <td><nobr><b><tt id='l2h-4020' xml:id='l2h-4020' class="function">guess_type</tt></b>(</nobr></td> | |
85 | <td><var>filename</var><big>[</big><var>, strict</var><big>]</big><var></var>)</td></tr></table></dt> | |
86 | <dd> | |
87 | Guess the type of a file based on its filename or URL, given by | |
88 | <var>filename</var>. The return value is a tuple <code>(<var>type</var>, | |
89 | <var>encoding</var>)</code> where <var>type</var> is <code>None</code> if the type can't be | |
90 | guessed (missing or unknown suffix) or a string of the form | |
91 | <code>'<var>type</var>/<var>subtype</var>'</code>, usable for a MIME | |
92 | <span class="mailheader">content-type:</span> header<a id='l2h-4021' xml:id='l2h-4021'></a>. | |
93 | ||
94 | <P> | |
95 | <var>encoding</var> is <code>None</code> for no encoding or the name of the | |
96 | program used to encode (e.g. <b class="program">compress</b> or <b class="program">gzip</b>). | |
97 | The encoding is suitable for use as a <span class="mailheader">Content-Encoding:</span> | |
98 | header, <em>not</em> as a <span class="mailheader">Content-Transfer-Encoding:</span> header. | |
99 | The mappings are table driven. Encoding suffixes are case sensitive; | |
100 | type suffixes are first tried case sensitively, then case | |
101 | insensitively. | |
102 | ||
103 | <P> | |
104 | Optional <var>strict</var> is a flag specifying whether the list of known | |
105 | MIME types is limited to only the official types <a class="ulink" href="http://www.isi.edu/in-notes/iana/assignments/media-types" | |
106 | >registered | |
107 | with IANA</a> | |
108 | are recognized. When <var>strict</var> is true (the default), only the | |
109 | IANA types are supported; when <var>strict</var> is false, some additional | |
110 | non-standard but commonly used MIME types are also recognized. | |
111 | </dl> | |
112 | ||
113 | <P> | |
114 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
115 | <td><nobr><b><tt id='l2h-4022' xml:id='l2h-4022' class="function">guess_all_extensions</tt></b>(</nobr></td> | |
116 | <td><var>type</var><big>[</big><var>, strict</var><big>]</big><var></var>)</td></tr></table></dt> | |
117 | <dd> | |
118 | Guess the extensions for a file based on its MIME type, given by | |
119 | <var>type</var>. | |
120 | The return value is a list of strings giving all possible filename extensions, | |
121 | including the leading dot ("<tt class="character">.</tt>"). The extensions are not guaranteed | |
122 | to have been associated with any particular data stream, but would be mapped | |
123 | to the MIME type <var>type</var> by <tt class="function">guess_type()</tt>. | |
124 | ||
125 | <P> | |
126 | Optional <var>strict</var> has the same meaning as with the | |
127 | <tt class="function">guess_type()</tt> function. | |
128 | </dl> | |
129 | ||
130 | <P> | |
131 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
132 | <td><nobr><b><tt id='l2h-4023' xml:id='l2h-4023' class="function">guess_extension</tt></b>(</nobr></td> | |
133 | <td><var>type</var><big>[</big><var>, strict</var><big>]</big><var></var>)</td></tr></table></dt> | |
134 | <dd> | |
135 | Guess the extension for a file based on its MIME type, given by | |
136 | <var>type</var>. | |
137 | The return value is a string giving a filename extension, including the | |
138 | leading dot ("<tt class="character">.</tt>"). The extension is not guaranteed to have been | |
139 | associated with any particular data stream, but would be mapped to the | |
140 | MIME type <var>type</var> by <tt class="function">guess_type()</tt>. If no extension can | |
141 | be guessed for <var>type</var>, <code>None</code> is returned. | |
142 | ||
143 | <P> | |
144 | Optional <var>strict</var> has the same meaning as with the | |
145 | <tt class="function">guess_type()</tt> function. | |
146 | </dl> | |
147 | ||
148 | <P> | |
149 | Some additional functions and data items are available for controlling | |
150 | the behavior of the module. | |
151 | ||
152 | <P> | |
153 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
154 | <td><nobr><b><tt id='l2h-4024' xml:id='l2h-4024' class="function">init</tt></b>(</nobr></td> | |
155 | <td><var></var><big>[</big><var>files</var><big>]</big><var></var>)</td></tr></table></dt> | |
156 | <dd> | |
157 | Initialize the internal data structures. If given, <var>files</var> must | |
158 | be a sequence of file names which should be used to augment the | |
159 | default type map. If omitted, the file names to use are taken from | |
160 | <tt class="constant">knownfiles</tt>. Each file named in <var>files</var> or | |
161 | <tt class="constant">knownfiles</tt> takes precedence over those named before it. | |
162 | Calling <tt class="function">init()</tt> repeatedly is allowed. | |
163 | </dl> | |
164 | ||
165 | <P> | |
166 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
167 | <td><nobr><b><tt id='l2h-4025' xml:id='l2h-4025' class="function">read_mime_types</tt></b>(</nobr></td> | |
168 | <td><var>filename</var>)</td></tr></table></dt> | |
169 | <dd> | |
170 | Load the type map given in the file <var>filename</var>, if it exists. The | |
171 | type map is returned as a dictionary mapping filename extensions, | |
172 | including the leading dot ("<tt class="character">.</tt>"), to strings of the form | |
173 | <code>'<var>type</var>/<var>subtype</var>'</code>. If the file <var>filename</var> does | |
174 | not exist or cannot be read, <code>None</code> is returned. | |
175 | </dl> | |
176 | ||
177 | <P> | |
178 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
179 | <td><nobr><b><tt id='l2h-4026' xml:id='l2h-4026' class="function">add_type</tt></b>(</nobr></td> | |
180 | <td><var>type, ext</var><big>[</big><var>, strict</var><big>]</big><var></var>)</td></tr></table></dt> | |
181 | <dd> | |
182 | Add a mapping from the mimetype <var>type</var> to the extension <var>ext</var>. | |
183 | When the extension is already known, the new type will replace the old | |
184 | one. When the type is already known the extension will be added | |
185 | to the list of known extensions. | |
186 | ||
187 | <P> | |
188 | When <var>strict</var> is the mapping will added to the official | |
189 | MIME types, otherwise to the non-standard ones. | |
190 | </dl> | |
191 | ||
192 | <P> | |
193 | <dl><dt><b><tt id='l2h-4027' xml:id='l2h-4027'>inited</tt></b></dt> | |
194 | <dd> | |
195 | Flag indicating whether or not the global data structures have been | |
196 | initialized. This is set to true by <tt class="function">init()</tt>. | |
197 | </dd></dl> | |
198 | ||
199 | <P> | |
200 | <dl><dt><b><tt id='l2h-4028' xml:id='l2h-4028'>knownfiles</tt></b></dt> | |
201 | <dd> | |
202 | List of type map file names commonly installed. These files are | |
203 | typically named <span class="file">mime.types</span> and are installed in different | |
204 | locations by different packages.<a id='l2h-4034' xml:id='l2h-4034'></a> | |
205 | </dd></dl> | |
206 | ||
207 | <P> | |
208 | <dl><dt><b><tt id='l2h-4029' xml:id='l2h-4029'>suffix_map</tt></b></dt> | |
209 | <dd> | |
210 | Dictionary mapping suffixes to suffixes. This is used to allow | |
211 | recognition of encoded files for which the encoding and the type are | |
212 | indicated by the same extension. For example, the <span class="file">.tgz</span> | |
213 | extension is mapped to <span class="file">.tar.gz</span> to allow the encoding and type | |
214 | to be recognized separately. | |
215 | </dd></dl> | |
216 | ||
217 | <P> | |
218 | <dl><dt><b><tt id='l2h-4030' xml:id='l2h-4030'>encodings_map</tt></b></dt> | |
219 | <dd> | |
220 | Dictionary mapping filename extensions to encoding types. | |
221 | </dd></dl> | |
222 | ||
223 | <P> | |
224 | <dl><dt><b><tt id='l2h-4031' xml:id='l2h-4031'>types_map</tt></b></dt> | |
225 | <dd> | |
226 | Dictionary mapping filename extensions to MIME types. | |
227 | </dd></dl> | |
228 | ||
229 | <P> | |
230 | <dl><dt><b><tt id='l2h-4032' xml:id='l2h-4032'>common_types</tt></b></dt> | |
231 | <dd> | |
232 | Dictionary mapping filename extensions to non-standard, but commonly | |
233 | found MIME types. | |
234 | </dd></dl> | |
235 | ||
236 | <P> | |
237 | The <tt class="class">MimeTypes</tt> class may be useful for applications which may | |
238 | want more than one MIME-type database: | |
239 | ||
240 | <P> | |
241 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
242 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-4033' xml:id='l2h-4033' class="class">MimeTypes</tt></b>(</nobr></td> | |
243 | <td><var></var><big>[</big><var>filenames</var><big>]</big><var></var>)</td></tr></table></dt> | |
244 | <dd> | |
245 | This class represents a MIME-types database. By default, it | |
246 | provides access to the same database as the rest of this module. | |
247 | The initial database is a copy of that provided by the module, and | |
248 | may be extended by loading additional <span class="file">mime.types</span>-style files | |
249 | into the database using the <tt class="method">read()</tt> or <tt class="method">readfp()</tt> | |
250 | methods. The mapping dictionaries may also be cleared before | |
251 | loading additional data if the default data is not desired. | |
252 | ||
253 | <P> | |
254 | The optional <var>filenames</var> parameter can be used to cause | |
255 | additional files to be loaded ``on top'' of the default database. | |
256 | ||
257 | <P> | |
258 | ||
259 | <span class="versionnote">New in version 2.2.</span> | |
260 | ||
261 | </dl> | |
262 | ||
263 | <P> | |
264 | ||
265 | <p><br /></p><hr class='online-navigation' /> | |
266 | <div class='online-navigation'> | |
267 | <!--Table of Child-Links--> | |
268 | <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a> | |
269 | ||
270 | <UL CLASS="ChildLinks"> | |
271 | <LI><A href="mimetypes-objects.html">12.7.1 MimeTypes Objects</a> | |
272 | </ul> | |
273 | <!--End of Table of Child-Links--> | |
274 | </div> | |
275 | ||
276 | <DIV CLASS="navigation"> | |
277 | <div class='online-navigation'> | |
278 | <p></p><hr /> | |
279 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
280 | <tr> | |
281 | <td class='online-navigation'><a rel="prev" title="12.6.1 Additional Methods of" | |
282 | href="mimetools-message-objects.html"><img src='../icons/previous.png' | |
283 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
284 | <td class='online-navigation'><a rel="parent" title="12. Internet Data Handling" | |
285 | href="netdata.html"><img src='../icons/up.png' | |
286 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
287 | <td class='online-navigation'><a rel="next" title="12.7.1 MimeTypes Objects" | |
288 | href="mimetypes-objects.html"><img src='../icons/next.png' | |
289 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
290 | <td align="center" width="100%">Python Library Reference</td> | |
291 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
292 | href="contents.html"><img src='../icons/contents.png' | |
293 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
294 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
295 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
296 | <td class='online-navigation'><a rel="index" title="Index" | |
297 | href="genindex.html"><img src='../icons/index.png' | |
298 | border='0' height='32' alt='Index' width='32' /></A></td> | |
299 | </tr></table> | |
300 | <div class='online-navigation'> | |
301 | <b class="navlabel">Previous:</b> | |
302 | <a class="sectref" rel="prev" href="mimetools-message-objects.html">12.6.1 Additional Methods of</A> | |
303 | <b class="navlabel">Up:</b> | |
304 | <a class="sectref" rel="parent" href="netdata.html">12. Internet Data Handling</A> | |
305 | <b class="navlabel">Next:</b> | |
306 | <a class="sectref" rel="next" href="mimetypes-objects.html">12.7.1 MimeTypes Objects</A> | |
307 | </div> | |
308 | </div> | |
309 | <hr /> | |
310 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
311 | </DIV> | |
312 | <!--End of Navigation Panel--> | |
313 | <ADDRESS> | |
314 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
315 | </ADDRESS> | |
316 | </BODY> | |
317 | </HTML> |