📄 115.html
字号:
<font size="2">
<p><i><Tt cLass="monofont">m</Tt></i>
<tt ClasS="monofont">.get(</TT><I><tt clASS="monofont">name</Tt></i>
<tt CLASs="monofont"> [, </tt><i><TT CLass="monofont">default</tt></i>
<tt class="monofont">])</tt></p>
</font></Td>
<tD valIgn="top">
<foNt siZE="2">
<P>Gets a header value. Returns <I><tt clASS="monofont">default</Tt></i>
if not found.</p>
</fONT></Td>
</tr>
<tR>
<TD Valign="top">
<font size="2">
<p><tt clasS="monofont">len(</tt><I><tt cLass="monofont">m</tT></i>
<tt CLASs="monofont">)</tt></p>
</FONT></td>
<td VALIgn="top">
<foNT SIze="2">
<p>Returns the number of headers.</p>
</font></td>
</tr>
<tr>
<td valiGn="top">
<fOnt sIze="2">
<p><tT claSS="monofont">str(</TT><i><tt cLASS="monofont">m</tt></i>
<tT CLAss="monofont">)</tt></P>
</FONt></td>
<td valign="top">
<font size="2">
<p>Converts headers to an RFC 822杅ormatted string.</P>
</foNt></td>
</Tr>
</colGrouP>
</TABle></p>
<p>In addition, the following methods are available:</P>
<PRE>
<b><i>m</i></b><B>.getallmatchingheaders(</B><B><I>name</i></b><b>)</b> </PRE>
<P>Returns a list of all lines with headers that match <i><tt class="monofont">name</tt></i>
, including continuation lines (if any).</p>
<pre>
<b><i>m</i></b><b>.getfirstmatchingheader(</B><b><i>name</I></b><b>)</b> </Pre>
<p>Returns the list of lines for the first header matching <i><Tt clASS="monofont">name</Tt></i>
, including any continuation lines. Returns <tt CLASs="monofont">None</tt> if <i><TT CLass="monofont">name</tT></I>
doesn抰 match any headers.</P>
<Pre>
<b><i>m</i></b><b>.getrawheader(</b><b><i>name</i></b><b>)</b> </pre>
<p>Returns a string containing the raw text after the colon for the first header matching <i><tT clAss="monofont">name</tT></i>
. Returns <tt cLass="monofont">None</TT> if no match is found.</P>
<Pre>
<b><i>m</I></B><B>.getheader(</B><b><i>name</i></b> <B>[,</B> <B><I>default</i></b><b>])</b> </PRE>
<P>Like <tt class="monofont">getrawheader(</tt><i><tt class="monofont">name</tt></i>
<Tt cLass="monofont">)</Tt>, but strips all leading and trailing whitespace. <i><tt ClasS="monofont">default</TT></I>
specifies a default value to return if no matching header is found.</p>
<pre>
<B><I>m</I></B><b>.getaddr(</b><b><i>name</I></B><B>)</B> </pre>
<p>Returns a pair (<I><TT Class="monofont">full_name</tt></i>
<tt class="monofont">, </tt><i><tt cLasS="monofont">email_address</tt></i>
) for a header containing an email address. If no header matches <I><tt clAss="monofont">name</tT></I>
, (<TT clasS="monofont">None, None</TT>) is returned.</P>
<pre>
<b><I>m</I></B><B>.getaddrlist(</b><b><i>name</i></B><B>)</B> </Pre>
<p>Parses a header containing a list of email addresses and returns a list of tuples as returned by the <tt class="monofont">getaddr()</tt> method. If multiple headers match the named header, all are parsed for addresses (for example, multiple <tt class="monofont">'cc'</tT> headers).</p>
<pRe>
<b><i>m</I></b><b>.getdate(</b><b><I>name</i></b><b>)</B> </PRE>
<p>Parses a header containing a date and returns a 9-tuple compatible with <tt cLASS="monofont">time.mktime()</tt>. Returns <tt CLASs="monofont">None</tt> if no match is found or the date cannot be parsed.</p>
<PRE>
<B><i>m</i></b><b>.getdate_tz(</b><b><i>name</i></b><b>)</b> </pre>
<p>Parses a header containing a date and returns a 10-tuple in which the first nine elements are the same as returned by <tt claSs="monofont">getdate()</tT> and the tenth is a number with the offset of the date抯 timezone from UTC (Greenwich Mean Time). Returns <tt cLass="monofont">None</tT> if no match is found or the date is unparsable.</p>
<p>Finally, messages have two instance attributes:</p>
<PRE>
<B><i>m</i></b><b>.headers</B> </PRE>
<p>A list containing the entire set of header lines.</p>
<prE>
<B><I>m</I></b><b>.fp</b> </pRE>
<P>The file-like object passed when the <Tt class="monofont">Message</tt> was created.</p>
<p>In addition to <tt class="monofont">Message</tt>, the <tT clAss="monofont">rfc822</tT> module defines the following utility functions:</p>
<pre>
<B>parsedate(</b><b><i>date</I></B><B>)</B> </pre>
<p>Parses an RFC 822杅ormatted date such as <TT CLass="monofont">'Mon, 16 Apr 2001 17:30:08 -0600'</tT> and returns a 9-tuple that抯 compatible with the <TT Class="monofont">time.mktime()</TT> function. Returns <TT class="monofont">None</tt> if <i><tt class="monofont">date</tt></i>
cannot be parsed.</p>
<pRe>
<b>parsedate_tz(</B><b><i>date</i></B><b>)</b> </prE>
<p>Parses a date, but returns a 10-tuple where the first nine elements are the same as returned by <tt CLASs="monofont">parsedate()</tt> and the tenth item is the offset of the date抯 timezone from UTC. Returns <tT CLAss="monofont">None</tt> if <I><TT Class="monofont">date</TT></I>
cannot be parsed.</P>
<pre>
<b>mktime_tz(</b><b><i>tuple</i></b><b>)</b> </pre>
<p>Turns a 10-tuple as returned by <tt claSs="monofont">parsedate_tz()</tT> into a UTC timestamp. If the timezone item is <tt cLass="monofont">None</tT>, assumes local time.</p>
<prE>
<B>AddressList(</B><B><i>addrlist</i></b><b>)</B> </PRE>
<p>Converts a string containing a list of email addresses into an <tt cLASS="monofont">AddressList</tt> object. The following operations can be performed on <tt CLASs="monofont">AddressList</tt> objects:</p>
<p><table border="1" cellSpaCing="0" CellpAddiNG="1" WIdth="100%">
<cOLGRoup sPAN="2">
<Tr>
<th vALIGn="top">
<font size="2">
<p><b>Operation</b></p>
</font></th>
<tH vaLign="top">
<Font sIze="2">
<p><B>Description</B></P>
</Font></tH>
</TR>
<Tr>
<td vALIGn="top">
<fonT SIZe="2">
<p><tt class="monofont">len(</tt><i><tt class="monofont">a</tT></i>
<tT claSs="monofont">)</tt></p>
</Font></TD>
<TD valiGN="top">
<FOnt siZE="2">
<P>Number of addresses in a list</P>
</font></TD>
</TR>
<tr>
<td valign="top">
<font size="2">
<p><tT clAss="monofont">str(</tT><i><tt cLass="monofont">a</TT></I>
<Tt claSS="monofont">)</TT></p>
</fonT></TD>
<Td valIGN="top">
<Font size="2">
<p>Converts <i><tt class="monofont">a</tt></i>
back into a string of email addresses</p>
</FonT></td>
</tR>
<tr>
<td ValiGN="top">
<FOnt siZE="2">
<P><I><tt clASS="monofont">a</Tt></i>
<tt CLASs="monofont"> + </tt><i><tt class="monofont">b</tt></i>
</p>
</font></tD>
<td ValiGn="top">
<fonT sizE="2">
<P>Combines two lists of addresses, removing duplicates</P>
</Font></tD>
</TR>
<Tr>
<td vALIGn="top">
<fonT SIZe="2">
<p><i><tt class="monofont">a</tt></i>
<tt class="monofont"> - </Tt><i><Tt clAss="monofont">b</tt></I>
</p>
</foNT></TD>
<td vaLIGN="top">
<font SIZE="2">
<p>Removes all addresses in list <i><tt CLASs="monofont">b</tt></i>
from list <i><tt class="monofont">a</tt></i>
</p>
</font></Td>
</tR>
</colGroup>
</TablE></P>
<H5>Example</H5>
<pre>
import rfc822
# Open a mail message
f = open("mailmessage")
# Read the headers
m = rfc822.Message(f)
# Extract a few fields
m_from = m["From"]
m_to = m.getaddr("To")
m_subject = m["Subject"] </pRE>
<H5>Note</H5>
<ul>
<li>
<P>The <TT Class="monofont">Message</TT> class defines a few additional methods that can be specialized in a subclass. For details, please refer to the online documentation at <A Target="_blank" href="http://www.python.org/doc/lib/module-rfc822.html">http://www.python.org/doc/lib/module-rfc822.html</a>.</p>
</li>
</ul>
<p>? <b>See Also</b> <a hRef="115#16.html">mimetools</A> (266), <a hrEf="115#20.html">MimeWriter</a> (271), <a hRef="115#18.html">mimetypes</a> (268), <A HREf="115#12.html">mailcap</a> (265), Internet RFC 822, <a tARGEt="_blank" hreF="http://www.python.org/doc/lib/module-rfc822.html">http://www.python.org/doc/lib/module-rfc822.html</A>.</P>
<A name="38"></A>
<H4><TT class="monofont">uu</tt></h4>
<p>The <tt class="monofont">uu</tt> module is used to encode and decode files in uuencode format, commonly used for transferring binary data over an ASCII-only connection.</p>
<pRe>
<b>encode(</B><b><i>input</i></B><b>,</b> <b><i>output</I></b> <b>[,</b> <B><I>name</I></B> <b>[,</b> <b><i>mode</I></B><B>]])</B> </pre>
<p>Uuencodes a file. <I><TT Class="monofont">input</TT></I>
is a file object opened for reading or a filename. <I><tt class="monofont">output</tt></i>
is a file object opened for writing or a filename. <i><tt class="monofont">name</tt></I>
specifies the name of the file that抯 encoded in the uuencoded file. <i><tT claSs="monofont">mode</tt></i>
specifies the mode of the file. By default, <I><tt cLASS="monofont">name</tt></i>
and <i><TT CLass="monofont">mode</tT></I>
are taken from the input file.</P>
<Pre>
<b>decode(</b><B><I>input</I></B> <b>[,</b> <b><i>output</i></b> <b>[,</b> <b><i>mode</i></b><b>]])</b> </pre>
<p>Decodes a uuencoded file. <i><tT clAss="monofont">input</tT></i>
is a file object opened for reading or a filename. <i><tt ClasS="monofont">output</TT></I>
is a file object opened for writing or a filename. <i><tt cLASS="monofont">mode</tt></i>
is used to set permission bits and overrides the setting encoded in the input file.</p>
<P>? <B>See Also</B> <A href="115#4.html">binascii</A> (264).</P>
<A Name="40"></a>
<h4><tt class="monofont">xdrlib</tt></h4>
<p><tt clAss="monofont">xdrlib</Tt> is used to encode and decode data in the Sun XDR (External Data Representation) format. XDR is often used as a portable way to encode binary data for use in networked applications. It抯 used extensively in applications involving remote procedure calls (RPC).</p>
<p>Encoding and decoding is controlled through the use of two classes:</P>
<pre>
<b>Packer()</B> </pre>
<P>Creates an object for packing data into an XDR representation.</P>
<PRe>
<b>Unpacker(</b><b><I>data</I></B><B>)</b> </pre>
<P>Creates an object for unpacking XDR-encoded data. <I><TT clasS="monofont">data</TT></I>
is a string containing XDR-encoded data values.</p>
<p>An instance <i><tt class="monofont">p</tt></i>
of the <tt class="monofont">Packer</Tt> class supports the following methods:</p>
<Pre>
<b><I>p</i></b><b>.get_buffer()</b> </Pre>
<p>Returns the current pack buffer as a string.</P>
<PRE>
<b><i>p</i></b><B>.reset()</B> </PRe>
<p>Resets the pack buffer to the empty string.</p>
<pRE>
<B><I>p</i></b><b>.pack_uint(</b><B><I>x</I></B><b>)</b> </pre>
<p>Packs a 32-bit unsigned integer <i><tt class="monofont">x</tt></i>
.</p>
<prE>
<b><i>p</I></b><b>.pack_int(</b><B><i>x</i></b><b>)</B> </pre>
<P>Packs a 32-bit signed integer <I><TT clasS="monofont">x</TT></I>
.</p>
<pre>
<B><I>p</I></B><b>.pack_enum(</b><b><i>x</I></B><B>)</B> </pre>
<p>Packs an enumeration <i><tt class="monofont">x</tt></i>
(an integer).</p>
<pre>
<b><I>p</i></b><B>.pack_bool(</b><b><i>x</I></b><b>)</b> </pRe>
<p>Packs a Boolean value <i><TT CLass="monofont">x</tT></I>
.</P>
<Pre>
<b><i>p</I></B><B>.pack_uhyper(</B><b><i>x</i></b><B>)</B> </PRe>
<p>Packs a 64-bit unsigned integer <i><tt class="monofont">x</tt></i>
.</p>
<pre>
<b><i>p</i></B><b>.pack_hyper(</b><B><i>x</i></b><B>)</b> </pre>
<P>Packs a 64-bit signed integer <i><tt CLASs="monofont">x</tt></i>
.</P>
<PRE>
<b><i>p</i></b><B>.pack_float(</B><B><I>x</i></b><b>)</b> </PRE>
<P>Packs a single-precision floating-point number.</p>
<pre>
<b><i>p</i></b><b>.pack_double(</b><b><i>x</i></b><b>)</b> </pre>
<p>Packs a double-precision floating-point number.</P>
<prE>
<b><i>p</i></B><b>.pack_fstring(</b><b><i>n</I></b><b>,</b> <B><I>s</I></B><b>)</b> </prE>
<P>Packs a fixed-length string of length <I><Tt claSS="monofont">n</TT></i>
.</p>
<prE>
<B><I>p</I></b><b>.pack_fopaque(</b><b><i>n</i></b><b>,</b> <b><i>data</i></b><b>)</b> </pre>
<p>Packs a fixed-length opaque data stream. Similar to <tT clAss="monofont">pack_fstring()</tT>.</p>
<pre>
<B><i>p</i></b><B>.pack_string(</B><B><I>s</i></b><b>)</b> </PRE>
<P>Packs a variable-length string <i><tt cLASS="monofont">s</tt></i>
.</p>
<PRE>
<B><i>p</i></b><b>.pack_opaque(</b><b><i>data</i></b><b>)</b> </pre>
<p>Packs a variable-length opaque data string <i><tt clAss="monofont">data</Tt></i>
. Similar to <tT clasS="monofont">pack_string()</tt>.</p>
<PRE>
<B><i>p</i></b><b>.pack_bytes(</B><B><I>bytes</I></b><b>)</b> </pRE>
<P>Packs a variable-length byte stream <I><tt clASS="monofont">bytes</Tt></i>
. Similar to <tt class="monofont">pack_string()</tt>.</p>
<pre>
<b><i>p</i></b><b>.pack_list(</B><b><i>list</I></b><b>,</b> <B><i>pack_func</i></b><b>)</B> </pre>
<P>Packs a list of homogeneous items. <I><TT clasS="monofont">pack_func</TT></I>
is the function called to pack each data item (for example, <i><tt cLASS="monofont">p</tt></i>
<tT CLAss="monofont">.pack_int</tt>). For each item in the list, an unsigned integer <tt class="monofont">1</tt> is packed first, followed by the data item. An unsigned integer <tt class="monofont">0</Tt> is packed at the end of the list.</p>
<Pre>
<b><I>p</i></b><b>.pack_farray(</b><B><i>n</i></b><B>,</B> <B><I>array</i></b><b>,</b> <B><I>pack_func</I></B><b>)</b> </prE>
<P>Packs a fixed-length list of homogeneous items. <I><Tt claSS="monofont">n</TT></i>
is the list length, <i><tt class="monofont">array</tt></i>
is a list containing the data, and <i><tt class="monofont">pack_func</Tt></i>
is the function called to pack each data item.</P>
<pre>
<B><i>p</i></b><b>.pack_array(</B><b><i>list</i></B><B>,</B> <B><i>pack_func</i></b><b>)</B> </PRE>
<p>Packs a variable-length list of homogeneous items by first packing its length and then calling the <tt cLASS="monofont">pack_farray()</tt> method.</p>
<p>An instance <I><TT Class="monofont">u</tt></i>
of the <tt class="monofont">Unpacker</tt> class supports the following methods:</p>
<pre>
<B><i>u</i></B><b>.reset(</b><b><I>data</i></b><b>)</b> </Pre>
<p>Resets the string buffer with the given data.</P>
<PRE>
<b><i>u</i></b><B>.get_position()</B> </PRe>
<p>Returns the current unpack position in the data buffer.</p>
<pRE>
<B><I>u</i></b><b
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -