Files
hustoj/web/python-docs/library/mailcap.html
2025-03-09 17:10:56 +08:00

286 lines
19 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
<head>
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<title>mailcap --- Mailcap 文件处理 &#8212; Python 3.8.20 文档</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/language_data.js"></script>
<script src="../_static/translations.js"></script>
<script src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="在 Python 3.8.20 文档 中搜索"
href="../_static/opensearch.xml"/>
<link rel="author" title="关于这些文档" href="../about.html" />
<link rel="index" title="索引" href="../genindex.html" />
<link rel="search" title="搜索" href="../search.html" />
<link rel="copyright" title="版权所有" href="../copyright.html" />
<link rel="next" title="mailbox --- 操作多种格式的邮箱" href="mailbox.html" />
<link rel="prev" title="json --- JSON 编码和解码器" href="json.html" />
<link rel="canonical" href="https://docs.python.org/3/library/mailcap.html" />
<style>
@media only screen {
table.full-width-table {
width: 100%;
}
}
</style>
<link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/menu.js"></script>
</head>
<body>
<div class="mobile-nav">
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
<label for="menuToggler" class="toggler__label">
<span></span>
</label>
<nav class="nav-content" role="navigation">
<a href="https://www.python.org/" class="nav-logo">
<img src="../_static/py.svg" alt="Logo"/>
</a>
<div class="version_switcher_placeholder"></div>
<form role="search" class="search" action="../search.html" method="get">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
<path fill-rule="nonzero"
d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z" fill="#444"></path>
</svg>
<input type="text" name="q" aria-label="快速搜索"/>
<input type="submit" value="转向"/>
</form>
</nav>
<div class="menu-wrapper">
<nav class="menu" role="navigation" aria-label="main navigation">
<div class="language_switcher_placeholder"></div>
<h4>上一个主题</h4>
<p class="topless"><a href="json.html"
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">json</span></code> --- JSON 编码和解码器</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="mailbox.html"
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">mailbox</span></code> --- 操作多种格式的邮箱</a></p>
<div role="note" aria-label="source link">
<h3>本页</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">报告 Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/mailcap.rst"
rel="nofollow">显示源代码
</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
这个文档所针对的是一个已不再受支持的 Python 旧版本。
你应当升级版本,并阅读
<a href="/3/library/mailcap.html"> Python 当前稳定版本的文档</a>.
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>导航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="总目录"
accesskey="I">索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模块索引"
>模块</a> |</li>
<li class="right" >
<a href="mailbox.html" title="mailbox --- 操作多种格式的邮箱"
accesskey="N">下一页</a> |</li>
<li class="right" >
<a href="json.html" title="json --- JSON 编码和解码器"
accesskey="P">上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="netdata.html" accesskey="U">互联网数据处理</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="module-mailcap">
<span id="mailcap-mailcap-file-handling"></span><h1><a class="reference internal" href="#module-mailcap" title="mailcap: Mailcap file handling."><code class="xref py py-mod docutils literal notranslate"><span class="pre">mailcap</span></code></a> --- Mailcap 文件处理<a class="headerlink" href="#module-mailcap" title="永久链接至标题"></a></h1>
<p><strong>源代码:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.8/Lib/mailcap.py">Lib/mailcap.py</a></p>
<hr class="docutils" />
<p>Mailcap 文件可用来配置支持 MIME 的应用例如邮件阅读器和 Web 浏览器如何响应具有不同 MIME 类型的文件。 &quot;mailcap&quot; 这个名称源自短语&quot;mail capability&quot;。) 例如,一个 mailcap 文件可能包含 <code class="docutils literal notranslate"><span class="pre">video/mpeg;</span> <span class="pre">xmpeg</span> <span class="pre">%s</span></code> 这样的行。 然后,如果用户遇到 MIME 类型为 <em class="mimetype">video/mpeg</em> 的邮件消息或 Web 文档时,<code class="docutils literal notranslate"><span class="pre">%s</span></code> 将被替换为一个文件名(通常是一个临时文件)并且将自动启动 <strong class="program">xmpeg</strong> 程序来查看该文件。</p>
<p>mailcap 格式的文档见 <span class="target" id="index-4"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc1524.html"><strong>RFC 1524</strong></a>, &quot;A User Agent Configuration Mechanism For Multimedia Mail Format Information&quot;,但它并不是一个因特网标准。 不过mailcap 文件在大多数 Unix 系统上都受到支持。</p>
<dl class="function">
<dt id="mailcap.findmatch">
<code class="sig-prename descclassname">mailcap.</code><code class="sig-name descname">findmatch</code><span class="sig-paren">(</span><em class="sig-param">caps</em>, <em class="sig-param">MIMEtype</em>, <em class="sig-param">key='view'</em>, <em class="sig-param">filename='/dev/null'</em>, <em class="sig-param">plist=[]</em><span class="sig-paren">)</span><a class="headerlink" href="#mailcap.findmatch" title="永久链接至目标"></a></dt>
<dd><p>返回一个 2 元组;其中第一个元素是包含所要执行命令的字符串 (它可被传递给 <a class="reference internal" href="os.html#os.system" title="os.system"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.system()</span></code></a>),第二个元素是对应于给定 MIME 类型的 mailcap 条目。 如果找不到匹配的 MIME 类型,则将返回 <code class="docutils literal notranslate"><span class="pre">(None,</span> <span class="pre">None)</span></code></p>
<p><em>key</em> 是所需字段的名称,它代表要执行的活动类型;默认值是 'view',因为在最通常的情况下你只是想要查看 MIME 类型数据的正文。 其他可能的值还有 'compose' 和 'edit',分别用于想要创建给定 MIME 类型正文或修改现有正文数据的情况。 请参阅 <span class="target" id="index-5"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc1524.html"><strong>RFC 1524</strong></a> 获取这些字段的完整列表。</p>
<p><em>filename</em> 是在命令行中用来替换 <code class="docutils literal notranslate"><span class="pre">%s</span></code> 的文件名;默认值 <code class="docutils literal notranslate"><span class="pre">'/dev/null'</span></code> 几乎肯定不是你想要的,因此通常你要通过指定一个文件名来覆盖它。</p>
<p><em>plist</em> 可以是一个包含命名形参的列表;默认值只是一个空列表。 列表中的每个条目必须为包含形参名称的字符串、等于号 (<code class="docutils literal notranslate"><span class="pre">'='</span></code>) 以及形参的值。 Mailcap 条目可以包含形如 <code class="docutils literal notranslate"><span class="pre">%{foo}</span></code> 的命名形参,它将由名为 'foo' 的形参的值所替换。 例如,如果命令行 <code class="docutils literal notranslate"><span class="pre">showpartial</span> <span class="pre">%{id}</span> <span class="pre">%{number}</span> <span class="pre">%{total}</span></code> 是在一个 mailcap 文件中,并且 <em>plist</em> 被设为 <code class="docutils literal notranslate"><span class="pre">['id=1',</span> <span class="pre">'number=2',</span> <span class="pre">'total=3']</span></code>,则结果命令行将为 <code class="docutils literal notranslate"><span class="pre">'showpartial</span> <span class="pre">1</span> <span class="pre">2</span> <span class="pre">3'</span></code></p>
<p>在 mailcap 文件中,可以指定可选的 &quot;test&quot; 字段来检测某些外部条件(例如所使用的机器架构或窗口系统)来确定是否要应用 mailcap 行。 <a class="reference internal" href="#mailcap.findmatch" title="mailcap.findmatch"><code class="xref py py-func docutils literal notranslate"><span class="pre">findmatch()</span></code></a> 将自动检查此类条件并在检查未通过时跳过条目。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.8.16 版更改: </span>为了防止使用 shell 元字符(在 shell 命令行中具有特殊效果的符号)的安全问题,<code class="docutils literal notranslate"><span class="pre">findmatch</span></code> 会拒绝把字母数字和 <code class="docutils literal notranslate"><span class="pre">&#64;+=:,./-_</span></code> 以外的 ASCII 字符注入被返回的命令行。</p>
<p>如果有不被允许的字符出现在 <em>filename</em> 中,<code class="docutils literal notranslate"><span class="pre">findmatch</span></code> 将总是返回 <code class="docutils literal notranslate"><span class="pre">(None,</span> <span class="pre">None)</span></code> 就如同未找到任何条目一样。 如果这样的字符出现在其他地方(在 <em>plist</em><em>MIMEtype</em> 中的值,<code class="docutils literal notranslate"><span class="pre">findmatch</span></code> 将忽略所有使用这些值的 mailcap 条目。 在两种情况下都将引发 <a class="reference internal" href="warnings.html#module-warnings" title="warnings: Issue warning messages and control their disposition."><code class="xref py py-mod docutils literal notranslate"><span class="pre">警告</span></code></a></p>
</div>
</dd></dl>
<dl class="function">
<dt id="mailcap.getcaps">
<code class="sig-prename descclassname">mailcap.</code><code class="sig-name descname">getcaps</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailcap.getcaps" title="永久链接至目标"></a></dt>
<dd><p>返回一个将 MIME 类型映射到 mailcap 文件条目列表的字典。 此字典必须被传给 <a class="reference internal" href="#mailcap.findmatch" title="mailcap.findmatch"><code class="xref py py-func docutils literal notranslate"><span class="pre">findmatch()</span></code></a> 函数。 条目会被存储为字典列表,但并不需要了解此表示形式的细节。</p>
<p>此信息来自在系统中找到的所有 mailcap 文件。 用户的 mailcap 文件 <code class="file docutils literal notranslate"><span class="pre">$HOME/.mailcap</span></code> 中的设置将覆盖系统 mailcap 文件 <code class="file docutils literal notranslate"><span class="pre">/etc/mailcap</span></code>, <code class="file docutils literal notranslate"><span class="pre">/usr/etc/mailcap</span></code><code class="file docutils literal notranslate"><span class="pre">/usr/local/etc/mailcap</span></code> 中的设置。</p>
</dd></dl>
<p>一个用法示例:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">mailcap</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">mailcap</span><span class="o">.</span><span class="n">getcaps</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mailcap</span><span class="o">.</span><span class="n">findmatch</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="s1">&#39;video/mpeg&#39;</span><span class="p">,</span> <span class="n">filename</span><span class="o">=</span><span class="s1">&#39;tmp1223&#39;</span><span class="p">)</span>
<span class="go">(&#39;xmpeg tmp1223&#39;, {&#39;view&#39;: &#39;xmpeg %s&#39;})</span>
</pre></div>
</div>
</section>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h4>上一个主题</h4>
<p class="topless"><a href="json.html"
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">json</span></code> --- JSON 编码和解码器</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="mailbox.html"
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">mailbox</span></code> --- 操作多种格式的邮箱</a></p>
<div role="note" aria-label="source link">
<h3>本页</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">报告 Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/mailcap.rst"
rel="nofollow">显示源代码
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>导航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="总目录"
>索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模块索引"
>模块</a> |</li>
<li class="right" >
<a href="mailbox.html" title="mailbox --- 操作多种格式的邮箱"
>下一页</a> |</li>
<li class="right" >
<a href="json.html" title="json --- JSON 编码和解码器"
>上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="netdata.html" >互联网数据处理</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="footer">
&copy; <a href="../copyright.html">版权所有</a> 2001-2024, Python Software Foundation.
<br />
This page is licensed under the Python Software Foundation License Version 2.
<br />
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
<br />
<br />
The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br />
<br />
最后更新于 12月 09, 2024.
<a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
<br />
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
</div>
</body>
</html>