Registry / http-networking / zope-contenttype

zope-contenttype

JSON →
library6.0pypypi✓ verified 84d ago

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.

pip install zope.contenttype
INSTALL
IMPORT
SIG · ZOPE-CONTENTTYPE
Z
zope-contenttype
http-networkingpythonv6.0
Install
1.8s avg
Import
10ms
Disk
19MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v6.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.010s · 17.9MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 1.8s · import 0.010s · 18MB
19MB installed
● package 19MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

guess_content_type
from zope.contenttype import guess_content_type
parse_content_type
from zope.contenttype import parse_content_type
ContentType
from zope.contenttype import ContentType
from zope.contenttype.contenttype import ContentType
The ContentType class is directly importable from the top-level package in recent versions.

Demonstrates how to guess content types using filenames and binary data, and how to parse a full content type string into its components. This covers the most common use cases for `zope.contenttype`.

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}")
Debug
Known issues
breakingVersion 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.
fix
Ensure your build tools and environment support PEP 420 native namespace packages. For `zc.buildout`, upgrade to version 5 or newer. Review any explicit `namespace_packages` configurations in `setup.py` files if upgrading from very old versions.
affects: >=6.0
breakingVersion 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.
fix
Upgrade your Python environment to version 3.9 or higher to use `zope.contenttype` version 6.0. Python 3.9 is the minimum required version for `zope.contenttype` 6.0.
affects: >=5.0
breakingIn 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.
fix
Ensure that the `file_contents` argument passed to `guess_content_type` is always a `bytes` object (e.g., `b'your data'`) when running on Python 3.
affects: >=4.0.1
gotchaStarting 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.
fix
If your application relies on a stricter content type guessing, you may need to implement custom logic or override `guess_content_type` to call `mimetypes.guess_type(strict=True)` directly or filter its results.
affects: >=5.2
Errors
Common errors & fixes
TypeError: a bytes-like object is required, not 'str'
Attempting to pass a Python 3 string object as the `file_contents` argument to `guess_content_type` instead of a bytes object.
fix
Convert the string to bytes before passing it, e.g., `guess_content_type(filename, my_string.encode('utf-8'))`.
ModuleNotFoundError: No module named 'zope.contenttype'
The `zope.contenttype` package is not installed in the current Python environment or the environment is not correctly activated.
fix
Install 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)
Running `zope.contenttype` 6.0 with an older buildout version (e.g., `<5`) or an environment not compatible with PEP 420 native namespace packages.
fix
Update 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.
Upgrade
Version history
6.0latest on PyPI · released Sep 12, 2025
Audit
Dependencies

No dependency data recorded yet.

Agent activity
57 hits · last 30 days
node
46
OpenAI (training)
1
Resources
zope-contenttype — pip install zope-contenttype · libregistry