Zope ContentType

6.0 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

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}")

view raw JSON →