class email.parser.Parser(_class=email.message.Message, *, policy=policy.compat32)
The constructor for the Parser
class takes an optional argument _class. This must be a callable factory (such as a function or a class), and it is used whenever a sub-message object needs to be created. It defaults to Message
(see email.message
). The factory will be called without arguments.
If policy is specified (it must be an instance of a policy
class) use the rules it specifies to update the representation of the message. If policy is not set, use the compat32
policy, which maintains backward compatibility with the Python 3.2 version of the email package. For more information see the policy
documentation.
Changed in version 3.3: Removed the strict argument that was deprecated in 2.4. Added the policy keyword.
The other public Parser
methods are:
-
parse(fp, headersonly=False)
-
Read all the data from the file-like object fp, parse the resulting text, and return the root message object. fp must support both the
readline()
and theread()
methods on file-like objects.The text contained in fp must be formatted as a block of RFC 2822 style headers and header continuation lines, optionally preceded by an envelope header. The header block is terminated either by the end of the data or by a blank line. Following the header block is the body of the message (which may contain MIME-encoded subparts).
Optional headersonly is a flag specifying whether to stop parsing after reading the headers or not. The default is
False
, meaning it parses the entire contents of the file.
-
parsestr(text, headersonly=False)
-
Similar to the
parse()
method, except it takes a string object instead of a file-like object. Calling this method on a string is exactly equivalent to wrapping text in aStringIO
instance first and callingparse()
.Optional headersonly is as with the
parse()
method.
Please login to continue.