Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{rfc822} --- |
Fred Drake | ffbe687 | 1999-04-22 21:23:22 +0000 | [diff] [blame] | 2 | Parse RFC 822 mail headers} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 3 | |
Fred Drake | ffbe687 | 1999-04-22 21:23:22 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{rfc822} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 5 | \modulesynopsis{Parse \rfc{822} style mail headers.} |
| 6 | |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 7 | This module defines a class, \class{Message}, which represents a |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 8 | collection of ``email headers'' as defined by the Internet standard |
Fred Drake | c589124 | 1998-02-09 19:16:20 +0000 | [diff] [blame] | 9 | \rfc{822}. It is used in various contexts, usually to read such |
Guido van Rossum | 8729483 | 1998-06-16 22:27:40 +0000 | [diff] [blame] | 10 | headers from a file. This module also defines a helper class |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 11 | \class{AddressList} for parsing \rfc{822} addresses. Please refer to |
| 12 | the RFC for information on the specific syntax of \rfc{822} headers. |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 13 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 14 | The \refmodule{mailbox}\refstmodindex{mailbox} module provides classes |
| 15 | to read mailboxes produced by various end-user mail programs. |
Guido van Rossum | 067a2ac | 1997-06-02 17:30:03 +0000 | [diff] [blame] | 16 | |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 17 | \begin{classdesc}{Message}{file\optional{, seekable}} |
Guido van Rossum | 1299100 | 1998-06-10 21:34:27 +0000 | [diff] [blame] | 18 | A \class{Message} instance is instantiated with an input object as |
| 19 | parameter. Message relies only on the input object having a |
Fred Drake | 23329d4 | 1998-08-10 17:46:22 +0000 | [diff] [blame] | 20 | \method{readline()} method; in particular, ordinary file objects |
| 21 | qualify. Instantiation reads headers from the input object up to a |
| 22 | delimiter line (normally a blank line) and stores them in the |
Eric S. Raymond | e7213c7 | 2001-01-27 10:56:14 +0000 | [diff] [blame] | 23 | instance. The message body, following the headers, is not consumed. |
Guido van Rossum | 1299100 | 1998-06-10 21:34:27 +0000 | [diff] [blame] | 24 | |
Fred Drake | 23329d4 | 1998-08-10 17:46:22 +0000 | [diff] [blame] | 25 | This class can work with any input object that supports a |
| 26 | \method{readline()} method. If the input object has seek and tell |
| 27 | capability, the \method{rewindbody()} method will work; also, illegal |
| 28 | lines will be pushed back onto the input stream. If the input object |
| 29 | lacks seek but has an \method{unread()} method that can push back a |
| 30 | line of input, \class{Message} will use that to push back illegal |
| 31 | lines. Thus this class can be used to parse messages coming from a |
| 32 | buffered stream. |
Guido van Rossum | 1299100 | 1998-06-10 21:34:27 +0000 | [diff] [blame] | 33 | |
Fred Drake | 23329d4 | 1998-08-10 17:46:22 +0000 | [diff] [blame] | 34 | The optional \var{seekable} argument is provided as a workaround for |
| 35 | certain stdio libraries in which \cfunction{tell()} discards buffered |
| 36 | data before discovering that the \cfunction{lseek()} system call |
| 37 | doesn't work. For maximum portability, you should set the seekable |
| 38 | argument to zero to prevent that initial \method{tell()} when passing |
| 39 | in an unseekable object such as a a file object created from a socket |
| 40 | object. |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 41 | |
| 42 | Input lines as read from the file may either be terminated by CR-LF or |
| 43 | by a single linefeed; a terminating CR-LF is replaced by a single |
| 44 | linefeed before the line is stored. |
| 45 | |
| 46 | All header matching is done independent of upper or lower case; |
Fred Drake | 23329d4 | 1998-08-10 17:46:22 +0000 | [diff] [blame] | 47 | e.g.\ \code{\var{m}['From']}, \code{\var{m}['from']} and |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 48 | \code{\var{m}['FROM']} all yield the same result. |
| 49 | \end{classdesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 50 | |
Guido van Rossum | 8729483 | 1998-06-16 22:27:40 +0000 | [diff] [blame] | 51 | \begin{classdesc}{AddressList}{field} |
Fred Drake | ae0f292 | 1999-06-10 15:03:07 +0000 | [diff] [blame] | 52 | You may instantiate the \class{AddressList} helper class using a single |
Fred Drake | 23329d4 | 1998-08-10 17:46:22 +0000 | [diff] [blame] | 53 | string parameter, a comma-separated list of \rfc{822} addresses to be |
| 54 | parsed. (The parameter \code{None} yields an empty list.) |
Guido van Rossum | 8729483 | 1998-06-16 22:27:40 +0000 | [diff] [blame] | 55 | \end{classdesc} |
| 56 | |
Guido van Rossum | 843e712 | 1996-12-06 21:23:53 +0000 | [diff] [blame] | 57 | \begin{funcdesc}{parsedate}{date} |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 58 | Attempts to parse a date according to the rules in \rfc{822}. |
| 59 | however, some mailers don't follow that format as specified, so |
| 60 | \function{parsedate()} tries to guess correctly in such cases. |
Fred Drake | c589124 | 1998-02-09 19:16:20 +0000 | [diff] [blame] | 61 | \var{date} is a string containing an \rfc{822} date, such as |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 62 | \code{'Mon, 20 Nov 1995 19:12:08 -0500'}. If it succeeds in parsing |
| 63 | the date, \function{parsedate()} returns a 9-tuple that can be passed |
| 64 | directly to \function{time.mktime()}; otherwise \code{None} will be |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 65 | returned. Note that fields 6, 7, and 8 of the result tuple are not |
| 66 | usable. |
Guido van Rossum | 843e712 | 1996-12-06 21:23:53 +0000 | [diff] [blame] | 67 | \end{funcdesc} |
| 68 | |
| 69 | \begin{funcdesc}{parsedate_tz}{date} |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 70 | Performs the same function as \function{parsedate()}, but returns |
| 71 | either \code{None} or a 10-tuple; the first 9 elements make up a tuple |
| 72 | that can be passed directly to \function{time.mktime()}, and the tenth |
| 73 | is the offset of the date's timezone from UTC (which is the official |
| 74 | term for Greenwich Mean Time). (Note that the sign of the timezone |
| 75 | offset is the opposite of the sign of the \code{time.timezone} |
| 76 | variable for the same timezone; the latter variable follows the |
| 77 | \POSIX{} standard while this module follows \rfc{822}.) If the input |
| 78 | string has no timezone, the last element of the tuple returned is |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 79 | \code{None}. Note that fields 6, 7, and 8 of the result tuple are not |
| 80 | usable. |
Guido van Rossum | 843e712 | 1996-12-06 21:23:53 +0000 | [diff] [blame] | 81 | \end{funcdesc} |
| 82 | |
Guido van Rossum | 8cf94e6 | 1998-02-18 05:09:14 +0000 | [diff] [blame] | 83 | \begin{funcdesc}{mktime_tz}{tuple} |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 84 | Turn a 10-tuple as returned by \function{parsedate_tz()} into a UTC |
| 85 | timestamp. It the timezone item in the tuple is \code{None}, assume |
| 86 | local time. Minor deficiency: this first interprets the first 8 |
| 87 | elements as a local time and then compensates for the timezone |
| 88 | difference; this may yield a slight error around daylight savings time |
Guido van Rossum | 8cf94e6 | 1998-02-18 05:09:14 +0000 | [diff] [blame] | 89 | switch dates. Not enough to worry about for common use. |
| 90 | \end{funcdesc} |
| 91 | |
Fred Drake | ea00205 | 1999-04-28 18:11:09 +0000 | [diff] [blame] | 92 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 93 | \begin{seealso} |
| 94 | \seemodule{mailbox}{Classes to read various mailbox formats produced |
| 95 | by end-user mail programs.} |
Skip Montanaro | 6634b14 | 2000-09-15 18:20:20 +0000 | [diff] [blame] | 96 | \seemodule{mimetools}{Subclass of rfc.Message that handles MIME encoded |
| 97 | messages.} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 98 | \end{seealso} |
| 99 | |
| 100 | |
Fred Drake | ea00205 | 1999-04-28 18:11:09 +0000 | [diff] [blame] | 101 | \subsection{Message Objects \label{message-objects}} |
Guido van Rossum | ecde781 | 1995-03-28 13:35:14 +0000 | [diff] [blame] | 102 | |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 103 | A \class{Message} instance has the following methods: |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 104 | |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 105 | \begin{methoddesc}{rewindbody}{} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 106 | Seek to the start of the message body. This only works if the file |
| 107 | object is seekable. |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 108 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 109 | |
Guido van Rossum | 444d0f8 | 1998-06-11 13:50:02 +0000 | [diff] [blame] | 110 | \begin{methoddesc}{isheader}{line} |
| 111 | Returns a line's canonicalized fieldname (the dictionary key that will |
Fred Drake | ea00205 | 1999-04-28 18:11:09 +0000 | [diff] [blame] | 112 | be used to index it) if the line is a legal \rfc{822} header; otherwise |
Guido van Rossum | 444d0f8 | 1998-06-11 13:50:02 +0000 | [diff] [blame] | 113 | returns None (implying that parsing should stop here and the line be |
| 114 | pushed back on the input stream). It is sometimes useful to override |
| 115 | this method in a subclass. |
| 116 | \end{methoddesc} |
| 117 | |
Guido van Rossum | 1299100 | 1998-06-10 21:34:27 +0000 | [diff] [blame] | 118 | \begin{methoddesc}{islast}{line} |
| 119 | Return true if the given line is a delimiter on which Message should |
Guido van Rossum | 444d0f8 | 1998-06-11 13:50:02 +0000 | [diff] [blame] | 120 | stop. The delimiter line is consumed, and the file object's read |
| 121 | location positioned immediately after it. By default this method just |
| 122 | checks that the line is blank, but you can override it in a subclass. |
Guido van Rossum | 1299100 | 1998-06-10 21:34:27 +0000 | [diff] [blame] | 123 | \end{methoddesc} |
| 124 | |
| 125 | \begin{methoddesc}{iscomment}{line} |
| 126 | Return true if the given line should be ignored entirely, just skipped. |
| 127 | By default this is a stub that always returns false, but you can |
| 128 | override it in a subclass. |
| 129 | \end{methoddesc} |
| 130 | |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 131 | \begin{methoddesc}{getallmatchingheaders}{name} |
Guido van Rossum | 6c4f003 | 1995-03-07 10:14:09 +0000 | [diff] [blame] | 132 | Return a list of lines consisting of all headers matching |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 133 | \var{name}, if any. Each physical line, whether it is a continuation |
| 134 | line or not, is a separate list item. Return the empty list if no |
| 135 | header matches \var{name}. |
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}{getfirstmatchingheader}{name} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 139 | Return a list of lines comprising the first header matching |
Fred Drake | ea00205 | 1999-04-28 18:11:09 +0000 | [diff] [blame] | 140 | \var{name}, and its continuation line(s), if any. Return |
| 141 | \code{None} if there is no header matching \var{name}. |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 142 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 143 | |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 144 | \begin{methoddesc}{getrawheader}{name} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 145 | Return a single string consisting of the text after the colon in the |
| 146 | first header matching \var{name}. This includes leading whitespace, |
| 147 | the trailing linefeed, and internal linefeeds and whitespace if there |
| 148 | any continuation line(s) were present. Return \code{None} if there is |
| 149 | no header matching \var{name}. |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 150 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 151 | |
Guido van Rossum | 1299100 | 1998-06-10 21:34:27 +0000 | [diff] [blame] | 152 | \begin{methoddesc}{getheader}{name\optional{, default}} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 153 | Like \code{getrawheader(\var{name})}, but strip leading and trailing |
Guido van Rossum | 1299100 | 1998-06-10 21:34:27 +0000 | [diff] [blame] | 154 | whitespace. Internal whitespace is not stripped. The optional |
| 155 | \var{default} argument can be used to specify a different default to |
| 156 | be returned when there is no header matching \var{name}. |
| 157 | \end{methoddesc} |
| 158 | |
| 159 | \begin{methoddesc}{get}{name\optional{, default}} |
Fred Drake | 23329d4 | 1998-08-10 17:46:22 +0000 | [diff] [blame] | 160 | An alias for \method{getheader()}, to make the interface more compatible |
Guido van Rossum | 1299100 | 1998-06-10 21:34:27 +0000 | [diff] [blame] | 161 | with regular dictionaries. |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 162 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 163 | |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 164 | \begin{methoddesc}{getaddr}{name} |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 165 | Return a pair \code{(\var{full name}, \var{email address})} parsed |
| 166 | from the string returned by \code{getheader(\var{name})}. If no |
| 167 | header matching \var{name} exists, return \code{(None, None)}; |
| 168 | otherwise both the full name and the address are (possibly empty) |
| 169 | strings. |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 170 | |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 171 | 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] | 172 | \code{'jack@cwi.nl (Jack Jansen)'}, then |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 173 | \code{m.getaddr('From')} will yield the pair |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 174 | \code{('Jack Jansen', 'jack@cwi.nl')}. |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 175 | If the header contained |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 176 | \code{'Jack Jansen <jack@cwi.nl>'} instead, it would yield the |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 177 | exact same result. |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 178 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 179 | |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 180 | \begin{methoddesc}{getaddrlist}{name} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 181 | This is similar to \code{getaddr(\var{list})}, but parses a header |
Fred Drake | 23329d4 | 1998-08-10 17:46:22 +0000 | [diff] [blame] | 182 | 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] | 183 | returns a list of \code{(\var{full name}, \var{email address})} pairs |
| 184 | (even if there was only one address in the header). If there is no |
| 185 | header matching \var{name}, return an empty list. |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 186 | |
Barry Warsaw | 53610ca | 1999-01-14 21:26:54 +0000 | [diff] [blame] | 187 | If multiple headers exist that match the named header (e.g. if there |
| 188 | are several \code{Cc} headers), all are parsed for addresses. Any |
| 189 | continuation lines the named headers contain are also parsed. |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 190 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 191 | |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 192 | \begin{methoddesc}{getdate}{name} |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 193 | Retrieve a header using \method{getheader()} and parse it into a 9-tuple |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 194 | compatible with \function{time.mktime()}; note that fields 6, 7, and 8 |
| 195 | are not usable. If there is no header matching |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 196 | \var{name}, or it is unparsable, return \code{None}. |
| 197 | |
| 198 | Date parsing appears to be a black art, and not all mailers adhere to |
| 199 | the standard. While it has been tested and found correct on a large |
| 200 | collection of email from many sources, it is still possible that this |
| 201 | function may occasionally yield an incorrect result. |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 202 | \end{methoddesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 203 | |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 204 | \begin{methoddesc}{getdate_tz}{name} |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 205 | Retrieve a header using \method{getheader()} and parse it into a |
| 206 | 10-tuple; the first 9 elements will make a tuple compatible with |
| 207 | \function{time.mktime()}, and the 10th is a number giving the offset |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 208 | of the date's timezone from UTC. Note that fields 6, 7, and 8 |
| 209 | are not usable. Similarly to \method{getdate()}, if |
Guido van Rossum | 843e712 | 1996-12-06 21:23:53 +0000 | [diff] [blame] | 210 | there is no header matching \var{name}, or it is unparsable, return |
| 211 | \code{None}. |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 212 | \end{methoddesc} |
Guido van Rossum | 843e712 | 1996-12-06 21:23:53 +0000 | [diff] [blame] | 213 | |
Fred Drake | 7063149 | 2001-05-22 14:36:30 +0000 | [diff] [blame^] | 214 | \class{Message} instances also support a limited mapping interface. |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 215 | In particular: \code{\var{m}[name]} is like |
| 216 | \code{\var{m}.getheader(name)} but raises \exception{KeyError} if |
| 217 | there is no matching header; and \code{len(\var{m})}, |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 218 | \code{\var{m}.has_key(name)}, \code{\var{m}.keys()}, |
| 219 | \code{\var{m}.values()} and \code{\var{m}.items()} act as expected |
Fred Drake | 7063149 | 2001-05-22 14:36:30 +0000 | [diff] [blame^] | 220 | (and consistently). \class{Message} instances also support the |
| 221 | mapping writable interface \code{\var{m}[name] = value} and \code{del |
| 222 | \var{m}[name]}. |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 223 | |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 224 | Finally, \class{Message} instances have two public instance variables: |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 225 | |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 226 | \begin{memberdesc}{headers} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 227 | A list containing the entire set of header lines, in the order in |
Guido van Rossum | 8729483 | 1998-06-16 22:27:40 +0000 | [diff] [blame] | 228 | which they were read (except that setitem calls may disturb this |
| 229 | order). Each line contains a trailing newline. The |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 230 | blank line terminating the headers is not contained in the list. |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 231 | \end{memberdesc} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 232 | |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 233 | \begin{memberdesc}{fp} |
Fred Drake | ea00205 | 1999-04-28 18:11:09 +0000 | [diff] [blame] | 234 | The file or file-like object passed at instantiation time. This can |
| 235 | be used to read the message content. |
Fred Drake | e14dde2 | 1998-04-04 06:19:30 +0000 | [diff] [blame] | 236 | \end{memberdesc} |
Guido van Rossum | 8729483 | 1998-06-16 22:27:40 +0000 | [diff] [blame] | 237 | |
Fred Drake | ea00205 | 1999-04-28 18:11:09 +0000 | [diff] [blame] | 238 | |
| 239 | \subsection{AddressList Objects \label{addresslist-objects}} |
Guido van Rossum | 8729483 | 1998-06-16 22:27:40 +0000 | [diff] [blame] | 240 | |
| 241 | An \class{AddressList} instance has the following methods: |
| 242 | |
Fred Drake | 9c84636 | 2001-04-09 15:42:56 +0000 | [diff] [blame] | 243 | \begin{methoddesc}{__len__}{} |
Guido van Rossum | 8729483 | 1998-06-16 22:27:40 +0000 | [diff] [blame] | 244 | Return the number of addresses in the address list. |
| 245 | \end{methoddesc} |
| 246 | |
Fred Drake | 9c84636 | 2001-04-09 15:42:56 +0000 | [diff] [blame] | 247 | \begin{methoddesc}{__str__}{} |
Guido van Rossum | 8729483 | 1998-06-16 22:27:40 +0000 | [diff] [blame] | 248 | Return a canonicalized string representation of the address list. |
| 249 | Addresses are rendered in "name" <host@domain> form, comma-separated. |
| 250 | \end{methoddesc} |
| 251 | |
Fred Drake | 9c84636 | 2001-04-09 15:42:56 +0000 | [diff] [blame] | 252 | \begin{methoddesc}{__add__}{alist} |
| 253 | Return a new \class{AddressList} instance that contains all addresses |
| 254 | in both \class{AddressList} operands, with duplicates removed (set |
| 255 | union). |
Guido van Rossum | 8729483 | 1998-06-16 22:27:40 +0000 | [diff] [blame] | 256 | \end{methoddesc} |
| 257 | |
Fred Drake | 9c84636 | 2001-04-09 15:42:56 +0000 | [diff] [blame] | 258 | \begin{methoddesc}{__iadd__}{alist} |
| 259 | In-place version of \method{__add__()}; turns this \class{AddressList} |
| 260 | instance into the union of itself and the right-hand instance, |
| 261 | \var{alist}. |
| 262 | \end{methoddesc} |
| 263 | |
| 264 | \begin{methoddesc}{__sub__}{alist} |
| 265 | Return a new \class{AddressList} instance that contains every address |
| 266 | in the left-hand \class{AddressList} operand that is not present in |
| 267 | the right-hand address operand (set difference). |
| 268 | \end{methoddesc} |
| 269 | |
| 270 | \begin{methoddesc}{__isub__}{alist} |
| 271 | In-place version of \method{__sub__()}, removing addresses in this |
| 272 | list which are also in \var{alist}. |
Guido van Rossum | 8729483 | 1998-06-16 22:27:40 +0000 | [diff] [blame] | 273 | \end{methoddesc} |
| 274 | |
| 275 | |
| 276 | Finally, \class{AddressList} instances have one public instance variable: |
| 277 | |
| 278 | \begin{memberdesc}{addresslist} |
| 279 | A list of tuple string pairs, one per address. In each member, the |
Eric S. Raymond | e7213c7 | 2001-01-27 10:56:14 +0000 | [diff] [blame] | 280 | first is the canonicalized name part, the second is the |
Fred Drake | 9c84636 | 2001-04-09 15:42:56 +0000 | [diff] [blame] | 281 | actual route-address (\character{@}-separated username-host.domain |
| 282 | pair). |
Guido van Rossum | 8729483 | 1998-06-16 22:27:40 +0000 | [diff] [blame] | 283 | \end{memberdesc} |