couchdb.multipart

Package couchdb :: Module multipart |

# Module multipart

Support for streamed reading and writing of multipart MIME content.

Functions
read_multipart(fileobj, boundary=None) Simple streaming MIME multipart parser. |
write_multipart(fileobj, subtype=`'``mixed``'`, boundary=None) Simple streaming MIME multipart writer. |

Function Details

### read_multipart(fileobj, boundary=None) |
Simple streaming MIME multipart parser. This function takes a file-like object reading a MIME envelope, and yields a `(headers, is_multipart, payload)` tuple for every part found, where `headers` is a dictionary containing the MIME headers of that part (with names lower-cased), `is_multipart` is a boolean indicating whether the part is itself multipart, and `payload` is either a string (if `is_multipart` is false), or an iterator over the nested parts. Note that the iterator produced for nested multipart payloads MUST be fully consumed, even if you wish to skip over the content. Parameters: `fileobj` - a file-like object `boundary` - the part boundary string, will generally be determined automatically from the headers of the outermost multipart envelope Returns: an iterator over the parts Since: 0.5

### write_multipart(fileobj, subtype=`'``mixed``'`, boundary=None) |
Simple streaming MIME multipart writer. This function returns a MultipartWriter object that has a few methods to control the nested MIME parts. For example, to write a flat multipart envelope you call the `add(mimetype, content, [headers])` method for every part, and finally call the `close()` method.
    >>> from StringIO import StringIO
    >>> buf = StringIO()
    >>> envelope = write_multipart(buf, boundary='==123456789==')
    >>> envelope.add('text/plain', 'Just testing')
    >>> envelope.close()
    >>> print buf.getvalue().replace('\r\n', '\n')
    Content-Type: multipart/mixed; boundary="==123456789=="
    <BLANKLINE>
    --==123456789==
    Content-Length: 12
    Content-MD5: nHmX4a6el41B06x2uCpglQ==
    Content-Type: text/plain
    <BLANKLINE>
    Just testing
    --==123456789==--
    <BLANKLINE>
Note that an explicit boundary is only specified for testing purposes. If the `boundary` parameter is omitted, the multipart writer will generate a random string for the boundary. To write nested structures, call the `open([headers])` method on the respective envelope, and finish each envelope using the `close()` method:
    >>> buf = StringIO()
    >>> envelope = write_multipart(buf, boundary='==123456789==')
    >>> part = envelope.open(boundary='==abcdefghi==')
    >>> part.add('text/plain', 'Just testing')
    >>> part.close()
    >>> envelope.close()
    >>> print buf.getvalue().replace('\r\n', '\n') #:doctest +ELLIPSIS
    Content-Type: multipart/mixed; boundary="==123456789=="
    <BLANKLINE>
    --==123456789==
    Content-Type: multipart/mixed; boundary="==abcdefghi=="
    <BLANKLINE>
    --==abcdefghi==
    Content-Length: 12
    Content-MD5: nHmX4a6el41B06x2uCpglQ==
    Content-Type: text/plain
    <BLANKLINE>
    Just testing
    --==abcdefghi==--
    --==123456789==--
    <BLANKLINE>
Parameters: `fileobj` - a writable file-like object that the output should get written to `subtype` - the subtype of the multipart MIME type (e.g. "mixed") `boundary` - the boundary to use to separate the different parts Since: 0.6