1 \section{\module{mimetypes
} ---
2 Map filenames to MIME types
}
4 \declaremodule{standard
}{mimetypes
}
5 \modulesynopsis{Mapping of filename extensions to MIME types.
}
6 \sectionauthor{Fred L. Drake, Jr.
}{fdrake@acm.org
}
9 \indexii{MIME
}{content type
}
11 The
\module{mimetypes
} module converts between a filename or URL and
12 the MIME type associated with the filename extension. Conversions are
13 provided from filename to MIME type and from MIME type to filename
14 extension; encodings are not supported for the latter conversion.
16 The module provides one class and a number of convenience functions.
17 The functions are the normal interface to this module, but some
18 applications may be interested in the class as well.
20 The functions described below provide the primary interface for this
21 module. If the module has not been initialized, they will call
22 \function{init()
} if they rely on the information
\function{init()
}
26 \begin{funcdesc
}{guess_type
}{filename
\optional{, strict
}}
27 Guess the type of a file based on its filename or URL, given by
28 \var{filename
}. The return value is a tuple
\code{(
\var{type
},
29 \var{encoding
})
} where
\var{type
} is
\code{None
} if the type can't be
30 guessed (missing or unknown suffix) or a string of the form
31 \code{'
\var{type
}/
\var{subtype
}'
}, usable for a MIME
32 \mailheader{content-type
} header
\indexii{MIME
}{headers
}.
34 \var{encoding
} is
\code{None
} for no encoding or the name of the
35 program used to encode (e.g.
\program{compress
} or
\program{gzip
}).
36 The encoding is suitable for use as a
\mailheader{Content-Encoding
}
37 header,
\emph{not
} as a
\mailheader{Content-Transfer-Encoding
} header.
38 The mappings are table driven. Encoding suffixes are case sensitive;
39 type suffixes are first tried case sensitively, then case
42 Optional
\var{strict
} is a flag specifying whether the list of known
43 MIME types is limited to only the official types
\ulink{registered
44 with IANA
}{http://www.isi.edu/in-notes/iana/assignments/media-types
}
45 are recognized. When
\var{strict
} is true (the default), only the
46 IANA types are supported; when
\var{strict
} is false, some additional
47 non-standard but commonly used MIME types are also recognized.
50 \begin{funcdesc
}{guess_all_extensions
}{type
\optional{, strict
}}
51 Guess the extensions for a file based on its MIME type, given by
53 The return value is a list of strings giving all possible filename extensions,
54 including the leading dot (
\character{.
}). The extensions are not guaranteed
55 to have been associated with any particular data stream, but would be mapped
56 to the MIME type
\var{type
} by
\function{guess_type()
}.
58 Optional
\var{strict
} has the same meaning as with the
59 \function{guess_type()
} function.
63 \begin{funcdesc
}{guess_extension
}{type
\optional{, strict
}}
64 Guess the extension for a file based on its MIME type, given by
66 The return value is a string giving a filename extension, including the
67 leading dot (
\character{.
}). The extension is not guaranteed to have been
68 associated with any particular data stream, but would be mapped to the
69 MIME type
\var{type
} by
\function{guess_type()
}. If no extension can
70 be guessed for
\var{type
},
\code{None
} is returned.
72 Optional
\var{strict
} has the same meaning as with the
73 \function{guess_type()
} function.
77 Some additional functions and data items are available for controlling
78 the behavior of the module.
81 \begin{funcdesc
}{init
}{\optional{files
}}
82 Initialize the internal data structures. If given,
\var{files
} must
83 be a sequence of file names which should be used to augment the
84 default type map. If omitted, the file names to use are taken from
85 \constant{knownfiles
}. Each file named in
\var{files
} or
86 \constant{knownfiles
} takes precedence over those named before it.
87 Calling
\function{init()
} repeatedly is allowed.
90 \begin{funcdesc
}{read_mime_types
}{filename
}
91 Load the type map given in the file
\var{filename
}, if it exists. The
92 type map is returned as a dictionary mapping filename extensions,
93 including the leading dot (
\character{.
}), to strings of the form
94 \code{'
\var{type
}/
\var{subtype
}'
}. If the file
\var{filename
} does
95 not exist or cannot be read,
\code{None
} is returned.
99 \begin{funcdesc
}{add_type
}{type, ext
\optional{, strict
}}
100 Add a mapping from the mimetype
\var{type
} to the extension
\var{ext
}.
101 When the extension is already known, the new type will replace the old
102 one. When the type is already known the extension will be added
103 to the list of known extensions.
105 When
\var{strict
} is the mapping will added to the official
106 MIME types, otherwise to the non-standard ones.
110 \begin{datadesc
}{inited
}
111 Flag indicating whether or not the global data structures have been
112 initialized. This is set to true by
\function{init()
}.
115 \begin{datadesc
}{knownfiles
}
116 List of type map file names commonly installed. These files are
117 typically named
\file{mime.types
} and are installed in different
118 locations by different packages.
\index{file!mime.types
}
121 \begin{datadesc
}{suffix_map
}
122 Dictionary mapping suffixes to suffixes. This is used to allow
123 recognition of encoded files for which the encoding and the type are
124 indicated by the same extension. For example, the
\file{.tgz
}
125 extension is mapped to
\file{.tar.gz
} to allow the encoding and type
126 to be recognized separately.
129 \begin{datadesc
}{encodings_map
}
130 Dictionary mapping filename extensions to encoding types.
133 \begin{datadesc
}{types_map
}
134 Dictionary mapping filename extensions to MIME types.
137 \begin{datadesc
}{common_types
}
138 Dictionary mapping filename extensions to non-standard, but commonly
143 The
\class{MimeTypes
} class may be useful for applications which may
144 want more than one MIME-type database:
146 \begin{classdesc
}{MimeTypes
}{\optional{filenames
}}
147 This class represents a MIME-types database. By default, it
148 provides access to the same database as the rest of this module.
149 The initial database is a copy of that provided by the module, and
150 may be extended by loading additional
\file{mime.types
}-style files
151 into the database using the
\method{read()
} or
\method{readfp()
}
152 methods. The mapping dictionaries may also be cleared before
153 loading additional data if the default data is not desired.
155 The optional
\var{filenames
} parameter can be used to cause
156 additional files to be loaded ``on top'' of the default database.
161 An example usage of the module:
166 >>> mimetypes.knownfiles
167 ['/etc/mime.types', '/etc/httpd/mime.types', ...
]
168 >>> mimetypes.suffix_map
['.tgz'
]
170 >>> mimetypes.encodings_map
['.gz'
]
172 >>> mimetypes.types_map
['.tgz'
]
173 'application/x-tar-gz'
176 \subsection{MimeTypes Objects
\label{mimetypes-objects
}}
178 \class{MimeTypes
} instances provide an interface which is very like
179 that of the
\refmodule{mimetypes
} module.
181 \begin{datadesc
}{suffix_map
}
182 Dictionary mapping suffixes to suffixes. This is used to allow
183 recognition of encoded files for which the encoding and the type are
184 indicated by the same extension. For example, the
\file{.tgz
}
185 extension is mapped to
\file{.tar.gz
} to allow the encoding and type
186 to be recognized separately. This is initially a copy of the global
187 \code{suffix_map
} defined in the module.
190 \begin{datadesc
}{encodings_map
}
191 Dictionary mapping filename extensions to encoding types. This is
192 initially a copy of the global
\code{encodings_map
} defined in the
196 \begin{datadesc
}{types_map
}
197 Dictionary mapping filename extensions to MIME types. This is
198 initially a copy of the global
\code{types_map
} defined in the
202 \begin{datadesc
}{common_types
}
203 Dictionary mapping filename extensions to non-standard, but commonly
204 found MIME types. This is initially a copy of the global
205 \code{common_types
} defined in the module.
208 \begin{methoddesc
}{guess_extension
}{type
\optional{, strict
}}
209 Similar to the
\function{guess_extension()
} function, using the
210 tables stored as part of the object.
213 \begin{methoddesc
}{guess_type
}{url
\optional{, strict
}}
214 Similar to the
\function{guess_type()
} function, using the tables
215 stored as part of the object.
218 \begin{methoddesc
}{read
}{path
}
219 Load MIME information from a file named
\var{path
}. This uses
220 \method{readfp()
} to parse the file.
223 \begin{methoddesc
}{readfp
}{file
}
224 Load MIME type information from an open file. The file must have
225 the format of the standard
\file{mime.types
} files.