| 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="node585.html" /> |
| 13 | <link rel="prev" href="node583.html" /> |
| 14 | <link rel="parent" href="module-email.Parser.html" /> |
| 15 | <link rel="next" href="node585.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>12.2.2.2 Parser class API</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.2.2.1 FeedParser API" |
| 25 | href="node583.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.2.2 Parsing email messages" |
| 28 | href="module-email.Parser.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.2.2.3 Additional notes" |
| 31 | href="node585.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="node583.html">12.2.2.1 FeedParser API</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="module-email.Parser.html">12.2.2 Parsing email messages</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="node585.html">12.2.2.3 Additional notes</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H3><A NAME="SECTION0014222000000000000000"> |
| 56 | 12.2.2.2 Parser class API</A> |
| 57 | </H3> |
| 58 | |
| 59 | <P> |
| 60 | The <tt class="class">Parser</tt> provides an API that can be used to parse a message when |
| 61 | the complete contents of the message are available in a string or file. The |
| 62 | <tt class="module">email.Parser</tt> module also provides a second class, called |
| 63 | <tt class="class">HeaderParser</tt> which can be used if you're only interested in |
| 64 | the headers of the message. <tt class="class">HeaderParser</tt> can be much faster in |
| 65 | these situations, since it does not attempt to parse the message body, |
| 66 | instead setting the payload to the raw body as a string. |
| 67 | <tt class="class">HeaderParser</tt> has the same API as the <tt class="class">Parser</tt> class. |
| 68 | |
| 69 | <P> |
| 70 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 71 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-3879' xml:id='l2h-3879' class="class">Parser</tt></b>(</nobr></td> |
| 72 | <td><var></var><big>[</big><var>_class</var><big>[</big><var>, strict</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 73 | <dd> |
| 74 | The constructor for the <tt class="class">Parser</tt> class takes an optional |
| 75 | argument <var>_class</var>. This must be a callable factory (such as a |
| 76 | function or a class), and it is used whenever a sub-message object |
| 77 | needs to be created. It defaults to <tt class="class">Message</tt> (see |
| 78 | <tt class="module"><a href="module-email.Message.html">email.Message</a></tt>). The factory will be called without |
| 79 | arguments. |
| 80 | |
| 81 | <P> |
| 82 | The optional <var>strict</var> flag is ignored. <div class="versionnote"><b>Deprecated since release 2.4.</b> |
| 83 | Because the |
| 84 | <tt class="class">Parser</tt> class is a backward compatible API wrapper around the |
| 85 | new-in-Python 2.4 <tt class="class">FeedParser</tt>, <em>all</em> parsing is effectively |
| 86 | non-strict. You should simply stop passing a <var>strict</var> flag to the |
| 87 | <tt class="class">Parser</tt> constructor.</div><p></p> |
| 88 | |
| 89 | <P> |
| 90 | |
| 91 | <span class="versionnote">Changed in version 2.2.2: |
| 92 | The <var>strict</var> flag was added.</span> |
| 93 | |
| 94 | <span class="versionnote">Changed in version 2.4: |
| 95 | The <var>strict</var> flag was deprecated.</span> |
| 96 | |
| 97 | </dl> |
| 98 | |
| 99 | <P> |
| 100 | The other public <tt class="class">Parser</tt> methods are: |
| 101 | |
| 102 | <P> |
| 103 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 104 | <td><nobr><b><tt id='l2h-3880' xml:id='l2h-3880' class="method">parse</tt></b>(</nobr></td> |
| 105 | <td><var>fp</var><big>[</big><var>, headersonly</var><big>]</big><var></var>)</td></tr></table></dt> |
| 106 | <dd> |
| 107 | Read all the data from the file-like object <var>fp</var>, parse the |
| 108 | resulting text, and return the root message object. <var>fp</var> must |
| 109 | support both the <tt class="method">readline()</tt> and the <tt class="method">read()</tt> methods |
| 110 | on file-like objects. |
| 111 | |
| 112 | <P> |
| 113 | The text contained in <var>fp</var> must be formatted as a block of <a class="rfc" id='rfcref-90842' xml:id='rfcref-90842' |
| 114 | href="http://www.faqs.org/rfcs/rfc2822.html">RFC 2822</a> |
| 115 | style headers and header continuation lines, optionally preceded by a |
| 116 | envelope header. The header block is terminated either by the |
| 117 | end of the data or by a blank line. Following the header block is the |
| 118 | body of the message (which may contain MIME-encoded subparts). |
| 119 | |
| 120 | <P> |
| 121 | Optional <var>headersonly</var> is as with the <tt class="method">parse()</tt> method. |
| 122 | |
| 123 | <P> |
| 124 | |
| 125 | <span class="versionnote">Changed in version 2.2.2: |
| 126 | The <var>headersonly</var> flag was added.</span> |
| 127 | |
| 128 | </dl> |
| 129 | |
| 130 | <P> |
| 131 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 132 | <td><nobr><b><tt id='l2h-3881' xml:id='l2h-3881' class="method">parsestr</tt></b>(</nobr></td> |
| 133 | <td><var>text</var><big>[</big><var>, headersonly</var><big>]</big><var></var>)</td></tr></table></dt> |
| 134 | <dd> |
| 135 | Similar to the <tt class="method">parse()</tt> method, except it takes a string |
| 136 | object instead of a file-like object. Calling this method on a string |
| 137 | is exactly equivalent to wrapping <var>text</var> in a <tt class="class">StringIO</tt> |
| 138 | instance first and calling <tt class="method">parse()</tt>. |
| 139 | |
| 140 | <P> |
| 141 | Optional <var>headersonly</var> is a flag specifying whether to stop |
| 142 | parsing after reading the headers or not. The default is <code>False</code>, |
| 143 | meaning it parses the entire contents of the file. |
| 144 | |
| 145 | <P> |
| 146 | |
| 147 | <span class="versionnote">Changed in version 2.2.2: |
| 148 | The <var>headersonly</var> flag was added.</span> |
| 149 | |
| 150 | </dl> |
| 151 | |
| 152 | <P> |
| 153 | Since creating a message object structure from a string or a file |
| 154 | object is such a common task, two functions are provided as a |
| 155 | convenience. They are available in the top-level <tt class="module">email</tt> |
| 156 | package namespace. |
| 157 | |
| 158 | <P> |
| 159 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 160 | <td><nobr><b><tt id='l2h-3882' xml:id='l2h-3882' class="function">message_from_string</tt></b>(</nobr></td> |
| 161 | <td><var>s</var><big>[</big><var>, _class</var><big>[</big><var>, strict</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 162 | <dd> |
| 163 | Return a message object structure from a string. This is exactly |
| 164 | equivalent to <code>Parser().parsestr(s)</code>. Optional <var>_class</var> and |
| 165 | <var>strict</var> are interpreted as with the <tt class="class">Parser</tt> class constructor. |
| 166 | |
| 167 | <P> |
| 168 | |
| 169 | <span class="versionnote">Changed in version 2.2.2: |
| 170 | The <var>strict</var> flag was added.</span> |
| 171 | |
| 172 | </dl> |
| 173 | |
| 174 | <P> |
| 175 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 176 | <td><nobr><b><tt id='l2h-3883' xml:id='l2h-3883' class="function">message_from_file</tt></b>(</nobr></td> |
| 177 | <td><var>fp</var><big>[</big><var>, _class</var><big>[</big><var>, strict</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 178 | <dd> |
| 179 | Return a message object structure tree from an open file object. This |
| 180 | is exactly equivalent to <code>Parser().parse(fp)</code>. Optional |
| 181 | <var>_class</var> and <var>strict</var> are interpreted as with the |
| 182 | <tt class="class">Parser</tt> class constructor. |
| 183 | |
| 184 | <P> |
| 185 | |
| 186 | <span class="versionnote">Changed in version 2.2.2: |
| 187 | The <var>strict</var> flag was added.</span> |
| 188 | |
| 189 | </dl> |
| 190 | |
| 191 | <P> |
| 192 | Here's an example of how you might use this at an interactive Python |
| 193 | prompt: |
| 194 | |
| 195 | <P> |
| 196 | <div class="verbatim"><pre> |
| 197 | >>> import email |
| 198 | >>> msg = email.message_from_string(myString) |
| 199 | </pre></div> |
| 200 | |
| 201 | <P> |
| 202 | |
| 203 | <DIV CLASS="navigation"> |
| 204 | <div class='online-navigation'> |
| 205 | <p></p><hr /> |
| 206 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 207 | <tr> |
| 208 | <td class='online-navigation'><a rel="prev" title="12.2.2.1 FeedParser API" |
| 209 | href="node583.html"><img src='../icons/previous.png' |
| 210 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 211 | <td class='online-navigation'><a rel="parent" title="12.2.2 Parsing email messages" |
| 212 | href="module-email.Parser.html"><img src='../icons/up.png' |
| 213 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 214 | <td class='online-navigation'><a rel="next" title="12.2.2.3 Additional notes" |
| 215 | href="node585.html"><img src='../icons/next.png' |
| 216 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 217 | <td align="center" width="100%">Python Library Reference</td> |
| 218 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 219 | href="contents.html"><img src='../icons/contents.png' |
| 220 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 221 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 222 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 223 | <td class='online-navigation'><a rel="index" title="Index" |
| 224 | href="genindex.html"><img src='../icons/index.png' |
| 225 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 226 | </tr></table> |
| 227 | <div class='online-navigation'> |
| 228 | <b class="navlabel">Previous:</b> |
| 229 | <a class="sectref" rel="prev" href="node583.html">12.2.2.1 FeedParser API</A> |
| 230 | <b class="navlabel">Up:</b> |
| 231 | <a class="sectref" rel="parent" href="module-email.Parser.html">12.2.2 Parsing email messages</A> |
| 232 | <b class="navlabel">Next:</b> |
| 233 | <a class="sectref" rel="next" href="node585.html">12.2.2.3 Additional notes</A> |
| 234 | </div> |
| 235 | </div> |
| 236 | <hr /> |
| 237 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 238 | </DIV> |
| 239 | <!--End of Navigation Panel--> |
| 240 | <ADDRESS> |
| 241 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 242 | </ADDRESS> |
| 243 | </BODY> |
| 244 | </HTML> |