18.6. mimetools-解析 MIME 消息的工具

从版本 2.3 开始不推荐使用:email软件包应优先于mimetools模块使用。仅提供此模块是为了保持向后兼容性,在 3.x 中已将其删除。

此模块定义rfc822模块的Message类的子类和许多 Util Function,这些Function对于处理 MIME 多重部分或编码的消息很有用。

它定义了以下项目:

    • class * mimetools. Message(* fp * [,* seekable *])
  • mimetools. choose_boundary ( )

    • 返回一个唯一的字符串,该字符串很有可能用作 Component 边界。该字符串的格式为'hostipaddr.uid.pid.timestamp.random'
  • mimetools. decode(* input output encoding *)

    • 从打开的文件对象* input *读取使用允许的 MIME * encoding 编码的数据,并将解码的数据写入打开的文件对象 output *。 * encoding *的有效值包括'base64''quoted-printable''uuencode''x-uuencode''uue''x-uue''7bit''8bit'。解码以'7bit''8bit'编码的消息无效。Importing 只是复制到输出中。
  • mimetools. encode(* input output encoding *)

    • 从打开的文件对象* input *读取数据,并使用允许的 MIME * encoding 对其进行编码,以打开文件对象 output *。 * encoding *的有效值与decode()相同。
  • mimetools. copyliteral(* input output *)

    • 从打开文件* input 读取行,直到 EOF,然后将它们写入打开文件 output *。
  • mimetools. copybinary(* input output *)

    • 从打开的文件* input 读取块直到 EOF,然后将它们写入打开的文件 output *。块大小当前固定为 8192.

See also

18.6.1. 消息对象的其他方法

Message类除了rfc822.Message方法之外,还定义了以下方法:

  • Message. getplist ( )

    • 返回* Content-Type Headers 的参数列表。这是一个字符串列表。对于key=value形式的参数, key 将转换为小写字母,但 value *不会转换为小写字母。例如,如果消息包含标题Content-type: text/html; spam=1; Spam=2; Spam,则getplist()将返回 Python 列表['spam=1', 'spam=2', 'Spam']
  • Message. getparam(* name *)

    • 以给定的* name 返回形式name=value的第一个参数(由getplist()返回)的 value 。如果 value *用引号“ <>”或“ ""”括起来,则将其删除。
  • Message. getencoding ( )

    • 返回在* Content-Transfer-Encoding *消息头中指定的编码。如果不存在这样的 Headers,则返回'7bit'。编码被转换为小写。
  • Message. gettype ( )

    • 返回* Content-Type *Headers 中指定的消息类型(格式为type/subtype)。如果不存在这样的 Headers,则返回'text/plain'。类型将转换为小写。
  • Message. getmaintype ( )

    • 返回* Content-Type *Headers 中指定的主要类型。如果不存在这样的 Headers,则返回'text'。主类型转换为小写。
  • Message. getsubtype ( )

    • 返回* Content-Type *Headers 中指定的子类型。如果不存在这样的 Headers,则返回'plain'。子类型将转换为小写。