This module contains representations of internet media type strings.
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')])
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
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()
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()
[]
Just the type/subtype pair of this content type.
>>> ContentType('text/html; charset=utf-8').media_type
'text/html'
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.