mime module

Synopsis

The mime module provides a simple MIME-parsing class. It is only included because the standard Python mimetools/multifile libraries are too buggy to use for parsing multipart/form-data HTTP requests.

class: Error(Exception)

The base class for all exceptions defined by the mime module.

class: Entity(rfc822.Message)

Entity objects provide information about a MIME entity. You are not expected to create objects of this class yourself, but you will encounter them in the cgi.Request.params map if you use multipart/form-data HTTP requests.

Public Instance Variables

mime

mime will reference a true value if this entity is a MIME entity (as opposed to a standard RFC822 entity) - i.e. if it has a MIME-Version header or it is a MIME multipart section.

content_type

This variable is only present if mime is true. If it is present, and the entity had no Content-Type header, it will be None. Otherwise, it will be a sequence containing two values. The first is the type of the entity, and the second is a map containing any additional parameters specified. Parameter names (but not values) are converted to lower-case before being entered into the map.

Example:

>>> e.content_type
('multipart/form-data', {'boundary': 'AaB03x'})

content_disposition

This variable is only present if mime is true. If it is present, and the entity had no Content-Disposition header, it will be None. Otherwise, it will be a sequence containing two values. The first is the disposition of the entity, and the second is a map containing any additional parameters specified. Parameter names (but not values) are converted to lower-case before being entered into the map.

Example:

>>> e.entities[0].content_disposition
('form-data', {'name': 'pics', 'filename': 'file1.txt'})

entities

This variable is only present if mime is true. If it is present, it will be a sequence of zero or more mime.Entity objects representing the MIME entities contained directly within this one. If this MIME entity cannot or does not contain any other MIME entities (i.e. if it is not a multipart message) then the sequence will be empty.

Note that this sequence only lists entities contained directly within this one, not entities contained within those entities. Thus to find all the entities contained within an entity you need to recursively scan the entities sequence in each entity found.

body

This contains the body of the entity. If a Content-Transfer-Encoding was used then it will have been decoded. If this entity is a MIME multipart then body will only contain the data preceding the first boundary.

Example:

>>> e.entities[0].body
'foo'

$Id$