Module mimetypes
Guess the MIME type of a file.
This module defines two useful functions:
guess_type(url, strict=1) -- guess the MIME type and encoding of a
URL.
guess_extension(type, strict=1) -- guess the extension for a given
MIME type.
It also contains the following, for tuning the behavior:
Data:
knownfiles -- list of files to parse inited -- flag set when init()
has been called suffix_map -- dictionary mapping suffixes to suffixes
encodings_map -- dictionary mapping suffixes to encodings types_map --
dictionary mapping suffixes to types
Functions:
init([files]) -- parse a list of files, default knownfiles
read_mime_types(file) -- parse one file, return a dictionary or None
|
guess_type(url,
strict=True)
Guess the type of a file based on its URL. |
|
|
|
|
|
guess_extension(type,
strict=True)
Guess the extension for a file based on its MIME type. |
|
|
|
add_type(type,
ext,
strict=True)
Add a mapping between a type and an extension. |
|
|
|
|
|
|
|
|
|
knownfiles = [ ' /etc/mime.types ' , ' /etc/httpd/mime.types ' , ' /et ...
|
|
inited = True
|
|
common_types = { ' .jpg ' : ' image/jpg ' , ' .mid ' : ' audio/midi ' , ' .m ...
|
|
encodings_map = { ' .Z ' : ' compress ' , ' .gz ' : ' gzip ' }
|
|
suffix_map = { ' .taz ' : ' .tar.gz ' , ' .tgz ' : ' .tar.gz ' , ' .tz ' : ' .t ...
|
|
types_map = { ' .% ' : ' application/x-trash ' , ' .323 ' : ' text/h323 ' , ...
|
Imports:
os,
posixpath,
urllib
guess_type(url,
strict=True)
|
|
Guess the type of a file based on its URL.
Return value is a tuple (type, encoding) where type is None if the
type can't be guessed (no or unknown suffix) or a string of the form
type/subtype, usable for a MIME Content-type header; and encoding is None
for no encoding or the name of the program used to encode (e.g. compress
or gzip). The mappings are table driven. Encoding suffixes are case
sensitive; type suffixes are first tried case sensitive, then case
insensitive.
The suffixes .tgz, .taz and .tz (case sensitive!) are all mapped to
'.tar.gz'. (This is table-driven too, using the dictionary
suffix_map.)
Optional `strict' argument when False adds a bunch of commonly found,
but non-standard types.
|
guess_all_extensions(type,
strict=True)
|
|
Guess the extensions for a file based on its MIME type.
Return value is a list of strings giving the possible filename
extensions, including the leading dot ('.'). The extension is not
guaranteed to have been associated with any particular data stream, but
would be mapped to the MIME type `type' by guess_type().
Optional `strict' argument when false adds a bunch of commonly found,
but non-standard types.
|
guess_extension(type,
strict=True)
|
|
Guess the extension for a file based on its MIME type.
Return value is a string giving a filename extension, including the
leading dot ('.'). The extension is not guaranteed to have been
associated with any particular data stream, but would be mapped to the
MIME type `type' by guess_type(). If no extension can be guessed for
`type', None is returned.
Optional `strict' argument when false adds a bunch of commonly found,
but non-standard types.
|
add_type(type,
ext,
strict=True)
|
|
Add a mapping between a type and an extension.
When the extension is already known, the new type will replace the old
one. When the type is already known the extension will be added to the
list of known extensions.
If strict is true, information will be added to list of standard
types, else to the list of non-standard types.
|
knownfiles
- Value:
[ ' /etc/mime.types ' ,
' /etc/httpd/mime.types ' ,
' /etc/httpd/conf/mime.types ' ,
' /etc/apache/mime.types ' ,
' /etc/apache2/mime.types ' ,
' /usr/local/etc/httpd/conf/mime.types ' ,
' /usr/local/lib/netscape/mime.types ' ,
' /usr/local/etc/httpd/conf/mime.types ' ,
...
|
|
common_types
- Value:
{ ' .jpg ' : ' image/jpg ' ,
' .mid ' : ' audio/midi ' ,
' .midi ' : ' audio/midi ' ,
' .pct ' : ' image/pict ' ,
' .pic ' : ' image/pict ' ,
' .pict ' : ' image/pict ' ,
' .rtf ' : ' application/rtf ' ,
' .xul ' : ' text/xul ' }
|
|
suffix_map
- Value:
{ ' .taz ' : ' .tar.gz ' , ' .tgz ' : ' .tar.gz ' , ' .tz ' : ' .tar.gz ' }
|
|
types_map
- Value:
{ ' .% ' : ' application/x-trash ' ,
' .323 ' : ' text/h323 ' ,
' .3gp ' : ' video/3gpp ' ,
' .7z ' : ' application/x-7z-compressed ' ,
' .a ' : ' application/octet-stream ' ,
' .abw ' : ' application/x-abiword ' ,
' .ai ' : ' application/postscript ' ,
' .aif ' : ' audio/x-aiff ' ,
...
|
|