intessa.conneg.content_type — Represent internet media types

This module contains representations of internet media type strings.

class intessa.conneg.content_type.ContentType[source]

An HTTP Content-Type header.

This is a subclass of str, not unicode, in accordance with RFC 2616 (http://www.ietf.org/rfc/rfc2616), which states that only US-ASCII is to be used in Content-Type headers.

Create a header from a bytestring:

>>> c_type = ContentType('text/html; charset=utf-8')
>>> c_type
ContentType('text/html; charset=utf-8')

Access the media type on its own:

>>> c_type.media_type
'text/html'

And the parameters, as an ordered dictionary:

>>> c_type.params
odict([('charset', 'utf-8')])
__eq__(other)[source]

Determine equality, preserving order and ignoring whitespace.

>>> ContentType('text/html') == 'text/html'
True
>>> ContentType('text/html; k=v') == 'text/html; k=v'
True
>>> ContentType('text/html; a=1; b=2') == 'text/html; b=2; a=1'
False
>>> ContentType('text/html  ; a=1 ; b=2') == 'text/html; a=1; b=2'
True
guess_extension(db=<mimetypes.MimeTypes instance at 0x1026e9ea8>)[source]

Return a likely file extension for this media type, or None.

Parameters:db – An instance of mimetypes.MimeTypes; if unspecified, will use the global default.
Returns:A single bytestring or None.

Example:

>>> ContentType('text/html').guess_extension()
'.html'
>>> ContentType('application/x-will-not-match').guess_extension()
guess_extensions(db=<mimetypes.MimeTypes instance at 0x1026e9ea8>)[source]

Return a list of likely file extensions for this media type.

Parameters:db – An instance of mimetypes.MimeTypes; if unspecified, will use the global default.
Returns:A list of bytestrings, which will be empty if no matches were found.

Example:

>>> ContentType('text/html').guess_extensions()
['.html', '.htm']
>>> ContentType('application/x-will-not-match').guess_extensions()
[]
media_type[source]

Just the type/subtype pair of this content type.

>>> ContentType('text/html; charset=utf-8').media_type
'text/html'
params[source]

An ordered dict of the key=value parameters of this header.

>>> ContentType('text/html; charset=utf-8').params
odict([('charset', 'utf-8')])
>>> ContentType('text/html; charset=utf-8').params['charset']
'utf-8'
>>> ContentType('text/html; charset=utf-8; boundary=xxx').params
odict([('charset', 'utf-8'), ('boundary', 'xxx')])

Note that each time you access this attribute a new dictionary will be created; modifications are unrecommended and will not persist.

Previous topic

intessa.conneg.codec_base — Base classes for building codecs

Next topic

intessa.conneg.accept — Utilities for manipulating HTTP Accept headers

This Page