| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="module-base64.html" /> |
| 13 | <link rel="prev" href="module-multifile.html" /> |
| 14 | <link rel="parent" href="netdata.html" /> |
| 15 | <link rel="next" href="message-objects.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>12.11 rfc822 -- Parse RFC 2822 mail headers</title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="12.10.2 MultiFile Example" |
| 25 | href="multifile-example.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="12. Internet Data Handling" |
| 28 | href="netdata.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="12.11.1 Message Objects" |
| 31 | href="message-objects.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Library Reference</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 38 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="multifile-example.html">12.10.2 MultiFile Example</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="netdata.html">12. Internet Data Handling</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="message-objects.html">12.11.1 Message Objects</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H1><A NAME="SECTION00141100000000000000000"> |
| 56 | 12.11 <tt class="module">rfc822</tt> -- |
| 57 | Parse RFC 2822 mail headers</A> |
| 58 | </H1> |
| 59 | |
| 60 | <P> |
| 61 | <A NAME="module-rfc822"></A> |
| 62 | |
| 63 | <P> |
| 64 | <div class="versionnote"><b>Deprecated since release 2.3.</b> |
| 65 | The <tt class="module"><a href="module-email.html">email</a></tt> package should be used in |
| 66 | preference to the <tt class="module">rfc822</tt> module. This |
| 67 | module is present only to maintain backward |
| 68 | compatibility.</div><p></p> |
| 69 | |
| 70 | <P> |
| 71 | This module defines a class, <tt class="class">Message</tt>, which represents an |
| 72 | ``email message'' as defined by the Internet standard |
| 73 | <a class="rfc" id='rfcref-91168' xml:id='rfcref-91168' |
| 74 | href="http://www.faqs.org/rfcs/rfc2822.html">RFC 2822</a>.<A NAME="tex2html141" |
| 75 | HREF="#foot60406"><SUP>12.6</SUP></A> Such messages |
| 76 | consist of a collection of message headers, and a message body. This |
| 77 | module also defines a helper class |
| 78 | <tt class="class">AddressList</tt> for parsing <a class="rfc" id='rfcref-91170' xml:id='rfcref-91170' |
| 79 | href="http://www.faqs.org/rfcs/rfc2822.html">RFC 2822</a> addresses. Please refer to |
| 80 | the RFC for information on the specific syntax of <a class="rfc" id='rfcref-91172' xml:id='rfcref-91172' |
| 81 | href="http://www.faqs.org/rfcs/rfc2822.html">RFC 2822</a> messages. |
| 82 | |
| 83 | <P> |
| 84 | The <tt class="module"><a href="module-mailbox.html">mailbox</a></tt><a id='l2h-4083' xml:id='l2h-4083'></a> module provides classes |
| 85 | to read mailboxes produced by various end-user mail programs. |
| 86 | |
| 87 | <P> |
| 88 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 89 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-4074' xml:id='l2h-4074' class="class">Message</tt></b>(</nobr></td> |
| 90 | <td><var>file</var><big>[</big><var>, seekable</var><big>]</big><var></var>)</td></tr></table></dt> |
| 91 | <dd> |
| 92 | A <tt class="class">Message</tt> instance is instantiated with an input object as |
| 93 | parameter. Message relies only on the input object having a |
| 94 | <tt class="method">readline()</tt> method; in particular, ordinary file objects |
| 95 | qualify. Instantiation reads headers from the input object up to a |
| 96 | delimiter line (normally a blank line) and stores them in the |
| 97 | instance. The message body, following the headers, is not consumed. |
| 98 | |
| 99 | <P> |
| 100 | This class can work with any input object that supports a |
| 101 | <tt class="method">readline()</tt> method. If the input object has seek and tell |
| 102 | capability, the <tt class="method">rewindbody()</tt> method will work; also, illegal |
| 103 | lines will be pushed back onto the input stream. If the input object |
| 104 | lacks seek but has an <tt class="method">unread()</tt> method that can push back a |
| 105 | line of input, <tt class="class">Message</tt> will use that to push back illegal |
| 106 | lines. Thus this class can be used to parse messages coming from a |
| 107 | buffered stream. |
| 108 | |
| 109 | <P> |
| 110 | The optional <var>seekable</var> argument is provided as a workaround for |
| 111 | certain stdio libraries in which <tt class="cfunction">tell()</tt> discards buffered |
| 112 | data before discovering that the <tt class="cfunction">lseek()</tt> system call |
| 113 | doesn't work. For maximum portability, you should set the seekable |
| 114 | argument to zero to prevent that initial <tt class="method">tell()</tt> when passing |
| 115 | in an unseekable object such as a file object created from a socket |
| 116 | object. |
| 117 | |
| 118 | <P> |
| 119 | Input lines as read from the file may either be terminated by CR-LF or |
| 120 | by a single linefeed; a terminating CR-LF is replaced by a single |
| 121 | linefeed before the line is stored. |
| 122 | |
| 123 | <P> |
| 124 | All header matching is done independent of upper or lower case; |
| 125 | e.g. <code><var>m</var>['From']</code>, <code><var>m</var>['from']</code> and |
| 126 | <code><var>m</var>['FROM']</code> all yield the same result. |
| 127 | </dl> |
| 128 | |
| 129 | <P> |
| 130 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 131 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-4075' xml:id='l2h-4075' class="class">AddressList</tt></b>(</nobr></td> |
| 132 | <td><var>field</var>)</td></tr></table></dt> |
| 133 | <dd> |
| 134 | You may instantiate the <tt class="class">AddressList</tt> helper class using a single |
| 135 | string parameter, a comma-separated list of <a class="rfc" id='rfcref-91175' xml:id='rfcref-91175' |
| 136 | href="http://www.faqs.org/rfcs/rfc2822.html">RFC 2822</a> addresses to be |
| 137 | parsed. (The parameter <code>None</code> yields an empty list.) |
| 138 | </dl> |
| 139 | |
| 140 | <P> |
| 141 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 142 | <td><nobr><b><tt id='l2h-4076' xml:id='l2h-4076' class="function">quote</tt></b>(</nobr></td> |
| 143 | <td><var>str</var>)</td></tr></table></dt> |
| 144 | <dd> |
| 145 | Return a new string with backslashes in <var>str</var> replaced by two |
| 146 | backslashes and double quotes replaced by backslash-double quote. |
| 147 | </dl> |
| 148 | |
| 149 | <P> |
| 150 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 151 | <td><nobr><b><tt id='l2h-4077' xml:id='l2h-4077' class="function">unquote</tt></b>(</nobr></td> |
| 152 | <td><var>str</var>)</td></tr></table></dt> |
| 153 | <dd> |
| 154 | Return a new string which is an <em>unquoted</em> version of <var>str</var>. |
| 155 | If <var>str</var> ends and begins with double quotes, they are stripped |
| 156 | off. Likewise if <var>str</var> ends and begins with angle brackets, they |
| 157 | are stripped off. |
| 158 | </dl> |
| 159 | |
| 160 | <P> |
| 161 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 162 | <td><nobr><b><tt id='l2h-4078' xml:id='l2h-4078' class="function">parseaddr</tt></b>(</nobr></td> |
| 163 | <td><var>address</var>)</td></tr></table></dt> |
| 164 | <dd> |
| 165 | Parse <var>address</var>, which should be the value of some |
| 166 | address-containing field such as <span class="mailheader">To:</span> or <span class="mailheader">Cc:</span>, |
| 167 | into its constituent ``realname'' and ``email address'' parts. |
| 168 | Returns a tuple of that information, unless the parse fails, in which |
| 169 | case a 2-tuple <code>(None, None)</code> is returned. |
| 170 | </dl> |
| 171 | |
| 172 | <P> |
| 173 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 174 | <td><nobr><b><tt id='l2h-4079' xml:id='l2h-4079' class="function">dump_address_pair</tt></b>(</nobr></td> |
| 175 | <td><var>pair</var>)</td></tr></table></dt> |
| 176 | <dd> |
| 177 | The inverse of <tt class="method">parseaddr()</tt>, this takes a 2-tuple of the form |
| 178 | <code>(<var>realname</var>, <var>email_address</var>)</code> and returns the string |
| 179 | value suitable for a <span class="mailheader">To:</span> or <span class="mailheader">Cc:</span> header. If |
| 180 | the first element of <var>pair</var> is false, then the second element is |
| 181 | returned unmodified. |
| 182 | </dl> |
| 183 | |
| 184 | <P> |
| 185 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 186 | <td><nobr><b><tt id='l2h-4080' xml:id='l2h-4080' class="function">parsedate</tt></b>(</nobr></td> |
| 187 | <td><var>date</var>)</td></tr></table></dt> |
| 188 | <dd> |
| 189 | Attempts to parse a date according to the rules in <a class="rfc" id='rfcref-91177' xml:id='rfcref-91177' |
| 190 | href="http://www.faqs.org/rfcs/rfc2822.html">RFC 2822</a>. |
| 191 | however, some mailers don't follow that format as specified, so |
| 192 | <tt class="function">parsedate()</tt> tries to guess correctly in such cases. |
| 193 | <var>date</var> is a string containing an <a class="rfc" id='rfcref-91179' xml:id='rfcref-91179' |
| 194 | href="http://www.faqs.org/rfcs/rfc2822.html">RFC 2822</a> date, such as |
| 195 | <code>'Mon, 20 Nov 1995 19:12:08 -0500'</code>. If it succeeds in parsing |
| 196 | the date, <tt class="function">parsedate()</tt> returns a 9-tuple that can be passed |
| 197 | directly to <tt class="function">time.mktime()</tt>; otherwise <code>None</code> will be |
| 198 | returned. Note that fields 6, 7, and 8 of the result tuple are not |
| 199 | usable. |
| 200 | </dl> |
| 201 | |
| 202 | <P> |
| 203 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 204 | <td><nobr><b><tt id='l2h-4081' xml:id='l2h-4081' class="function">parsedate_tz</tt></b>(</nobr></td> |
| 205 | <td><var>date</var>)</td></tr></table></dt> |
| 206 | <dd> |
| 207 | Performs the same function as <tt class="function">parsedate()</tt>, but returns |
| 208 | either <code>None</code> or a 10-tuple; the first 9 elements make up a tuple |
| 209 | that can be passed directly to <tt class="function">time.mktime()</tt>, and the tenth |
| 210 | is the offset of the date's timezone from UTC (which is the official |
| 211 | term for Greenwich Mean Time). (Note that the sign of the timezone |
| 212 | offset is the opposite of the sign of the <code>time.timezone</code> |
| 213 | variable for the same timezone; the latter variable follows the |
| 214 | POSIX standard while this module follows <a class="rfc" id='rfcref-91181' xml:id='rfcref-91181' |
| 215 | href="http://www.faqs.org/rfcs/rfc2822.html">RFC 2822</a>.) If the input |
| 216 | string has no timezone, the last element of the tuple returned is |
| 217 | <code>None</code>. Note that fields 6, 7, and 8 of the result tuple are not |
| 218 | usable. |
| 219 | </dl> |
| 220 | |
| 221 | <P> |
| 222 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 223 | <td><nobr><b><tt id='l2h-4082' xml:id='l2h-4082' class="function">mktime_tz</tt></b>(</nobr></td> |
| 224 | <td><var>tuple</var>)</td></tr></table></dt> |
| 225 | <dd> |
| 226 | Turn a 10-tuple as returned by <tt class="function">parsedate_tz()</tt> into a UTC |
| 227 | timestamp. If the timezone item in the tuple is <code>None</code>, assume |
| 228 | local time. Minor deficiency: this first interprets the first 8 |
| 229 | elements as a local time and then compensates for the timezone |
| 230 | difference; this may yield a slight error around daylight savings time |
| 231 | switch dates. Not enough to worry about for common use. |
| 232 | </dl> |
| 233 | |
| 234 | <P> |
| 235 | <div class="seealso"> |
| 236 | <p class="heading">See Also:</p> |
| 237 | |
| 238 | <dl compact="compact" class="seemodule"> |
| 239 | <dt>Module <b><tt class="module"><a href="module-email.html">email</a></tt>:</b> |
| 240 | <dd>Comprehensive email handling package; supersedes |
| 241 | the <tt class="module">rfc822</tt> module. |
| 242 | </dl> |
| 243 | <dl compact="compact" class="seemodule"> |
| 244 | <dt>Module <b><tt class="module"><a href="module-mailbox.html">mailbox</a></tt>:</b> |
| 245 | <dd>Classes to read various mailbox formats produced |
| 246 | by end-user mail programs. |
| 247 | </dl> |
| 248 | <dl compact="compact" class="seemodule"> |
| 249 | <dt>Module <b><tt class="module"><a href="module-mimetools.html">mimetools</a></tt>:</b> |
| 250 | <dd>Subclass of <tt class="class">rfc822.Message</tt> that |
| 251 | handles MIME encoded messages. |
| 252 | </dl> |
| 253 | </div> |
| 254 | |
| 255 | <P> |
| 256 | <BR><HR><H4>Footnotes</H4> |
| 257 | <DL> |
| 258 | <DT><A NAME="foot60406">...2822.</A><A |
| 259 | href="module-rfc822.html#tex2html141"><SUP>12.6</SUP></A></DT> |
| 260 | <DD>This module originally conformed to <a class="rfc" id='rfcref-91149' xml:id='rfcref-91149' |
| 261 | href="http://www.faqs.org/rfcs/rfc822.html">RFC 822</a>, |
| 262 | hence the name. Since then, <a class="rfc" id='rfcref-91151' xml:id='rfcref-91151' |
| 263 | href="http://www.faqs.org/rfcs/rfc2822.html">RFC 2822</a> has been released as an |
| 264 | update to <a class="rfc" id='rfcref-91153' xml:id='rfcref-91153' |
| 265 | href="http://www.faqs.org/rfcs/rfc822.html">RFC 822</a>. This module should be considered |
| 266 | <a class="rfc" id='rfcref-91155' xml:id='rfcref-91155' |
| 267 | href="http://www.faqs.org/rfcs/rfc2822.html">RFC 2822</a>-conformant, especially in cases where the |
| 268 | syntax or semantics have changed since <a class="rfc" id='rfcref-91157' xml:id='rfcref-91157' |
| 269 | href="http://www.faqs.org/rfcs/rfc822.html">RFC 822</a>. |
| 270 | |
| 271 | </DD> |
| 272 | </DL> |
| 273 | <p><br /></p><hr class='online-navigation' /> |
| 274 | <div class='online-navigation'> |
| 275 | <!--Table of Child-Links--> |
| 276 | <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a> |
| 277 | |
| 278 | <UL CLASS="ChildLinks"> |
| 279 | <LI><A href="message-objects.html">12.11.1 Message Objects</a> |
| 280 | <LI><A href="addresslist-objects.html">12.11.2 AddressList Objects</a> |
| 281 | </ul> |
| 282 | <!--End of Table of Child-Links--> |
| 283 | </div> |
| 284 | |
| 285 | <DIV CLASS="navigation"> |
| 286 | <div class='online-navigation'> |
| 287 | <p></p><hr /> |
| 288 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 289 | <tr> |
| 290 | <td class='online-navigation'><a rel="prev" title="12.10.2 MultiFile Example" |
| 291 | href="multifile-example.html"><img src='../icons/previous.png' |
| 292 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 293 | <td class='online-navigation'><a rel="parent" title="12. Internet Data Handling" |
| 294 | href="netdata.html"><img src='../icons/up.png' |
| 295 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 296 | <td class='online-navigation'><a rel="next" title="12.11.1 Message Objects" |
| 297 | href="message-objects.html"><img src='../icons/next.png' |
| 298 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 299 | <td align="center" width="100%">Python Library Reference</td> |
| 300 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 301 | href="contents.html"><img src='../icons/contents.png' |
| 302 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 303 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 304 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 305 | <td class='online-navigation'><a rel="index" title="Index" |
| 306 | href="genindex.html"><img src='../icons/index.png' |
| 307 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 308 | </tr></table> |
| 309 | <div class='online-navigation'> |
| 310 | <b class="navlabel">Previous:</b> |
| 311 | <a class="sectref" rel="prev" href="multifile-example.html">12.10.2 MultiFile Example</A> |
| 312 | <b class="navlabel">Up:</b> |
| 313 | <a class="sectref" rel="parent" href="netdata.html">12. Internet Data Handling</A> |
| 314 | <b class="navlabel">Next:</b> |
| 315 | <a class="sectref" rel="next" href="message-objects.html">12.11.1 Message Objects</A> |
| 316 | </div> |
| 317 | </div> |
| 318 | <hr /> |
| 319 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 320 | </DIV> |
| 321 | <!--End of Navigation Panel--> |
| 322 | <ADDRESS> |
| 323 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 324 | </ADDRESS> |
| 325 | </BODY> |
| 326 | </HTML> |