On this page
18.6. mimetools-解析 MIME 消息的工具
从版本 2.3 开始不推荐使用:email软件包应优先于mimetools模块使用。仅提供此模块是为了保持向后兼容性,在 3.x 中已将其删除。
此模块定义rfc822模块的Message类的子类和许多 Util Function,这些Function对于处理 MIME 多重部分或编码的消息很有用。
它定义了以下项目:
-
- class *
mimetools.
Message
(* fp * [,* seekable *])
- 返回Message类的新实例。这是rfc822.Message类的子类,带有一些其他方法(请参见下文)。 * seekable *参数与rfc822.Message的含义相同。
- class *
mimetools.
choose_boundary
( )- 返回一个唯一的字符串,该字符串很有可能用作 Component 边界。该字符串的格式为
'hostipaddr.uid.pid.timestamp.random'
。
- 返回一个唯一的字符串,该字符串很有可能用作 Component 边界。该字符串的格式为
mimetools.
decode
(* input , output , encoding *)- 从打开的文件对象* input *读取使用允许的 MIME * encoding 编码的数据,并将解码的数据写入打开的文件对象 output *。 * encoding *的有效值包括
'base64'
,'quoted-printable'
,'uuencode'
,'x-uuencode'
,'uue'
,'x-uue'
,'7bit'
和'8bit'
。解码以'7bit'
或'8bit'
编码的消息无效。Importing 只是复制到输出中。
- 从打开的文件对象* input *读取使用允许的 MIME * encoding 编码的数据,并将解码的数据写入打开的文件对象 output *。 * encoding *的有效值包括
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
Module email
全面的电子邮件处理包;取代mimetools模块。
Module rfc822
提供mimetools.Message的 Base Class。
Module multifile
支持读取包含不同部分(例如 MIME 数据)的文件。
MIME 常见问题文档。有关 MIME 的概述,请参阅本文档第 1 部分中对问题 1.1 的回答。
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']
。
- 返回* Content-Type Headers 的参数列表。这是一个字符串列表。对于
Message.
getparam
(* name *)- 以给定的* name 返回形式
name=value
的第一个参数(由getplist()返回)的 value 。如果 value *用引号“<
…>
”或“"
…"
”括起来,则将其删除。
- 以给定的* name 返回形式
Message.
getencoding
( )- 返回在* Content-Transfer-Encoding *消息头中指定的编码。如果不存在这样的 Headers,则返回
'7bit'
。编码被转换为小写。
- 返回在* Content-Transfer-Encoding *消息头中指定的编码。如果不存在这样的 Headers,则返回
Message.
gettype
( )- 返回* Content-Type *Headers 中指定的消息类型(格式为
type/subtype
)。如果不存在这样的 Headers,则返回'text/plain'
。类型将转换为小写。
- 返回* Content-Type *Headers 中指定的消息类型(格式为
Message.
getmaintype
( )- 返回* Content-Type *Headers 中指定的主要类型。如果不存在这样的 Headers,则返回
'text'
。主类型转换为小写。
- 返回* Content-Type *Headers 中指定的主要类型。如果不存在这样的 Headers,则返回
Message.
getsubtype
( )- 返回* Content-Type *Headers 中指定的子类型。如果不存在这样的 Headers,则返回
'plain'
。子类型将转换为小写。
- 返回* Content-Type *Headers 中指定的子类型。如果不存在这样的 Headers,则返回