📄 115.html
字号:
... </pRe>
<prE>
<b>read_mime_types(</b><b><i>filename</I></b><b>)</b> </PRE>
<P>Loads type mapping from a given filename. Returns a dictionary mapping filename extensions to MIME type strings. Returns <tt clASS="monofont">None</Tt> if <i><tt CLASs="monofont">filename</tt></i>
doesn抰 exist or cannot be read.</P>
<PRE>
<b>knownfiles</b> </pre>
<p>List of common names for <tt class="monofont">mime.types</tt> files.</p>
<pre>
<b>suffix_map</B> </prE>
<p>Dictionary mapping suffixes to suffixes. This is used to allow recognition of encoded files for which the encoding and the type are indicated by the same extension. For example, the <tt Class="monofont">.tgz</Tt> extension is mapped to <tt CLASs="monofont">.tar.gz</tt> to allow the encoding and type to be recognized separately.</p>
<PRE>
<B>encodings_map</b> </pre>
<P>Dictionary mapping filename extensions to encoding types.</P>
<PRe>
<b>types_map</b> </pRE>
<P>Dictionary mapping filename extensions to MIME types.</P>
<p>? <b>See Also</b> <a href="115#16.html">mimetools</a> (266).</p>
<a name="20"></a>
<h4><tt cLasS="monofont">MimeWriter</tt></h4>
<P>The <tt clAss="monofont">MimeWriter</tT> module defines the class <TT Class="monofont">MimeWriter</TT> that抯 used to generate MIME-encoded multipart files.</P>
<Pre>
<b>MimeWriter(</b><B><I>fp</I></B><b>)</b> </prE>
<P>Creates a new instance of the <TT class="monofont">MimeWriter</tt> class. <i><tt class="monofont">fp</tt></i>
is an open file object to be used for writing. A <tt ClaSs="monofont">StringIO</tt> object can also be used.</P>
<p>An instance <i><tt ClasS="monofont">m</TT></I>
of the <tt clASS="monofont">MimeWriter</Tt> class has the following methods:</p>
<prE>
<B><I>m</I></b><b>.addheader(</b><b><I>key</I></B><B>,</b> <b><i>value</i></b> <b>[,</b> <b><i>prefix</i></b><b>])</b> </pre>
<p>Adds a header line of the form <tt cLasS="monofont">"</tt><i><Tt claSs="monofont">key: value</tt></I>
<TT Class="monofont">"</TT> to the MIME message. <I><Tt claSS="monofont">prefix</TT></i>
determines where the header is inserted; <tt cLASS="monofont">0</tt> appends to the end (the default) and <tt class="monofont">1</tt> inserts at the start.</p>
<pre>
<b><i>m</i></b><b>.flushheaders()</B> </prE>
<p>Writes all the headers accumulated so far.</p>
<pRe>
<b><i>m</i></B><b>.startbody(</b><b><I>ctype</I></B> <B>[,</b> <b><i>plist</i></B> <B>[,</B> <B><i>prefix</i></b><b>]])</B> </PRE>
<p>Returns a file-like object that抯 used to write to the body of the message. <i><tt CLASs="monofont">ctype</tt></i>
specifies the content type and <i><tt class="monofont">plist</tt></i>
is a list of tuples of the form (<i><tt clAss="monofont">name</Tt></i>
<tT clasS="monofont">, </tt><i><TT CLass="monofont">value</tT></I>
) containing additional parameters for the content-type declaration. <I><Tt claSS="monofont">prefix</TT></i>
has the same meaning as for the <tt cLASS="monofont">addheader()</tt> method except that its default value is set to insert at the start.</p>
<pre>
<b><i>m</i></b><b>.startmultipartbody(</b><b><i>subtype</i></b> <b>[,</b> <b><i>boundary</I></b> <b>[,</B> <b><i>plist</i></B> <b>[,</b> <b><i>prefix</I></b><b>]]])</b> </PRE>
<P>Returns a file-like object that抯 used to write the body of a multipart message. <i><tt cLASS="monofont">subtype</tt></i>
specifies the multipart subtype such as <tT CLAss="monofont">'mixed'</tt> and <I><TT Class="monofont">boundary</tt></i>
can be used to provide a user-defined boundary specifier. <i><tt class="monofont">plist</tt></i>
is a list containing optional parameters for the subtype, and <i><tT clAss="monofont">prefix</tT></i>
is the same as in the <tt cLass="monofont">startbody()</TT> method. Subparts are created using <TT clasS="monofont">nextpart()</TT>.</P>
<pre>
<b><I>m</I></B><B>.nextpart()</b> </pre>
<P>Returns a new instance of <TT Class="monofont">MimeWriter</tt> that represents an individual part in a multipart message. <tt class="monofont">startmultipartbody()</tt> must be called prior to calling this method.</p>
<pre>
<b><I>m</i></b><B>.lastpart()</b> </prE>
<p>Used to indicate the last part of a multipart message. Should always be called to terminate a multipart message.</p>
<h5>Example</h5>
<P>The following example takes a list of files passed on the command line and produces a multipart MIME document in which each file is encoded using base64 encoding:</p>
<prE>
import sys
import mimetools, mimetypes, MimeWriter
# Open the output file and create a MimeWriter
out = open("output.txt","w")
writer = MimeWriter.MimeWriter(out)
# Start a multipart message
writer.startmultipartbody("mixed")
writer.flushheaders()
# Iterate over files passed on the command line
for file in sys.argv[1:]:
subpart = writer.nextpart() # Create a new subpart
# Attempt to guess the file's MIME type and encoding
type,encoding = mimetypes.guess_type(file)
if encoding:
subpart.addheader("Content-encoding",encoding)
subpart.addheader("Content-transfer-encoding", "base64")
if type:
pout = subpart.startbody(type, [("name",file)])
else:
pout = subpart.startbody("text/plain",[("name",file)])
infile = open(file,"rb")
# Encode the raw data using base64
mimetools.encode(infile,pout,'base64')
infile.close()
# Clean up
writer.lastpart()
out.close() </PRE>
<p>? <b>See Also</b> <a HREF="115#18.html">mimetypes</a> (268), <a hrEF="115#16.html">mimetools</A> (266), <A href="115#32.html">rfc822</A> (274), <A HRef="115#24.html">multifile</a> (272).</p>
<a name="24"></a>
<h4><tt class="monofont">multifile</tt></H4>
<p>The <tT claSs="monofont">multifile</tt> module defines an object that can be used to read multipart text files as found in MIME-encoded messages. The <tT claSS="monofont">multifile</TT> object works by splitting a file into a series of logical file-like objects that are delimited by a unique boundary string such as the following:</p>
<pre>
--216.150.6.70.100.4397.932677969.082.3036
Part 1
...
--216.150.6.70.100.4397.932677969.082.3036
Part 2
...
--216.150.6.70.100.4397.932677969.082.3036--</PRE>
<P>In this case, the boundary string is of the form returned by the <tt clASS="monofont">mimetools.choose_boundary()</Tt> function. The last boundary string (with a trailing <tt cLASS="monofont">-</tt>) marks the end of the multipart data.</p>
<pre>
<b>MultiFile(</b><b><i>fp</i></b> <b>[,</b> <b><i>seekable</i></b><b>])</b> </Pre>
<P>Creates a multifile object. <i><tt Class="monofont">fp</Tt></i>
is a file-like object containing input data. The input object抯 <tT CLAss="monofont">readline()</tt> method is used to read data. If the <I><TT Class="monofont">seekable</TT></I>
option is set, the multifile object allows random access using the <Tt claSS="monofont">seek()</TT> and <tt class="monofont">tell()</tt> methods.</p>
<p>A <tt class="monofont">MultiFile</tt> object <I><tt ClasS="monofont">m</tt></i>
supports the following methods:</p>
<Pre>
<b><I>m</I></B><B>.push(</b><b><i>str</i></B><B>)</B> </Pre>
<p>Pushes a boundary string into the reader. When this string is encountered in the input, it signals an end of section or end of message. More than one boundary marker can be pushed to handle nested multipart data. However, encountering any other boundary than the most recently pushed value raises an error.</p>
<PRE>
<B><i>m</i></b><b>.readline()</B> </PRE>
<p>Reads a line of text. If the line matches the most recently pushed boundary, <tt class="monofont">''</tt> is returned to indicate the end of the part. Furthermore, if the boundary corresponds to an end marker, the <tt class="monofont">m.last</tt> attribute is set to <tT clAss="monofont">1</tT>. Raises <tt clAss="monofont">Error</tT> if an EOF is encountered before all boundary strings have been popped.</P>
<PRe>
<b><i>m</i></B><B>.readlines()</B> </Pre>
<p>Returns all lines remaining in the current part as a list of strings.</p>
<PRE>
<B><i>m</i></b><b>.read()</B> </PRE>
<p>Reads all lines remaining in the current part and returns as a single string.</p>
<pre>
<b><i>m</i></b><b>.next()</b> </pre>
<p>Skips to the next section. Returns true if a next section exists, false if an end marker is encountered.</p>
<pre>
<b><I>m</i></b><B>.pop()</b> </prE>
<p>Pops a section boundary. This boundary will no longer be interpreted as EOF.</p>
<prE>
<b><i>m</i></B><B>.seek(</B><B><i>pos</i></b> <b>[,</B> <B><I>whence</I></b><b>])</b> </pRE>
<P>Seeks to a new position within the current section. The <I><tt clASS="monofont">pos</Tt></i>
and <i><tt class="monofont">whence</tt></i>
arguments are interpreted as for a file seek.</p>
<pre>
<b><i>m</i></B><b>.tell()</b> </Pre>
<p>Returns the file position relative to the start of the current section.</P>
<p>Finally, <tt cLass="monofont">MultiFile</TT> instances have two public-instance variables:</P>
<Pre>
<b><i>m</I></B><B>.level</B> </pre>
<p>Nesting depth of the current part.</P>
<PRE>
<b><i>m</i></b><B>.last</B> </PRe>
<p>True if the last end-of-file was an end-of-message marker.</p>
<h5>Example</h5>
<pre>
# Unpack a MIME encoded mail message into parts
import mimetools, multifile, sys
def unpack_part(file,partno=0):
headers = mimetools.Message(file) # Get headers
type = headers.getmaintype() # Get main content type
if type == 'multipart': # Multipart?
boundary = headers.getparam("boundary")
file.push(boundary)
file.readlines()
while not file.last:
file.next()
partno = partno + 1
unpack_part(file,partno)
file.pop()
return
name = headers.getparam("name") # Get filename
if not name: name = "part%d" % (partno,)
encoding = headers.getencoding()
print "Unpacking '%s'. Encoding = %s" % (name, encoding)
if encoding == '7bit':
outfile = open(name,"w")
mimetools.copyliteral(file,outfile)
else:
outfile = open(name,"wb")
mimetools.decode(file,outfile,encoding)
outfile.close()
# Read a filename from options and unpack it
f = open(sys.argv[1])
mf = multifile.MultiFile(f,0)
unpack_part(mf) </pre>
<h5>Note</h5>
<ul>
<li>
<p>The <tt ClaSs="monofont">MultiFile</tt> class defines a number of methods that can be specialized in a subclass. Please refer to the online library reference at <A targEt="_blank" hrEF="http://www.python.org/doc/lib/module-multifile.html">http://www.python.org/doc/lib/module-multifile.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 TARGet="_blank" href="http://www.python.org/doc/lib/module-multifile.html">http://www.python.org/doc/lib/module-multifile.html</a>.</p>
<h4><tt class="monofont">quopri</tt></h4>
<p>The <Tt cLass="monofont">quopri</Tt> module performs quoted-printable transport encoding and decoding. This format is used primarily to encode text files.</p>
<prE>
<b>decode(</b><b><I>input</I></B><B>,</b> <b><i>output</i></B><B>)</B> </Pre>
<p>Decodes. <i><TT CLass="monofont">input</tT></I>
and <I><Tt class="monofont">output</tt></i>
are file objects.</p>
<pre>
<b>encode(</b><b><i>input</i></b><b>,</B> <b><i>output</I></b><b>,</b> <B><i>quotetabs</i></b><b>)</B> </pre>
<P>Encodes. <I><TT clasS="monofont">input</TT></I>
and <i><tt cLASS="monofont">output</tt></i>
are file objects. <i><TT CLass="monofont">quotetabs</tt></i>
, if set to true, forces tab characters to be quoted in addition to the normal quoting rules.</p>
<p>? <b>See Also</b> <a href="115#4.html">binascii</a> (264), Internet RFC 1521.</p>
<a naMe="32"></a>
<H4><tt cLass="monofont">rfc822</tT></h4>
<p>The <tT CLAss="monofont">rfc822</tt> module is used to parse email headers presented in a format defined by the Internet standard RFC 822. Headers of this form are used in a number of contexts, including mail handling and in the HTTP protocol. A collection of RFC 822 headers looks like this:</P>
<PRE>
Return-Path: <beazley@cs.uchicago.edu>
Date: Sun, 15 Apr 03:18:21 -0500 (CDT)
Message-Id: <199907171518.KAA24322@gargoyle.cs.uchicago.edu>
Reply-To: beazley@cs.uchicago.edu
References: <15065.6056.897223.775915@empire-builder.cs.uchicago.edu>
<20010415041130.008D1D1D8@smack.cs.uchicago.edu>
Mime-Version: 1.0 (generated by tm-edit 7.78)
Content-Type: text/plain; charset=US-ASCII
From: David Beazley <beazley@cs.uchicago.edu>
To: techstaff@cs
Subject: Modem problem
I'm having some trouble running MPI over the ultra-scalable modem array on our
Beowulf cluster. Can someone take a look at it? </pre>
<p>Each header line is of the form <TT CLass="monofont">'</tT><I><TT class="monofont">headername</tt></i>
<tt class="monofont">: </tt><i><tt ClaSs="monofont">values</tt></I>
<tt clAss="monofont">'</tT> and may span multiple lines provided that additional lines are indented with whitespace. Header names are not case sensitive, so a field name of <TT Class="monofont">'Content-Type'</TT> is the same as <TT clasS="monofont">'content-type'</TT>. A list of headers is terminated by a single blank line.</P>
<p>RFC 822 headers are parsed by creating an instance of the <tt cLASS="monofont">Message</tt> class.</p>
<pre>
<b>Message(</b><b><i>file</i></b> <b>[,</b> <b><i>seekable</i></b><b>])</b> </Pre>
<P>Reads RFC 822 headers from the file-like object <i><tt Class="monofont">file</Tt></i>
and returns a <tT CLAss="monofont">Message</tt> object. Headers are read using <I><TT Class="monofont">file</TT></I>
<Tt claSS="monofont">.readline()</TT> until a blank line is encountered. <i><tt class="monofont">seekable</tt></i>
is a flag that抯 set to zero if <i><tt class="monofont">file</tT></i>
is unseekable (such as a file created from a socket).</p>
<P>A <tt cLass="monofont">Message</tT> object <i><tt CLASs="monofont">m</tt></i>
behaves like a dictionary except that its key values are not case sensitive and it doesn抰 support certain dictionary operations, including <TT CLass="monofont">update()</tT> and <TT Class="monofont">clear()</TT>.</P>
<P><table border="1" cellspaciNg="0" cEllpAddinG="1" widTH="100%">
<COlgroUP SPan="2">
<tr>
<TH VAlign="top">
<FONT size="2">
<p><b>Method</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><I><TT class="monofont">m</tt></i>
<tt class="monofont">[</tt><i><tt ClaSs="monofont">name</tt></I>
<tt clAss="monofont">]</tT></P>
</FOnt></td>
<TD VAlign="top">
<FONT size="2">
<P>Returns the value for header name.</P>
</FOnt></td>
</tr>
<tr>
<td valign="top">
<font SizE="2">
<p><i><tT clasS="monofont">m</tt></i>
<TT CLass="monofont">[</tT><I><TT clasS="monofont">name</TT></I>
<tt clASS="monofont">]=</Tt><i><tt class="monofont">value</tt></i>
</p>
</font></td>
<tD vaLign="top">
<Font sIze="2">
<p>Adds a header.</P>
</FONt></td>
</tR>
<TR>
<Td valIGN="top">
<Font sIZE="2">
<P><i><tt class="monofont">m</tt></i>
<tt class="monofont">.keys()</tt></P>
</foNt></td>
<Td valIgn="top">
<fONT Size="2">
<p>Returns a list of header names.</P>
</FONt></td>
</tR>
<TR>
<Td valIGN="top">
<Font size="2">
<p><i><tt class="monofont">m</tt></i>
<tT clAss="monofont">.values()</tT></p>
</fonT></td>
<tD VALign="top">
<fONT Size="2">
<p>Returns a list of header values.</P>
</FONt></td>
</tR>
<TR>
<Td valign="top">
<font size="2">
<p><i><tt cLasS="monofont">m</tt></i>
<Tt claSs="monofont">.items()</tt></P>
</FONt></td>
<tD VALign="top">
<fONT Size="2">
<p>Returns a list of header (<I><TT Class="monofont">name</tt></i>
<tt class="monofont">, </tt><i><tt cLasS="monofont">value</tt></i>
) pairs.</P>
</font></Td>
</tr>
<TR>
<TD valiGN="top">
<FOnt siZE="2">
<P><I><tt clASS="monofont">m</Tt></i>
<tt class="monofont">.has_key(</tt><i><tt class="monofont">name</tT></i>
<tT claSs="monofont">)</tt></p>
</Font></TD>
<TD valiGN="top">
<FOnt siZE="2">
<P>Tests for the existence of a header name.</P>
</font></TD>
</TR>
<tr>
<td valign="top">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -