source: trunk/essentials/dev-lang/python/Doc/lib/emailparser.tex@ 3368

Last change on this file since 3368 was 3225, checked in by bird, 19 years ago

Python 2.5

File size: 9.8 KB
Line 
1\declaremodule{standard}{email.parser}
2\modulesynopsis{Parse flat text email messages to produce a message
3 object structure.}
4
5Message object structures can be created in one of two ways: they can be
6created from whole cloth by instantiating \class{Message} objects and
7stringing them together via \method{attach()} and
8\method{set_payload()} calls, or they can be created by parsing a flat text
9representation of the email message.
10
11The \module{email} package provides a standard parser that understands
12most email document structures, including MIME documents. You can
13pass the parser a string or a file object, and the parser will return
14to you the root \class{Message} instance of the object structure. For
15simple, non-MIME messages the payload of this root object will likely
16be a string containing the text of the message. For MIME
17messages, the root object will return \code{True} from its
18\method{is_multipart()} method, and the subparts can be accessed via
19the \method{get_payload()} and \method{walk()} methods.
20
21There are actually two parser interfaces available for use, the classic
22\class{Parser} API and the incremental \class{FeedParser} API. The classic
23\class{Parser} API is fine if you have the entire text of the message in
24memory as a string, or if the entire message lives in a file on the file
25system. \class{FeedParser} is more appropriate for when you're reading the
26message from a stream which might block waiting for more input (e.g. reading
27an email message from a socket). The \class{FeedParser} can consume and parse
28the message incrementally, and only returns the root object when you close the
29parser\footnote{As of email package version 3.0, introduced in
30Python 2.4, the classic \class{Parser} was re-implemented in terms of the
31\class{FeedParser}, so the semantics and results are identical between the two
32parsers.}.
33
34Note that the parser can be extended in limited ways, and of course
35you can implement your own parser completely from scratch. There is
36no magical connection between the \module{email} package's bundled