Zope ContentType
Zope ContentType is a utility module within the Zope ecosystem designed for handling MIME types, particularly the 'Content-Type' HTTP header. It provides functionalities for guessing content types from filenames and (optional) data, determining content types from text, and parsing MIME type strings. Currently at version 6.0, the library follows semantic versioning with new minor releases approximately every 2-6 months and major releases every 2-3 years, in line with the broader Zope project.
Common errors
-
TypeError: a bytes-like object is required, not 'str'
cause Attempting to pass a Python 3 string object as the `file_contents` argument to `guess_content_type` instead of a bytes object.fixConvert the string to bytes before passing it, e.g., `guess_content_type(filename, my_string.encode('utf-8'))`. -
ModuleNotFoundError: No module named 'zope.contenttype'
cause The `zope.contenttype` package is not installed in the current Python environment or the environment is not correctly activated.fixInstall the package using pip: `pip install zope.contenttype`. If using a virtual environment, ensure it is activated. -
SyntaxError: invalid syntax (related to namespace packages or pkg_resources)
cause Running `zope.contenttype` 6.0 with an older buildout version (e.g., `<5`) or an environment not compatible with PEP 420 native namespace packages.fixUpdate your `zc.buildout` to version 5 or newer, or ensure your project setup correctly handles PEP 420 namespace packages. This generally means removing `namespace_packages` from `setup.py` for modern Python versions.
Warnings
- breaking Version 6.0 replaced `pkg_resources` namespace with PEP 420 native namespaces. This change might impact older `zc.buildout` configurations (prior to version 5) that rely on `pkg_resources` for namespace package discovery.
- breaking Version 5.0 dropped support for Python 2.7, 3.5, and 3.6. Version 5.2 further dropped support for Python 3.7 and 3.8.
- breaking In version 4.0.1, the `file_contents` argument of `guess_content_type` was changed from expecting a string to expecting bytes in Python 3. Passing a string will result in a TypeError.
- gotcha Starting with version 5.2, `zope.contenttype` calls the standard library's `mimetypes.guess_type` with the parameter `strict=False`. This change allows the recognition of more content types (e.g., midi, pict, xul, rtf) that might have been ignored in previous versions. This could subtly change content type guessing behavior.
Install
-
pip install zope.contenttype
Imports
- guess_content_type
from zope.contenttype import guess_content_type
- parse_content_type
from zope.contenttype import parse_content_type
- ContentType
from zope.contenttype.contenttype import ContentType
from zope.contenttype import ContentType
Quickstart
from zope.contenttype import guess_content_type, parse_content_type
# Guess content type from a filename and optional data
filename = 'report.pdf'
content_data = b'%PDF-1.4...'
content_type, encoding = guess_content_type(filename, content_data)
print(f"Guessed content type for '{filename}': {content_type}, encoding: {encoding}")
filename_text = 'document.txt'
text_data = b'This is a plain text document.'
text_type, text_encoding = guess_content_type(filename_text, text_data)
print(f"Guessed content type for '{filename_text}': {text_type}, encoding: {text_encoding}")
# Parse a content type string
content_type_str = 'text/html; charset=utf-8'
parsed_type = parse_content_type(content_type_str)
print(f"Parsed content type '{content_type_str}':")
print(f" Primary type: {parsed_type.media_type.primary}")
print(f" Sub type: {parsed_type.media_type.sub}")
print(f" Charset: {parsed_type.charset}")
# Example of guessing from text (no filename hint)
html_snippet = b'<html><body>Hello</body></html>'
html_type, _ = guess_content_type(None, html_snippet)
print(f"Guessed content type for HTML snippet: {html_type}")