18.8. MimeWriter — Generic MIME file writer - Python 2.7.18 documentation 编辑

Deprecated since version 2.3: The email package should be used in preference to the MimeWriter module. This module is present only to maintain backward compatibility.

This module defines the class MimeWriter. The MimeWriter class implements a basic formatter for creating MIME multi-part files. It doesn’t seek around the output file nor does it use large amounts of buffer space. You must write the parts out in the order that they should occur in the final file. MimeWriter does buffer the headers you add, allowing you to rearrange their order.

class MimeWriter.MimeWriter(fp)

Return a new instance of the MimeWriter class. The only argument passed, fp, is a file object to be used for writing. Note that a StringIO object could also be used.

18.8.1. MimeWriter Objects

MimeWriter instances have the following methods:

MimeWriter.addheader(key, value[, prefix])

Add a header line to the MIME message. The key is the name of the header, where the value obviously provides the value of the header. The optional argument prefix determines where the header is inserted; 0 means append at the end, 1 is insert at the start. The default is to append.

MimeWriter.flushheaders()

Causes all headers accumulated so far to be written out (and forgotten). This is useful if you don’t need a body part at all, e.g. for a subpart of type message/rfc822 that’s (mis)used to store some header-like information.

MimeWriter.startbody(ctype[, plist[, prefix]])

Returns a file-like object which can be used to write to the body of the message. The content-type is set to the provided ctype, and the optional parameter plist provides additional parameters for the content-type declaration. prefix functions as in addheader() except that the default is to insert at the start.

MimeWriter.startmultipartbody(subtype[, boundary[, plist[, prefix]]])

Returns a file-like object which can be used to write to the body of the message. Additionally, this method initializes the multi-part code, where subtype provides the multipart subtype, boundary may provide a user-defined boundary specification, and plist provides optional parameters for the subtype. prefix functions as in startbody(). Subparts should be created using nextpart().

MimeWriter.nextpart()

Returns a new instance of MimeWriter which represents an individual part in a multipart message. This may be used to write the part as well as used for creating recursively complex multipart messages. The message must first be initialized with startmultipartbody() before using nextpart().

MimeWriter.lastpart()

This is used to designate the last part of a multipart message, and should always be used when writing multipart messages.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:34 次

字数:3608

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文