Movatterモバイル変換


[0]ホーム

URL:


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:12.10.2 MultiFile ExampleUp:12. Internet Data HandlingNext:12.11.1 Message Objects

12.11rfc822 -- Parse RFC 2822 mail headers

Deprecated since release 2.3.Theemail package should be used in preference to therfc822 module. This module is present only to maintain backward compatibility.

This module defines a class,Message, which represents an``email message'' as defined by the Internet standardRFC 2822.12.5 Such messagesconsist of a collection of message headers, and a message body. Thismodule also defines a helper classAddressList for parsingRFC 2822 addresses. Please refer tothe RFC for information on the specific syntax ofRFC 2822 messages.

Themailbox module provides classes to read mailboxes produced by various end-user mail programs.

classMessage(file[, seekable])
AMessage instance is instantiated with an input object asparameter. Message relies only on the input object having areadline() method; in particular, ordinary file objectsqualify. Instantiation reads headers from the input object up to adelimiter line (normally a blank line) and stores them in theinstance. The message body, following the headers, is not consumed.

This class can work with any input object that supports areadline() method. If the input object has seek and tellcapability, therewindbody() method will work; also, illegallines will be pushed back onto the input stream. If the input objectlacks seek but has anunread() method that can push back aline of input,Message will use that to push back illegallines. Thus this class can be used to parse messages coming from abuffered stream.

The optionalseekable argument is provided as a workaround forcertain stdio libraries in whichtell() discards buffereddata before discovering that thelseek() system calldoesn't work. For maximum portability, you should set the seekableargument to zero to prevent that initialtell() when passingin an unseekable object such as a a file object created from a socketobject.

Input lines as read from the file may either be terminated by CR-LF orby a single linefeed; a terminating CR-LF is replaced by a singlelinefeed before the line is stored.

All header matching is done independent of upper or lower case;e.g.m['From'],m['from'] andm['FROM'] all yield the same result.

classAddressList(field)
You may instantiate theAddressList helper class using a singlestring parameter, a comma-separated list ofRFC 2822 addresses to beparsed. (The parameterNone yields an empty list.)

quote(str)
Return a new string with backslashes instr replaced by twobackslashes and double quotes replaced by backslash-double quote.

unquote(str)
Return a new string which is anunquoted version ofstr.Ifstr ends and begins with double quotes, they are strippedoff. Likewise ifstr ends and begins with angle brackets, theyare stripped off.

parseaddr(address)
Parseaddress, which should be the value of someaddress-containing field such asTo: orCc:,into its constituent ``realname'' and ``email address'' parts.Returns a tuple of that information, unless the parse fails, in whichcase a 2-tuple(None, None) is returned.

dump_address_pair(pair)
The inverse ofparseaddr(), this takes a 2-tuple of the form(realname,email_address) and returns the stringvalue suitable for aTo: orCc: header. Ifthe first element ofpair is false, then the second element isreturned unmodified.

parsedate(date)
Attempts to parse a date according to the rules inRFC 2822.however, some mailers don't follow that format as specified, soparsedate() tries to guess correctly in such cases.date is a string containing anRFC 2822 date, such as'Mon, 20 Nov 1995 19:12:08 -0500'. If it succeeds in parsingthe date,parsedate() returns a 9-tuple that can be passeddirectly totime.mktime(); otherwiseNone will bereturned. Note that fields 6, 7, and 8 of the result tuple are notusable.

parsedate_tz(date)
Performs the same function asparsedate(), but returnseitherNone or a 10-tuple; the first 9 elements make up a tuplethat can be passed directly totime.mktime(), and the tenthis the offset of the date's timezone from UTC (which is the officialterm for Greenwich Mean Time). (Note that the sign of the timezoneoffset is the opposite of the sign of thetime.timezonevariable for the same timezone; the latter variable follows thePOSIX standard while this module followsRFC 2822.) If the inputstring has no timezone, the last element of the tuple returned isNone. Note that fields 6, 7, and 8 of the result tuple are notusable.

mktime_tz(tuple)
Turn a 10-tuple as returned byparsedate_tz() into a UTCtimestamp. If the timezone item in the tuple isNone, assumelocal time. Minor deficiency: this first interprets the first 8elements as a local time and then compensates for the timezonedifference; this may yield a slight error around daylight savings timeswitch dates. Not enough to worry about for common use.

See Also:

Moduleemail:
Comprehensive email handling package; supercedes therfc822 module.
Modulemailbox:
Classes to read various mailbox formats produced by end-user mail programs.
Modulemimetools:
Subclass of rfc.Message that handles MIME encoded messages.



Footnotes

...2822.12.5
This module originally conformed toRFC 822,hence the name. Since then,RFC 2822 has been released as anupdate toRFC 822. This module should be consideredRFC 2822-conformant, especially in cases where thesyntax or semantics have changed sinceRFC 822.


Subsections


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:12.10.2 MultiFile ExampleUp:12. Internet Data HandlingNext:12.11.1 Message Objects
Release 2.2.3, documentation updated on 30 May 2003.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2026 Movatter.jp