Fred Drake | 3a0351c | 1998-04-04 07:23:21 +0000 | [diff] [blame] | 1 | \section{Standard Module \module{rfc822}} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 2 | \label{module-rfc822} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 3 | \stmodindex{rfc822} |
| 4 | |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 5 | |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 6 | This module defines a class, \class{Message}, which represents a |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 7 | collection of ``email headers'' as defined by the Internet standard |
Fred Drake | c589124 | 1998-02-09 19:16:20 +0000 | [diff] [blame] | 8 | \rfc{822}. It is used in various contexts, usually to read such |
| 9 | headers from a file. |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 10 | |
Fred Drake | 5ca9033 | 1997-12-16 15:19:47 +0000 | [diff] [blame] | 11 | Note that there's a separate module to read \UNIX{}, MH, and MMDF |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 12 | style mailbox files: \module{mailbox}\refstmodindex{mailbox}. |
Guido van Rossum | 067a2ac | 1997-06-02 17:30:03 +0000 | [diff] [blame] | 13 | |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 14 | \begin{classdesc}{Message}{file\optional{, seekable}} |
| 15 | A \class{Message} instance is instantiated with an open file object as |
| 16 | parameter. The optional \var{seekable} parameter indicates if the |
| 17 | file object is seekable; the default value is \code{1} for true. |
Guido van Rossum | 067a2ac | 1997-06-02 17:30:03 +0000 | [diff] [blame] | 18 | Instantiation reads headers from the file up to a blank line and |
| 19 | stores them in the instance; after instantiation, the file is |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 20 | positioned directly after the blank line that terminates the headers. |
| 21 | |
| 22 | Input lines as read from the file may either be terminated by CR-LF or |
| 23 | by a single linefeed; a terminating CR-LF is replaced by a single |
| 24 | linefeed before the line is stored. |
| 25 | |
| 26 | All header matching is done independent of upper or lower case; |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 27 | e.g. \code{\var{m}['From']}, \code{\var{m}['from']} and |
| 28 | \code{\var{m}['FROM']} all yield the same result. |
| 29 | \end{classdesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 30 | |
Guido van Rossum | 843e712 | 1996-12-06 21:23:53 +0000 | [diff] [blame] | 31 | \begin{funcdesc}{parsedate}{date} |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 32 | Attempts to parse a date according to the rules in \rfc{822}. |
| 33 | however, some mailers don't follow that format as specified, so |
| 34 | \function{parsedate()} tries to guess correctly in such cases. |
Fred Drake | c589124 | 1998-02-09 19:16:20 +0000 | [diff] [blame] | 35 | \var{date} is a string containing an \rfc{822} date, such as |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 36 | \code{'Mon, 20 Nov 1995 19:12:08 -0500'}. If it succeeds in parsing |
| 37 | the date, \function{parsedate()} returns a 9-tuple that can be passed |
| 38 | directly to \function{time.mktime()}; otherwise \code{None} will be |
Guido van Rossum | 843e712 | 1996-12-06 21:23:53 +0000 | [diff] [blame] | 39 | returned. |
| 40 | \end{funcdesc} |
| 41 | |
| 42 | \begin{funcdesc}{parsedate_tz}{date} |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 43 | Performs the same function as \function{parsedate()}, but returns |
| 44 | either \code{None} or a 10-tuple; the first 9 elements make up a tuple |
| 45 | that can be passed directly to \function{time.mktime()}, and the tenth |
| 46 | is the offset of the date's timezone from UTC (which is the official |
| 47 | term for Greenwich Mean Time). (Note that the sign of the timezone |
| 48 | offset is the opposite of the sign of the \code{time.timezone} |
| 49 | variable for the same timezone; the latter variable follows the |
| 50 | \POSIX{} standard while this module follows \rfc{822}.) If the input |
| 51 | string has no timezone, the last element of the tuple returned is |
| 52 | \code{None}. |
Guido van Rossum | 843e712 | 1996-12-06 21:23:53 +0000 | [diff] [blame] | 53 | \end{funcdesc} |
| 54 | |
Guido van Rossum | 8cf94e6 | 1998-02-18 05:09:14 +0000 | [diff] [blame] | 55 | \begin{funcdesc}{mktime_tz}{tuple} |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 56 | Turn a 10-tuple as returned by \function{parsedate_tz()} into a UTC |
| 57 | timestamp. It the timezone item in the tuple is \code{None}, assume |
| 58 | local time. Minor deficiency: this first interprets the first 8 |
| 59 | elements as a local time and then compensates for the timezone |
| 60 | difference; this may yield a slight error around daylight savings time |
Guido van Rossum | 8cf94e6 | 1998-02-18 05:09:14 +0000 | [diff] [blame] | 61 | switch dates. Not enough to worry about for common use. |
| 62 | \end{funcdesc} |
| 63 | |
Guido van Rossum | ecde781 | 1995-03-28 13:35:14 +0000 | [diff] [blame] | 64 | \subsection{Message Objects} |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 65 | \label{message-objects} |
Guido van Rossum | ecde781 | 1995-03-28 13:35:14 +0000 | [diff] [blame] | 66 | |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 67 | A \class{Message} instance has the following methods: |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 68 | |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 69 | \begin{methoddesc}{rewindbody}{} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 70 | Seek to the start of the message body. This only works if the file |
| 71 | object is seekable. |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 72 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 73 | |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 74 | \begin{methoddesc}{getallmatchingheaders}{name} |
Guido van Rossum | 6c4f003 | 1995-03-07 10:14:09 +0000 | [diff] [blame] | 75 | Return a list of lines consisting of all headers matching |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 76 | \var{name}, if any. Each physical line, whether it is a continuation |
| 77 | line or not, is a separate list item. Return the empty list if no |
| 78 | header matches \var{name}. |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 79 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 80 | |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 81 | \begin{methoddesc}{getfirstmatchingheader}{name} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 82 | Return a list of lines comprising the first header matching |
| 83 | \var{name}, and its continuation line(s), if any. Return \code{None} |
| 84 | if there is no header matching \var{name}. |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 85 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 86 | |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 87 | \begin{methoddesc}{getrawheader}{name} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 88 | Return a single string consisting of the text after the colon in the |
| 89 | first header matching \var{name}. This includes leading whitespace, |
| 90 | the trailing linefeed, and internal linefeeds and whitespace if there |
| 91 | any continuation line(s) were present. Return \code{None} if there is |
| 92 | no header matching \var{name}. |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 93 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 94 | |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 95 | \begin{methoddesc}{getheader}{name} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 96 | Like \code{getrawheader(\var{name})}, but strip leading and trailing |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 97 | whitespace. Internal whitespace is not stripped. |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 98 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 99 | |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 100 | \begin{methoddesc}{getaddr}{name} |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 101 | Return a pair \code{(\var{full name}, \var{email address})} parsed |
| 102 | from the string returned by \code{getheader(\var{name})}. If no |
| 103 | header matching \var{name} exists, return \code{(None, None)}; |
| 104 | otherwise both the full name and the address are (possibly empty) |
| 105 | strings. |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 106 | |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 107 | Example: If \var{m}'s first \code{From} header contains the string |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 108 | \code{'jack@cwi.nl (Jack Jansen)'}, then |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 109 | \code{m.getaddr('From')} will yield the pair |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 110 | \code{('Jack Jansen', 'jack@cwi.nl')}. |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 111 | If the header contained |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 112 | \code{'Jack Jansen <jack@cwi.nl>'} instead, it would yield the |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 113 | exact same result. |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 114 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 115 | |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 116 | \begin{methoddesc}{getaddrlist}{name} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 117 | This is similar to \code{getaddr(\var{list})}, but parses a header |
| 118 | containing a list of email addresses (e.g. a \code{To} header) and |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 119 | returns a list of \code{(\var{full name}, \var{email address})} pairs |
| 120 | (even if there was only one address in the header). If there is no |
| 121 | header matching \var{name}, return an empty list. |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 122 | |
| 123 | XXX The current version of this function is not really correct. It |
| 124 | yields bogus results if a full name contains a comma. |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 125 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 126 | |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 127 | \begin{methoddesc}{getdate}{name} |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 128 | Retrieve a header using \method{getheader()} and parse it into a 9-tuple |
| 129 | compatible with \function{time.mktime()}. If there is no header matching |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 130 | \var{name}, or it is unparsable, return \code{None}. |
| 131 | |
| 132 | Date parsing appears to be a black art, and not all mailers adhere to |
| 133 | the standard. While it has been tested and found correct on a large |
| 134 | collection of email from many sources, it is still possible that this |
| 135 | function may occasionally yield an incorrect result. |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 136 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 137 | |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 138 | \begin{methoddesc}{getdate_tz}{name} |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 139 | Retrieve a header using \method{getheader()} and parse it into a |
| 140 | 10-tuple; the first 9 elements will make a tuple compatible with |
| 141 | \function{time.mktime()}, and the 10th is a number giving the offset |
| 142 | of the date's timezone from UTC. Similarly to \method{getdate()}, if |
Guido van Rossum | 843e712 | 1996-12-06 21:23:53 +0000 | [diff] [blame] | 143 | there is no header matching \var{name}, or it is unparsable, return |
| 144 | \code{None}. |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 145 | \end{methoddesc} |
Guido van Rossum | 843e712 | 1996-12-06 21:23:53 +0000 | [diff] [blame] | 146 | |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 147 | \class{Message} instances also support a read-only mapping interface. |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 148 | In particular: \code{\var{m}[name]} is like |
| 149 | \code{\var{m}.getheader(name)} but raises \exception{KeyError} if |
| 150 | there is no matching header; and \code{len(\var{m})}, |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 151 | \code{\var{m}.has_key(name)}, \code{\var{m}.keys()}, |
| 152 | \code{\var{m}.values()} and \code{\var{m}.items()} act as expected |
| 153 | (and consistently). |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 154 | |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 155 | Finally, \class{Message} instances have two public instance variables: |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 156 | |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 157 | \begin{memberdesc}{headers} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 158 | A list containing the entire set of header lines, in the order in |
| 159 | which they were read. Each line contains a trailing newline. The |
| 160 | blank line terminating the headers is not contained in the list. |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 161 | \end{memberdesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 162 | |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 163 | \begin{memberdesc}{fp} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 164 | The file object passed at instantiation time. |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 165 | \end{memberdesc} |