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, the text_type() function also defined in this module, and a simple heuristic for detecting binary data.

Returns:

A tuple of (type, encoding) like mimetypes.guess_type().

Parameters:
  • name (str) – The file name for the content. This is given priority when guessing the type.

  • body (bytes) – The binary data of the content.

  • default (str) – If given, and no content type can be guessed, this will be returned as the type portion.

zope.contenttype.text_type(s)[source]

Given an unnamed piece of data, try to guess its content type.

Detects HTML, XML, and plain text.

Returns:

A MIME type string such as ‘text/html’.

Return type:

str

Parameters:

s (bytes) – The binary data to examine.

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.