⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 message-objects.html

📁 一本很好的python的说明书,适合对python感兴趣的人
💻 HTML
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>12.2.1 Message Objects </title>
<META NAME="description" CONTENT="12.2.1 Message Objects ">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="lib.css" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="next" href="addresslist-objects.html" tppabs="http://www.python.org/doc/current/lib/addresslist-objects.html">
<LINK REL="previous" href="module-rfc822.html" tppabs="http://www.python.org/doc/current/lib/module-rfc822.html">
<LINK REL="up" href="module-rfc822.html" tppabs="http://www.python.org/doc/current/lib/module-rfc822.html">
<LINK REL="next" href="addresslist-objects.html" tppabs="http://www.python.org/doc/current/lib/addresslist-objects.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="module-rfc822.html" tppabs="http://www.python.org/doc/current/lib/module-rfc822.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
  alt="Previous Page" width="32"></A></td>
<td><A href="module-rfc822.html" tppabs="http://www.python.org/doc/current/lib/module-rfc822.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
  alt="Up One Level" width="32"></A></td>
<td><A href="addresslist-objects.html" tppabs="http://www.python.org/doc/current/lib/addresslist-objects.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
  alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
  alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
  alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
  alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="module-rfc822.html" tppabs="http://www.python.org/doc/current/lib/module-rfc822.html">12.2 rfc822  </A>
<b class="navlabel">Up:</b> <a class="sectref" href="module-rfc822.html" tppabs="http://www.python.org/doc/current/lib/module-rfc822.html">12.2 rfc822  </A>
<b class="navlabel">Next:</b> <a class="sectref" href="addresslist-objects.html" tppabs="http://www.python.org/doc/current/lib/addresslist-objects.html">12.2.2 AddressList Objects</A>
<br><hr></DIV>
<!--End of Navigation Panel-->

<H2>
<BR>
12.2.1 Message Objects 
</H2>

<P>
A <tt class="class">Message</tt> instance has the following methods:

<P>
<dl><dt><b><a name='l2h-2352'><tt class='method'>rewindbody</tt></a></b> ()
<dd>
Seek to the start of the message body.  This only works if the file
object is seekable.
</dl>

<P>
<dl><dt><b><a name='l2h-2353'><tt class='method'>isheader</tt></a></b> (<var>line</var>)
<dd>
Returns a line's canonicalized fieldname (the dictionary key that will
be used to index it) if the line is a legal <a class="rfc" name="rfcref-47427"
href="javascript:if(confirm('http://www.ietf.org/rfc/rfc0822.txt  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://www.ietf.org/rfc/rfc0822.txt'" tppabs="http://www.ietf.org/rfc/rfc0822.txt">RFC 822 <img src="offsite.gif" tppabs="http://www.python.org/doc/current/icons/offsite.gif"
  border='0' class='offsitelink' height='15' width='17' alt='[off-site link]'
  ></a> header; otherwise
returns None (implying that parsing should stop here and the line be
pushed back on the input stream).  It is sometimes useful to override
this method in a subclass.
</dl>

<P>
<dl><dt><b><a name='l2h-2354'><tt class='method'>islast</tt></a></b> (<var>line</var>)
<dd>
Return true if the given line is a delimiter on which Message should
stop.  The delimiter line is consumed, and the file object's read
location positioned immediately after it.  By default this method just
checks that the line is blank, but you can override it in a subclass.
</dl>

<P>
<dl><dt><b><a name='l2h-2355'><tt class='method'>iscomment</tt></a></b> (<var>line</var>)
<dd>
Return true if the given line should be ignored entirely, just skipped.
By default this is a stub that always returns false, but you can
override it in a subclass.
</dl>

<P>
<dl><dt><b><a name='l2h-2356'><tt class='method'>getallmatchingheaders</tt></a></b> (<var>name</var>)
<dd>
Return a list of lines consisting of all headers matching
<var>name</var>, if any.  Each physical line, whether it is a continuation
line or not, is a separate list item.  Return the empty list if no
header matches <var>name</var>.
</dl>

<P>
<dl><dt><b><a name='l2h-2357'><tt class='method'>getfirstmatchingheader</tt></a></b> (<var>name</var>)
<dd>
Return a list of lines comprising the first header matching
<var>name</var>, and its continuation line(s), if any.  Return
<code>None</code> if there is no header matching <var>name</var>.
</dl>

<P>
<dl><dt><b><a name='l2h-2358'><tt class='method'>getrawheader</tt></a></b> (<var>name</var>)
<dd>
Return a single string consisting of the text after the colon in the
first header matching <var>name</var>.  This includes leading whitespace,
the trailing linefeed, and internal linefeeds and whitespace if there
any continuation line(s) were present.  Return <code>None</code> if there is
no header matching <var>name</var>.
</dl>

<P>
<dl><dt><b><a name='l2h-2359'><tt class='method'>getheader</tt></a></b> (<var>name</var><big>[</big><var>, default</var><big>]</big>)
<dd>
Like <code>getrawheader(<var>name</var>)</code>, but strip leading and trailing
whitespace.  Internal whitespace is not stripped.  The optional
<var>default</var> argument can be used to specify a different default to
be returned when there is no header matching <var>name</var>.
</dl>

<P>
<dl><dt><b><a name='l2h-2360'><tt class='method'>get</tt></a></b> (<var>name</var><big>[</big><var>, default</var><big>]</big>)
<dd>
An alias for <tt class="method">getheader()</tt>, to make the interface more compatible 
with regular dictionaries.
</dl>

<P>
<dl><dt><b><a name='l2h-2361'><tt class='method'>getaddr</tt></a></b> (<var>name</var>)
<dd>
Return a pair <code>(<var>full name</var>, <var>email address</var>)</code> parsed
from the string returned by <code>getheader(<var>name</var>)</code>.  If no
header matching <var>name</var> exists, return <code>(None, None)</code>;
otherwise both the full name and the address are (possibly empty)
strings.

<P>
Example: If <var>m</var>'s first <code>From</code> header contains the string
<code>'jack@cwi.nl (Jack Jansen)'</code>, then
<code>m.getaddr('From')</code> will yield the pair
<code>('Jack Jansen', 'jack@cwi.nl')</code>.
If the header contained
<code>'Jack Jansen &lt;jack@cwi.nl&gt;'</code> instead, it would yield the
exact same result.
</dl>

<P>
<dl><dt><b><a name='l2h-2362'><tt class='method'>getaddrlist</tt></a></b> (<var>name</var>)
<dd>
This is similar to <code>getaddr(<var>list</var>)</code>, but parses a header
containing a list of email addresses (e.g. a <code>To</code> header) and
returns a list of <code>(<var>full name</var>, <var>email address</var>)</code> pairs
(even if there was only one address in the header).  If there is no
header matching <var>name</var>, return an empty list.

<P>
If multiple headers exist that match the named header (e.g. if there
are several <code>Cc</code> headers), all are parsed for addresses.  Any
continuation lines the named headers contain are also parsed.
</dl>

<P>
<dl><dt><b><a name='l2h-2363'><tt class='method'>getdate</tt></a></b> (<var>name</var>)
<dd>
Retrieve a header using <tt class="method">getheader()</tt> and parse it into a 9-tuple
compatible with <tt class="function">time.mktime()</tt>; note that fields 6, 7, and 8 
are not usable.  If there is no header matching
<var>name</var>, or it is unparsable, return <code>None</code>.

<P>
Date parsing appears to be a black art, and not all mailers adhere to
the standard.  While it has been tested and found correct on a large
collection of email from many sources, it is still possible that this
function may occasionally yield an incorrect result.
</dl>

<P>
<dl><dt><b><a name='l2h-2364'><tt class='method'>getdate_tz</tt></a></b> (<var>name</var>)
<dd>
Retrieve a header using <tt class="method">getheader()</tt> and parse it into a
10-tuple; the first 9 elements will make a tuple compatible with
<tt class="function">time.mktime()</tt>, and the 10th is a number giving the offset
of the date's timezone from UTC.  Note that fields 6, 7, and 8 
are not usable.  Similarly to <tt class="method">getdate()</tt>, if
there is no header matching <var>name</var>, or it is unparsable, return
<code>None</code>. 
</dl>

<P>
<tt class="class">Message</tt> instances also support a read-only mapping interface.
In particular: <code><var>m</var>[name]</code> is like
<code><var>m</var>.getheader(name)</code> but raises <tt class="exception">KeyError</tt> if
there is no matching header; and <code>len(<var>m</var>)</code>,
<code><var>m</var>.has_key(name)</code>, <code><var>m</var>.keys()</code>,
<code><var>m</var>.values()</code> and <code><var>m</var>.items()</code> act as expected
(and consistently).

<P>
Finally, <tt class="class">Message</tt> instances have two public instance variables:

<P>
<dl><dt><b><a name='l2h-2365'><tt class='member'>headers</tt></a></b>
<dd>
A list containing the entire set of header lines, in the order in
which they were read (except that setitem calls may disturb this
order). Each line contains a trailing newline.  The
blank line terminating the headers is not contained in the list.
</dl>

<P>
<dl><dt><b><a name='l2h-2366'><tt class='member'>fp</tt></a></b>
<dd>
The file or file-like object passed at instantiation time.  This can
be used to read the message content.
</dl>

<P>

<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="module-rfc822.html" tppabs="http://www.python.org/doc/current/lib/module-rfc822.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
  alt="Previous Page" width="32"></A></td>
<td><A href="module-rfc822.html" tppabs="http://www.python.org/doc/current/lib/module-rfc822.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
  alt="Up One Level" width="32"></A></td>
<td><A href="addresslist-objects.html" tppabs="http://www.python.org/doc/current/lib/addresslist-objects.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
  alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
  alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
  alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
  alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="module-rfc822.html" tppabs="http://www.python.org/doc/current/lib/module-rfc822.html">12.2 rfc822  </A>
<b class="navlabel">Up:</b> <a class="sectref" href="module-rfc822.html" tppabs="http://www.python.org/doc/current/lib/module-rfc822.html">12.2 rfc822  </A>
<b class="navlabel">Next:</b> <a class="sectref" href="addresslist-objects.html" tppabs="http://www.python.org/doc/current/lib/addresslist-objects.html">12.2.2 AddressList Objects</A>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>See <i><a href="about.html" tppabs="http://www.python.org/doc/current/lib/about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -