API¶
There are two major features provided by this package: determining content types, and parsing text representations of content types into structured data.
Note
When this module is imported, it uses add_files()
to
extend the standard mimetypes
database with a set of common
office and multimedia types and filename extensions.
Tip
When used as the main module (e.g., python -m
zope.contenttype
) this module will print out the
mimetypes
database.
Determining Content Types¶
- zope.contenttype.guess_content_type(name='', body=b'', default=None)[source]¶
Given a named piece of content, try to guess its content type.
The implementation relies on the
mimetypes
standard Python module, thetext_type()
function also defined in this module, and a simple heuristic for detecting binary data.- Returns:
A tuple of
(type, encoding)
likemimetypes.guess_type()
.- Parameters:
- zope.contenttype.text_type(s)[source]¶
Given an unnamed piece of data, try to guess its content type.
Detects HTML, XML, and plain text.
Utility Functions¶
- zope.contenttype.add_files(filenames)[source]¶
Add the names of MIME type map files to the standard
mimetypes
module.MIME type map files are used for detecting the MIME type of some content based on the content’s filename extension.
The files should be formatted similarly to the ‘mime.types’ file included in this package. Each line specifies a MIME type and the file extensions that imply that MIME type. Here are some sample lines:
text/css css text/plain bat c h pl ksh text/x-vcard vcf
Parsing Text Representations¶
MIME Content-Type parsing helper functions.
This supports parsing RFC 1341 Content-Type values, including quoted-string values as defined in RFC 822.