Changeset 391 for python/trunk/Doc/library/email.parser.rst
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Doc/library/email.parser.rst
r2 r391 1 :mod:`email `: Parsing email messages2 ------------------------------------ 1 :mod:`email.parser`: Parsing email messages 2 ------------------------------------------- 3 3 4 4 .. module:: email.parser … … 8 8 Message object structures can be created in one of two ways: they can be created 9 9 from whole cloth by instantiating :class:`~email.message.Message` objects and 10 stringing them together via :meth:`attach` and :meth:`set_payload` calls, or they 10 stringing them together via :meth:`~email.message.Message.attach` and 11 :meth:`~email.message.Message.set_payload` calls, or they 11 12 can be created by parsing a flat text representation of the email message. 12 13 … … 17 18 non-MIME messages the payload of this root object will likely be a string 18 19 containing the text of the message. For MIME messages, the root object will 19 return ``True`` from its :meth:`is_multipart` method, and the subparts can be 20 accessed via the :meth:`get_payload` and :meth:`walk` methods. 20 return ``True`` from its :meth:`~email.message.Message.is_multipart` method, and 21 the subparts can be accessed via the :meth:`~email.message.Message.get_payload` 22 and :meth:`~email.message.Message.walk` methods. 21 23 22 24 There are actually two parser interfaces available for use, the classic … … 128 130 Read all the data from the file-like object *fp*, parse the resulting 129 131 text, and return the root message object. *fp* must support both the 130 :meth:`readline` and the :meth:`read` methods on file-like objects. 132 :meth:`~io.TextIOBase.readline` and the :meth:`~io.TextIOBase.read` 133 methods on file-like objects. 131 134 132 135 The text contained in *fp* must be formatted as a block of :rfc:`2822` … … 136 139 message (which may contain MIME-encoded subparts). 137 140 138 Optional *headersonly* is as with the :meth:`parse` method.139 140 .. versionchanged:: 2.2.2141 The *headersonly* flag was added.142 143 144 .. method:: parsestr(text[, headersonly])145 146 Similar to the :meth:`parse` method, except it takes a string object147 instead of a file-like object. Calling this method on a string is exactly148 equivalent to wrapping *text* in a :class:`StringIO` instance first and149 calling :meth:`parse`.150 151 141 Optional *headersonly* is a flag specifying whether to stop parsing after 152 142 reading the headers or not. The default is ``False``, meaning it parses … … 156 146 The *headersonly* flag was added. 157 147 148 149 .. method:: parsestr(text[, headersonly]) 150 151 Similar to the :meth:`parse` method, except it takes a string object 152 instead of a file-like object. Calling this method on a string is exactly 153 equivalent to wrapping *text* in a :class:`~StringIO.StringIO` instance first and 154 calling :meth:`parse`. 155 156 Optional *headersonly* is as with the :meth:`parse` method. 157 158 .. versionchanged:: 2.2.2 159 The *headersonly* flag was added. 160 158 161 Since creating a message object structure from a string or a file object is such 159 162 a common task, two functions are provided as a convenience. They are available … … 166 169 Return a message object structure from a string. This is exactly equivalent to 167 170 ``Parser().parsestr(s)``. Optional *_class* and *strict* are interpreted as 168 with the :class:` Parser` class constructor.171 with the :class:`~email.parser.Parser` class constructor. 169 172 170 173 .. versionchanged:: 2.2.2 … … 176 179 Return a message object structure tree from an open file object. This is 177 180 exactly equivalent to ``Parser().parse(fp)``. Optional *_class* and *strict* 178 are interpreted as with the :class:` Parser` class constructor.181 are interpreted as with the :class:`~email.parser.Parser` class constructor. 179 182 180 183 .. versionchanged:: 2.2.2 … … 194 197 * Most non-\ :mimetype:`multipart` type messages are parsed as a single message 195 198 object with a string payload. These objects will return ``False`` for 196 :meth:` is_multipart`. Their :meth:`get_payload` method will return a string197 object.199 :meth:`~email.message.Message.is_multipart`. Their 200 :meth:`~email.message.Message.get_payload` method will return a string object. 198 201 199 202 * All :mimetype:`multipart` type messages will be parsed as a container message 200 203 object with a list of sub-message objects for their payload. The outer 201 container message will return ``True`` for :meth:`is_multipart` and their 202 :meth:`get_payload` method will return the list of :class:`~email.message.Message` 203 subparts. 204 container message will return ``True`` for 205 :meth:`~email.message.Message.is_multipart` and their 206 :meth:`~email.message.Message.get_payload` method will return the list of 207 :class:`~email.message.Message` subparts. 204 208 205 209 * Most messages with a content type of :mimetype:`message/\*` (e.g. 206 210 :mimetype:`message/delivery-status` and :mimetype:`message/rfc822`) will also be 207 211 parsed as container object containing a list payload of length 1. Their 208 :meth:` is_multipart` method will return ``True``. The single element in the209 list payload will be a sub-message object.212 :meth:`~email.message.Message.is_multipart` method will return ``True``. 213 The single element in the list payload will be a sub-message object. 210 214 211 215 * Some non-standards compliant messages may not be internally consistent about 212 216 their :mimetype:`multipart`\ -edness. Such messages may have a 213 217 :mailheader:`Content-Type` header of type :mimetype:`multipart`, but their 214 :meth:`is_multipart` method may return ``False``. If such messages were parsed 215 with the :class:`FeedParser`, they will have an instance of the 216 :class:`MultipartInvariantViolationDefect` class in their *defects* attribute 217 list. See :mod:`email.errors` for details. 218 :meth:`~email.message.Message.is_multipart` method may return ``False``. 219 If such messages were parsed with the :class:`~email.parser.FeedParser`, 220 they will have an instance of the 221 :class:`~email.errors.MultipartInvariantViolationDefect` class in their 222 *defects* attribute list. See :mod:`email.errors` for details. 218 223 219 224 .. rubric:: Footnotes 220 225 221 226 .. [#] As of email package version 3.0, introduced in Python 2.4, the classic 222 :class:`Parser` was re-implemented in terms of the :class:`FeedParser`, so the 223 semantics and results are identical between the two parsers. 224 227 :class:`~email.parser.Parser` was re-implemented in terms of the 228 :class:`~email.parser.FeedParser`, so the semantics and results are 229 identical between the two parsers. 230
Note:
See TracChangeset
for help on using the changeset viewer.