Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 1 | \section{Standard Module \sectcode{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} |
| 65 | |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 66 | A \class{Message} instance has the following methods: |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 67 | |
| 68 | \begin{funcdesc}{rewindbody}{} |
| 69 | Seek to the start of the message body. This only works if the file |
| 70 | object is seekable. |
| 71 | \end{funcdesc} |
| 72 | |
| 73 | \begin{funcdesc}{getallmatchingheaders}{name} |
Guido van Rossum | 6c4f003 | 1995-03-07 10:14:09 +0000 | [diff] [blame] | 74 | Return a list of lines consisting of all headers matching |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 75 | \var{name}, if any. Each physical line, whether it is a continuation |
| 76 | line or not, is a separate list item. Return the empty list if no |
| 77 | header matches \var{name}. |
| 78 | \end{funcdesc} |
| 79 | |
| 80 | \begin{funcdesc}{getfirstmatchingheader}{name} |
| 81 | Return a list of lines comprising the first header matching |
| 82 | \var{name}, and its continuation line(s), if any. Return \code{None} |
| 83 | if there is no header matching \var{name}. |
| 84 | \end{funcdesc} |
| 85 | |
| 86 | \begin{funcdesc}{getrawheader}{name} |
| 87 | Return a single string consisting of the text after the colon in the |
| 88 | first header matching \var{name}. This includes leading whitespace, |
| 89 | the trailing linefeed, and internal linefeeds and whitespace if there |
| 90 | any continuation line(s) were present. Return \code{None} if there is |
| 91 | no header matching \var{name}. |
| 92 | \end{funcdesc} |
| 93 | |
| 94 | \begin{funcdesc}{getheader}{name} |
| 95 | Like \code{getrawheader(\var{name})}, but strip leading and trailing |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 96 | whitespace. Internal whitespace is not stripped. |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 97 | \end{funcdesc} |
| 98 | |
| 99 | \begin{funcdesc}{getaddr}{name} |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 100 | Return a pair \code{(\var{full name}, \var{email address})} parsed |
| 101 | from the string returned by \code{getheader(\var{name})}. If no |
| 102 | header matching \var{name} exists, return \code{(None, None)}; |
| 103 | otherwise both the full name and the address are (possibly empty) |
| 104 | strings. |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 105 | |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 106 | 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] | 107 | \code{'jack@cwi.nl (Jack Jansen)'}, then |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 108 | \code{m.getaddr('From')} will yield the pair |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 109 | \code{('Jack Jansen', 'jack@cwi.nl')}. |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 110 | If the header contained |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 111 | \code{'Jack Jansen <jack@cwi.nl>'} instead, it would yield the |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 112 | exact same result. |
| 113 | \end{funcdesc} |
| 114 | |
| 115 | \begin{funcdesc}{getaddrlist}{name} |
| 116 | This is similar to \code{getaddr(\var{list})}, but parses a header |
| 117 | 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] | 118 | returns a list of \code{(\var{full name}, \var{email address})} pairs |
| 119 | (even if there was only one address in the header). If there is no |
| 120 | header matching \var{name}, return an empty list. |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 121 | |
| 122 | XXX The current version of this function is not really correct. It |
| 123 | yields bogus results if a full name contains a comma. |
| 124 | \end{funcdesc} |
| 125 | |
| 126 | \begin{funcdesc}{getdate}{name} |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 127 | Retrieve a header using \method{getheader()} and parse it into a 9-tuple |
| 128 | compatible with \function{time.mktime()}. If there is no header matching |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 129 | \var{name}, or it is unparsable, return \code{None}. |
| 130 | |
| 131 | Date parsing appears to be a black art, and not all mailers adhere to |
| 132 | the standard. While it has been tested and found correct on a large |
| 133 | collection of email from many sources, it is still possible that this |
| 134 | function may occasionally yield an incorrect result. |
| 135 | \end{funcdesc} |
| 136 | |
Guido van Rossum | 843e712 | 1996-12-06 21:23:53 +0000 | [diff] [blame] | 137 | \begin{funcdesc}{getdate_tz}{name} |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 138 | Retrieve a header using \method{getheader()} and parse it into a |
| 139 | 10-tuple; the first 9 elements will make a tuple compatible with |
| 140 | \function{time.mktime()}, and the 10th is a number giving the offset |
| 141 | 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] | 142 | there is no header matching \var{name}, or it is unparsable, return |
| 143 | \code{None}. |
| 144 | \end{funcdesc} |
| 145 | |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 146 | \class{Message} instances also support a read-only mapping interface. |
| 147 | In particular: \code{\var{m}[name]} is the same as |
| 148 | \code{\var{m}.getheader(name)}; and \code{len(\var{m})}, |
| 149 | \code{\var{m}.has_key(name)}, \code{\var{m}.keys()}, |
| 150 | \code{\var{m}.values()} and \code{\var{m}.items()} act as expected |
| 151 | (and consistently). |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 152 | |
Fred Drake | cdea8a3 | 1998-03-14 06:17:43 +0000 | [diff] [blame] | 153 | Finally, \class{Message} instances have two public instance variables: |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 154 | |
| 155 | \begin{datadesc}{headers} |
| 156 | A list containing the entire set of header lines, in the order in |
| 157 | which they were read. Each line contains a trailing newline. The |
| 158 | blank line terminating the headers is not contained in the list. |
| 159 | \end{datadesc} |
| 160 | |
| 161 | \begin{datadesc}{fp} |
| 162 | The file object passed at instantiation time. |
| 163 | \end{datadesc} |