Sophie

Sophie

distrib > Mandriva > 8.2 > i586 > by-pkgid > 0b7eb7009605a11593fbe388d7fbee61 > files > 493

python-docs-2.2-9.1mdk.i586.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>12.7 mimetypes -- Map filenames to MIME types</title>
<META NAME="description" CONTENT="12.7 mimetypes -- Map filenames to MIME types">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=">
<link rel="STYLESHEET" href="lib.css">
<link rel="first" href="lib.html">
<link rel="contents" href="contents.html" title="Contents">
<link rel="index" href="genindex.html" title="Index">
<LINK REL="next" href="module-MimeWriter.html">
<LINK REL="previous" href="module-mimetools.html">
<LINK REL="up" href="netdata.html">
<LINK REL="next" href="mimetypes-objects.html">
</head>
<body>
<DIV CLASS="navigation">
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="mimetools-message-objects.html"><img src="../icons/previous.gif"
  border="0" height="32"
  alt="Previous Page" width="32"></A></td>
<td><A href="netdata.html"><img src="../icons/up.gif"
  border="0" height="32"
  alt="Up One Level" width="32"></A></td>
<td><A href="mimetypes-objects.html"><img src="../icons/next.gif"
  border="0" height="32"
  alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html"><img src="../icons/contents.gif"
  border="0" height="32"
  alt="Contents" width="32"></A></td>
<td><a href="modindex.html" title="Module Index"><img src="../icons/modules.gif"
  border="0" height="32"
  alt="Module Index" width="32"></a></td>
<td><A href="genindex.html"><img src="../icons/index.gif"
  border="0" height="32"
  alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="mimetools-message-objects.html">12.6.1 Additional Methods of</A>
<b class="navlabel">Up:</b> <a class="sectref" href="netdata.html">12. Internet Data Handling</A>
<b class="navlabel">Next:</b> <a class="sectref" href="mimetypes-objects.html">12.7.1 MimeTypes Objects</A>
<br><hr>
</DIV>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION0014700000000000000000">
12.7 <tt class="module">mimetypes</tt> --
         Map filenames to MIME types</A>
</H1>

<P>


<P>
<a name="l2h-2936">&nbsp;</a>
<P>
The <tt class="module">mimetypes</tt> module converts between a filename or URL and
the MIME type associated with the filename extension.  Conversions are
provided from filename to MIME type and from MIME type to filename
extension; encodings are not supported for the latter conversion.

<P>
The module provides one class and a number of convenience functions.
The functions are the normal interface to this module, but some
applications may be interested in the class as well.

<P>
The functions described below provide the primary interface for this
module.  If the module has not been initialized, they will call
<tt class="function">init()</tt> if they rely on the information <tt class="function">init()</tt>
sets up.

<P>
<dl><dt><b><a name="l2h-2925"><tt class="function">guess_type</tt></a></b>(<var>filename</var><big>[</big><var>, strict</var><big>]</big>)
<dd>
Guess the type of a file based on its filename or URL, given by
<var>filename</var>.  The return value is a tuple <code>(<var>type</var>,
<var>encoding</var>)</code> where <var>type</var> is <code>None</code> if the type can't be
guessed (missing or unknown suffix) or a string of the form
<code>'<var>type</var>/<var>subtype</var>'</code>, usable for a MIME
<span class="mailheader">content-type:</span> header<a name="l2h-2937">&nbsp;</a>.

<P>
<var>encoding</var> is <code>None</code> for no encoding or the name of the
program used to encode (e.g. <b class="program">compress</b> or <b class="program">gzip</b>).
The encoding is suitable for use as a <span class="mailheader">Content-Encoding:</span>
header, <i>not</i> as a <span class="mailheader">Content-Transfer-Encoding:</span> header.
The mappings are table driven.  Encoding suffixes are case sensitive;
type suffixes are first tried case sensitively, then case
insensitively.

<P>
Optional <var>strict</var> is a flag specifying whether the list of known
MIME types is limited to only the official types <a class="ulink" href="http://www.isi.edu/in-notes/iana/assignments/media-types"
  >registered
with IANA</a>
are recognized.  When <var>strict</var> is true (the default), only the
IANA types are supported; when <var>strict</var> is false, some additional
non-standard but commonly used MIME types are also recognized.
</dl>

<P>
<dl><dt><b><a name="l2h-2926"><tt class="function">guess_extension</tt></a></b>(<var>type</var><big>[</big><var>, strict</var><big>]</big>)
<dd>
Guess the extension for a file based on its MIME type, given by
<var>type</var>.
The return value is a string giving a filename extension, including the
leading dot ("<tt class="character">.</tt>").  The extension is not guaranteed to have been
associated with any particular data stream, but would be mapped to the 
MIME type <var>type</var> by <tt class="function">guess_type()</tt>.  If no extension can
be guessed for <var>type</var>, <code>None</code> is returned.

<P>
Optional <var>strict</var> has the same meaning as with the
<tt class="function">guess_type()</tt> function.
</dl>

<P>
Some additional functions and data items are available for controlling
the behavior of the module.

<P>
<dl><dt><b><a name="l2h-2927"><tt class="function">init</tt></a></b>(<big>[</big><var>files</var><big>]</big>)
<dd>
Initialize the internal data structures.  If given, <var>files</var> must
be a sequence of file names which should be used to augment the
default type map.  If omitted, the file names to use are taken from
<tt class="constant">knownfiles</tt>.  Each file named in <var>files</var> or
<tt class="constant">knownfiles</tt> takes precedence over those named before it.
Calling <tt class="function">init()</tt> repeatedly is allowed.
</dl>

<P>
<dl><dt><b><a name="l2h-2928"><tt class="function">read_mime_types</tt></a></b>(<var>filename</var>)
<dd>
Load the type map given in the file <var>filename</var>, if it exists.  The 
type map is returned as a dictionary mapping filename extensions,
including the leading dot ("<tt class="character">.</tt>"), to strings of the form
<code>'<var>type</var>/<var>subtype</var>'</code>.  If the file <var>filename</var> does
not exist or cannot be read, <code>None</code> is returned.
</dl>

<P>
<dl><dt><b><a name="l2h-2929"><tt>inited</tt></a></b>
<dd>
Flag indicating whether or not the global data structures have been
initialized.  This is set to true by <tt class="function">init()</tt>.
</dl>

<P>
<dl><dt><b><a name="l2h-2930"><tt>knownfiles</tt></a></b>
<dd>
List of type map file names commonly installed.  These files are
typically named <span class="file">mime.types</span> and are installed in different
locations by different packages.<a name="l2h-2938">&nbsp;</a></dl>

<P>
<dl><dt><b><a name="l2h-2931"><tt>suffix_map</tt></a></b>
<dd>
Dictionary mapping suffixes to suffixes.  This is used to allow
recognition of encoded files for which the encoding and the type are
indicated by the same extension.  For example, the <span class="file">.tgz</span>
extension is mapped to <span class="file">.tar.gz</span> to allow the encoding and type
to be recognized separately.
</dl>

<P>
<dl><dt><b><a name="l2h-2932"><tt>encodings_map</tt></a></b>
<dd>
Dictionary mapping filename extensions to encoding types.
</dl>

<P>
<dl><dt><b><a name="l2h-2933"><tt>types_map</tt></a></b>
<dd>
Dictionary mapping filename extensions to MIME types.
</dl>

<P>
<dl><dt><b><a name="l2h-2934"><tt>common_types</tt></a></b>
<dd>
Dictionary mapping filename extensions to non-standard, but commonly
found MIME types.
</dl>

<P>
The <tt class="class">MimeTypes</tt> class may be useful for applications which may
want more than one MIME-type database:

<P>
<dl><dt><b><span class="typelabel">class</span> <a name="l2h-2935"><tt class="class">MimeTypes</tt></a></b>(<big>[</big><var>filenames</var><big>]</big>)
<dd>
  This class represents a MIME-types database.  By default, it
  provides access to the same database as the rest of this module.
  The initial database is a copy of that provided by the module, and
  may be extended by loading additional <span class="file">mime.types</span>-style files
  into the database using the <tt class="method">read()</tt> or <tt class="method">readfp()</tt>
  methods.  The mapping dictionaries may also be cleared before
  loading additional data if the default data is not desired.

<P>
The optional <var>filenames</var> parameter can be used to cause
  additional files to be loaded ``on top'' of the default database.

<P>

<span class="versionnote">New in version 2.2.</span>

</dl>

<P>

<p><hr>
<!--Table of Child-Links-->
<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a>

<UL CLASS="ChildLinks">
<LI><A href="mimetypes-objects.html">12.7.1 MimeTypes Objects</a>
</ul>
<!--End of Table of Child-Links-->

<DIV CLASS="navigation">
<p><hr>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="mimetools-message-objects.html"><img src="../icons/previous.gif"
  border="0" height="32"
  alt="Previous Page" width="32"></A></td>
<td><A href="netdata.html"><img src="../icons/up.gif"
  border="0" height="32"
  alt="Up One Level" width="32"></A></td>
<td><A href="mimetypes-objects.html"><img src="../icons/next.gif"
  border="0" height="32"
  alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html"><img src="../icons/contents.gif"
  border="0" height="32"
  alt="Contents" width="32"></A></td>
<td><a href="modindex.html" title="Module Index"><img src="../icons/modules.gif"
  border="0" height="32"
  alt="Module Index" width="32"></a></td>
<td><A href="genindex.html"><img src="../icons/index.gif"
  border="0" height="32"
  alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="mimetools-message-objects.html">12.6.1 Additional Methods of</A>
<b class="navlabel">Up:</b> <a class="sectref" href="netdata.html">12. Internet Data Handling</A>
<b class="navlabel">Next:</b> <a class="sectref" href="mimetypes-objects.html">12.7.1 MimeTypes Objects</A>
<hr>
<span class="release-info">Release 2.2, documentation updated on December 21, 2001.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>