“Python/docs/3.9/library/poplib”的版本间差异

来自菜鸟教程
Python/docs/3.9/library/poplib
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
 +
{{DISPLAYTITLE:poplib — POP3 协议客户端 — Python 文档}}
 
<div id="module-poplib" class="section">
 
<div id="module-poplib" class="section">
  
 
<span id="poplib-pop3-protocol-client"></span>
 
<span id="poplib-pop3-protocol-client"></span>
= [[#module-poplib|<code>poplib</code>]] --- POP3 protocol client =
+
= poplib POP3 协议客户端 =
  
'''Source code:''' [https://github.com/python/cpython/tree/3.9/Lib/poplib.py Lib/poplib.py]
+
'''源代码:''' [[#id1|<span id="id2" class="problematic">:source:`Lib/poplib.py`</span>]]
  
This module defines a class, [[#poplib.POP3|<code>POP3</code>]], which encapsulates a connection to a
 
POP3 server and implements the protocol as defined in <span id="index-1" class="target"></span>[https://tools.ietf.org/html/rfc1939.html '''RFC 1939''']. The
 
[[#poplib.POP3|<code>POP3</code>]] class supports both the minimal and optional command sets from
 
<span id="index-2" class="target"></span>[https://tools.ietf.org/html/rfc1939.html '''RFC 1939''']. The [[#poplib.POP3|<code>POP3</code>]] class also supports the <code>STLS</code> command introduced
 
in <span id="index-3" class="target"></span>[https://tools.ietf.org/html/rfc2595.html '''RFC 2595'''] to enable encrypted communication on an already established connection.
 
  
Additionally, this module provides a class [[#poplib.POP3_SSL|<code>POP3_SSL</code>]], which provides
+
-----
support for connecting to POP3 servers that use SSL as an underlying protocol
 
layer.
 
  
Note that POP3, though widely supported, is obsolescent. The implementation
+
该模块定义了一个类,[[#poplib.POP3|POP3]],它封装了一个到 POP3 服务器的连接并实现了在 <span id="index-1" class="target"></span>[https://tools.ietf.org/html/rfc1939.html RFC 1939] 中定义的协议。 [[#poplib.POP3|POP3]] 类支持来自 <span id="index-2" class="target"></span>[https://tools.ietf.org/html/rfc1939.html RFC 1939] 的最小和可选命令集。 [[#poplib.POP3|POP3]] 类还支持 <span id="index-3" class="target"></span>[https://tools.ietf.org/html/rfc2595.html RFC 2595] 中引入的 <code>STLS</code> 命令,以在已建立的连接上启用加密通信。
quality of POP3 servers varies widely, and too many are quite poor. If your
 
mailserver supports IMAP, you would be better off using the
 
[[../imaplib#imaplib|<code>imaplib.IMAP4</code>]] class, as IMAP servers tend to be better implemented.
 
  
The [[#module-poplib|<code>poplib</code>]] module provides two classes:
+
此外,该模块提供了一个类 [[#poplib.POP3_SSL|POP3_SSL]],它支持连接到使用 SSL 作为底层协议层的 POP3 服务器。
 +
 
 +
请注意,POP3 虽然得到广泛支持,但已过时。 POP3服务器的实现质量参差不齐,太多就很差了。 如果您的邮件服务器支持 IMAP,您最好使用 [[../imaplib#imaplib|imaplib.IMAP4]] 类,因为 IMAP 服务器往往能更好地实现。
 +
 
 +
[[#module-poplib|poplib]] 模块提供了两个类:
  
 
<dl>
 
<dl>
<dt>''class'' <code>poplib.</code><code>POP3</code><span class="sig-paren">(</span>''host'', ''port=POP3_PORT''<span class="optional">[</span>, ''timeout''<span class="optional">]</span><span class="sig-paren">)</span></dt>
+
<dt>''<span class="pre">class</span>'' <span class="sig-prename descclassname"><span class="pre">poplib.</span></span><span class="sig-name descname"><span class="pre">POP3</span></span><span class="sig-paren">(</span>''<span class="pre">host</span>'', ''<span class="pre">port=POP3_PORT</span>''<span class="optional">[</span>, ''<span class="pre">timeout</span>''<span class="optional">]</span><span class="sig-paren">)</span></dt>
<dd><p>This class implements the actual POP3 protocol. The connection is created when
+
<dd><p>这个类实现了实际的 POP3 协议。 连接是在实例初始化时创建的。 如果省略 ''port'',则使用标准 POP3 端口 (110)。 可选的 ''timeout'' 参数指定连接尝试的超时时间(如果未指定,将使用全局默认超时设置)。</p>
the instance is initialized. If ''port'' is omitted, the standard POP3 port (110)
 
is used. The optional ''timeout'' parameter specifies a timeout in seconds for the
 
connection attempt (if not specified, the global default timeout setting will
 
be used).</p>
 
<p>Raises an [[../sys#auditing|<span class="std std-ref">auditing event</span>]] <code>poplib.connect</code> with arguments <code>self</code>, <code>host</code>, <code>port</code>.</p>
 
<p>All commands will raise an [[../sys#auditing|<span class="std std-ref">auditing event</span>]]
 
<code>poplib.putline</code> with arguments <code>self</code> and <code>line</code>,
 
where <code>line</code> is the bytes about to be sent to the remote host.</p>
 
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<p><span class="versionmodified changed">在 3.9 版更改: </span>If the ''timeout'' parameter is set to be zero, it will raise a
+
<p><span class="versionmodified changed"> 在 3.9 版更改: </span> 如果 ''timeout'' 参数设置为零,则会引发 [[../exceptions#ValueError|ValueError]] 以防止创建非阻塞套接字。</p>
[[../exceptions#ValueError|<code>ValueError</code>]] to prevent the creation of a non-blocking socket.</p>
 
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt>''class'' <code>poplib.</code><code>POP3_SSL</code><span class="sig-paren">(</span>''<span class="n">host</span>'', ''<span class="n">port</span><span class="o">=</span><span class="default_value">POP3_SSL_PORT</span>'', ''<span class="n">keyfile</span><span class="o">=</span><span class="default_value">None</span>'', ''<span class="n">certfile</span><span class="o">=</span><span class="default_value">None</span>'', ''<span class="n">timeout</span><span class="o">=</span><span class="default_value">None</span>'', ''<span class="n">context</span><span class="o">=</span><span class="default_value">None</span>''<span class="sig-paren">)</span></dt>
+
<dt>''<span class="pre">class</span>'' <span class="sig-prename descclassname"><span class="pre">poplib.</span></span><span class="sig-name descname"><span class="pre">POP3_SSL</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">host</span></span>'', ''<span class="n"><span class="pre">port</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">POP3_SSL_PORT</span></span>'', ''<span class="n"><span class="pre">keyfile</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>'', ''<span class="n"><span class="pre">certfile</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>'', ''<span class="n"><span class="pre">timeout</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>'', ''<span class="n"><span class="pre">context</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>This is a subclass of [[#poplib.POP3|<code>POP3</code>]] that connects to the server over an SSL
+
<dd><p>这是 [[#poplib.POP3|POP3]] 的子类,它通过 SSL 加密套接字连接到服务器。 如果未指定 ''port'',则为 995,则使用标准的 POP3-over-SSL 端口。 ''timeout'' [[#poplib.POP3|POP3]] 构造函数中工作。 ''context'' 是一个可选的 [[../ssl#ssl|ssl.SSLContext]] 对象,它允许将 SSL 配置选项、证书和私钥捆绑到一个(可能是长期存在的)结构中。 请阅读 [[../ssl#ssl-security|安全注意事项]] 以获得最佳实践。</p>
encrypted socket. If ''port'' is not specified, 995, the standard POP3-over-SSL
+
<p>''keyfile'' ''certfile'' ''context'' 的传统替代方案 - 它们可以分别指向 PEM 格式的私钥和证书链文件,用于 SSL 连接。</p>
port is used. ''timeout'' works as in the [[#poplib.POP3|<code>POP3</code>]] constructor.
 
''context'' is an optional [[../ssl#ssl|<code>ssl.SSLContext</code>]] object which allows
 
bundling SSL configuration options, certificates and private keys into a
 
single (potentially long-lived) structure. Please read [[../ssl#ssl-security|<span class="std std-ref">Security considerations</span>]]
 
for best practices.</p>
 
<p>''keyfile'' and ''certfile'' are a legacy alternative to ''context'' - they can
 
point to PEM-formatted private key and certificate chain files,
 
respectively, for the SSL connection.</p>
 
<p>Raises an [[../sys#auditing|<span class="std std-ref">auditing event</span>]] <code>poplib.connect</code> with arguments <code>self</code>, <code>host</code>, <code>port</code>.</p>
 
<p>All commands will raise an [[../sys#auditing|<span class="std std-ref">auditing event</span>]]
 
<code>poplib.putline</code> with arguments <code>self</code> and <code>line</code>,
 
where <code>line</code> is the bytes about to be sent to the remote host.</p>
 
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<p><span class="versionmodified changed">在 3.2 版更改: </span>''context'' parameter added.</p>
+
<p><span class="versionmodified changed">在 3.2 版更改:添加了 </span>''context'' 参数。</p>
  
 
</div>
 
</div>
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<p><span class="versionmodified changed">3.4 版更改: </span>The class now supports hostname check with
+
<p><span class="versionmodified changed"> 3.4 版更改: </span> 该类现在支持使用 [[../ssl#ssl.SSLContext|ssl.SSLContext.check_hostname]] ''服务器名称指示'' 进行主机名检查(请参阅 [[../ssl#ssl|ssl.HAS_SNI]] )。</p>
[[../ssl#ssl.SSLContext|<code>ssl.SSLContext.check_hostname</code>]] and ''Server Name Indication'' (see
 
[[../ssl#ssl|<code>ssl.HAS_SNI</code>]]).</p>
 
  
 
</div>
 
</div>
 
<div class="deprecated">
 
<div class="deprecated">
  
<p><span class="versionmodified deprecated">3.6 版后已移除: </span>''keyfile'' and ''certfile'' are deprecated in favor of ''context''.
+
<p><span class="versionmodified deprecated"> 3.6 版起已弃用:</span>''keyfile'' ''certfile'' 已弃用,取而代之的是 ''context''。 请改用 [[../ssl#ssl.SSLContext|ssl.SSLContext.load_cert_chain()]],或让 [[../ssl#ssl|ssl.create_default_context()]] 为您选择系统的可信 CA 证书。</p>
Please use [[../ssl#ssl.SSLContext|<code>ssl.SSLContext.load_cert_chain()</code>]] instead, or let
 
[[../ssl#ssl|<code>ssl.create_default_context()</code>]] select the system's trusted CA
 
certificates for you.</p>
 
  
 
</div>
 
</div>
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<p><span class="versionmodified changed">在 3.9 版更改: </span>If the ''timeout'' parameter is set to be zero, it will raise a
+
<p><span class="versionmodified changed"> 在 3.9 版更改: </span> 如果 ''timeout'' 参数设置为零,则会引发 [[../exceptions#ValueError|ValueError]] 以防止创建非阻塞套接字。</p>
[[../exceptions#ValueError|<code>ValueError</code>]] to prevent the creation of a non-blocking socket.</p>
 
  
 
</div></dd></dl>
 
</div></dd></dl>
  
One exception is defined as an attribute of the [[#module-poplib|<code>poplib</code>]] module:
+
一个例外被定义为 [[#module-poplib|poplib]] 模块的一个属性:
  
; ''exception'' <code>poplib.</code><code>error_proto</code>
+
; ''<span class="pre">exception</span>'' <span class="sig-prename descclassname"><span class="pre">poplib.</span></span><span class="sig-name descname"><span class="pre">error_proto</span></span>
: Exception raised on any errors from this module (errors from [[../socket#module-socket|<code>socket</code>]] module are not caught). The reason for the exception is passed to the constructor as a string.
+
: 来自该模块的任何错误引发异常(未捕获来自 [[../socket#module-socket|socket]] 模块的错误)。 异常的原因作为字符串传递给构造函数。
  
 
<div class="admonition seealso">
 
<div class="admonition seealso">
  
参见
+
也可以看看
  
; Module [[../imaplib#module-imaplib|<code>imaplib</code>]]
+
; 模块 [[../imaplib#module-imaplib|imaplib]]
: The standard Python IMAP module.
+
: 标准 Python IMAP 模块。
; [http://www.catb.org/~esr/fetchmail/fetchmail-FAQ.html Frequently Asked Questions About Fetchmail]
+
; [http://www.catb.org/~esr/fetchmail/fetchmail-FAQ.html 关于 Fetchmail 的常见问题]
: The FAQ for the '''fetchmail''' POP/IMAP client collects information on POP3 server variations and RFC noncompliance that may be useful if you need to write an application based on the POP protocol.
+
: '''fetchmail''' POP/IMAP 客户端的 FAQ 收集了有关 POP3 服务器变体和 RFC 不合规性的信息,如果您需要编写基于 POP 协议的应用程序,这些信息可能会很有用。
  
  
第102行: 第70行:
 
<div id="pop3-objects" class="section">
 
<div id="pop3-objects" class="section">
  
<span id="id1"></span>
+
<span id="id3"></span>
== POP3 Objects ==
+
== POP3 对象 ==
  
All POP3 commands are represented by methods of the same name, in lower-case;
+
所有 POP3 命令都由同名的方法表示,以小写形式表示; 大多数返回服务器发送的响应文本。
most return the response text sent by the server.
 
  
An [[#poplib.POP3|<code>POP3</code>]] instance has the following methods:
+
[[#poplib.POP3|POP3]] 实例具有以下方法:
  
; <code>POP3.</code><code>set_debuglevel</code><span class="sig-paren">(</span>''<span class="n">level</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">POP3.</span></span><span class="sig-name descname"><span class="pre">set_debuglevel</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">level</span></span>''<span class="sig-paren">)</span>
: Set the instance's debugging level. This controls the amount of debugging output printed. The default, <code>0</code>, produces no debugging output. A value of <code>1</code> produces a moderate amount of debugging output, generally a single line per request. A value of <code>2</code> or higher produces the maximum amount of debugging output, logging each line sent and received on the control connection.
+
: 设置实例的调试级别。 这控制打印的调试输出量。 默认值 <code>0</code> 不产生调试输出。 <code>1</code> 的值会产生适量的调试输出,通常每个请求只有一行。 <code>2</code> 或更高的值会产生最大数量的调试输出,记录控制连接上发送和接收的每一行。
  
; <code>POP3.</code><code>getwelcome</code><span class="sig-paren">(</span><span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">POP3.</span></span><span class="sig-name descname"><span class="pre">getwelcome</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span>
: Returns the greeting string sent by the POP3 server.
+
: 返回 POP3 服务器发送的问候语字符串。
  
 
<dl>
 
<dl>
<dt><code>POP3.</code><code>capa</code><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">POP3.</span></span><span class="sig-name descname"><span class="pre">capa</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
<dd><p>Query the server's capabilities as specified in <span id="index-4" class="target"></span>[https://tools.ietf.org/html/rfc2449.html '''RFC 2449'''].
+
<dd><p>查询 <span id="index-4" class="target"></span>[https://tools.ietf.org/html/rfc2449.html RFC 2449] 中指定的服务器功能。 以 <code>{'name': ['param'...]}</code> 形式返回字典。</p>
Returns a dictionary in the form <code>{'name': ['param'...]}</code>.</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.4 新版功能.</span></p>
+
<p><span class="versionmodified added">3.4 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
; <code>POP3.</code><code>user</code><span class="sig-paren">(</span>''<span class="n">username</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">POP3.</span></span><span class="sig-name descname"><span class="pre">user</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">username</span></span>''<span class="sig-paren">)</span>
: Send user command, response should indicate that a password is required.
+
: 发送用户命令,响应应指示需要密码。
  
; <code>POP3.</code><code>pass_</code><span class="sig-paren">(</span>''<span class="n">password</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">POP3.</span></span><span class="sig-name descname"><span class="pre">pass_</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">password</span></span>''<span class="sig-paren">)</span>
: Send password, response includes message count and mailbox size. Note: the mailbox on the server is locked until <code>quit()</code> is called.
+
: 发送密码,响应包括消息计数和邮箱大小。 注意:服务器上的邮箱被锁定,直到调用<code>quit()</code>
  
; <code>POP3.</code><code>apop</code><span class="sig-paren">(</span>''<span class="n">user</span>'', ''<span class="n">secret</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">POP3.</span></span><span class="sig-name descname"><span class="pre">apop</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">user</span></span>'', ''<span class="n"><span class="pre">secret</span></span>''<span class="sig-paren">)</span>
: Use the more secure APOP authentication to log into the POP3 server.
+
: 使用更安全的 APOP 认证登录 POP3 服务器。
  
; <code>POP3.</code><code>rpop</code><span class="sig-paren">(</span>''<span class="n">user</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">POP3.</span></span><span class="sig-name descname"><span class="pre">rpop</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">user</span></span>''<span class="sig-paren">)</span>
: Use RPOP authentication (similar to UNIX r-commands) to log into POP3 server.
+
: 使用 RPOP 身份验证(类似于 UNIX r-commands)登录到 POP3 服务器。
  
; <code>POP3.</code><code>stat</code><span class="sig-paren">(</span><span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">POP3.</span></span><span class="sig-name descname"><span class="pre">stat</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span>
: Get mailbox status. The result is a tuple of 2 integers: <code>(message count, mailbox size)</code>.
+
: 获取邮箱状态。 结果是 2 个整数的元组:<code>(message count, mailbox size)</code>
  
; <code>POP3.</code><code>list</code><span class="sig-paren">(</span><span class="optional">[</span>''which''<span class="optional">]</span><span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">POP3.</span></span><span class="sig-name descname"><span class="pre">list</span></span><span class="sig-paren">(</span><span class="optional">[</span>''<span class="pre">which</span>''<span class="optional">]</span><span class="sig-paren">)</span>
: Request message list, result is in the form <code>(response, ['mesg_num octets', ...], octets)</code>. If ''which'' is set, it is the message to list.
+
: 请求消息列表,结果格式为<code>(response, ['mesg_num octets', ...], octets)</code>。 如果设置了''which'',就是要列出的消息。
  
; <code>POP3.</code><code>retr</code><span class="sig-paren">(</span>''<span class="n">which</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">POP3.</span></span><span class="sig-name descname"><span class="pre">retr</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">which</span></span>''<span class="sig-paren">)</span>
: Retrieve whole message number ''which'', and set its seen flag. Result is in form <code>(response, ['line', ...], octets)</code>.
+
: 检索整个消息号 ''which'',并设置其已见标志。 结果采用 <code>(response, ['line', ...], octets)</code> 形式。
  
; <code>POP3.</code><code>dele</code><span class="sig-paren">(</span>''<span class="n">which</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">POP3.</span></span><span class="sig-name descname"><span class="pre">dele</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">which</span></span>''<span class="sig-paren">)</span>
: Flag message number ''which'' for deletion. On most servers deletions are not actually performed until QUIT (the major exception is Eudora QPOP, which deliberately violates the RFCs by doing pending deletes on any disconnect).
+
: 标记消息号 ''which'' 用于删除。 在大多数服务器上,直到 QUIT 才会真正执行删除(主要的例外是 Eudora QPOP,它通过在任何断开连接时执行挂起删除来故意违反 RFC)。
  
; <code>POP3.</code><code>rset</code><span class="sig-paren">(</span><span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">POP3.</span></span><span class="sig-name descname"><span class="pre">rset</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span>
: Remove any deletion marks for the mailbox.
+
: 删除邮箱的所有删除标记。
  
; <code>POP3.</code><code>noop</code><span class="sig-paren">(</span><span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">POP3.</span></span><span class="sig-name descname"><span class="pre">noop</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span>
: Do nothing. Might be used as a keep-alive.
+
: 没做什么。 可以用作保活。
  
; <code>POP3.</code><code>quit</code><span class="sig-paren">(</span><span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">POP3.</span></span><span class="sig-name descname"><span class="pre">quit</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span>
: Signoff: commit changes, unlock mailbox, drop connection.
+
: 签收:提交更改、解锁邮箱、断开连接。
  
 
<dl>
 
<dl>
<dt><code>POP3.</code><code>top</code><span class="sig-paren">(</span>''<span class="n">which</span>'', ''<span class="n">howmuch</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">POP3.</span></span><span class="sig-name descname"><span class="pre">top</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">which</span></span>'', ''<span class="n"><span class="pre">howmuch</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Retrieves the message header plus ''howmuch'' lines of the message after the
+
<dd><p>在消息号 ''which'' 的头之后检索消息头加上消息的 ''howmuch'' 行。 结果采用 <code>(response, ['line', ...], octets)</code> 形式。</p>
header of message number ''which''. Result is in form <code>(response, ['line', ...], octets)</code>.</p>
+
<p>此方法使用的 POP3 TOP 命令与 RETR 命令不同,它不设置邮件的已查看标志; 不幸的是,TOP 在 RFC 中没有详细说明,并且经常在非品牌服务器中被破坏。 在信任此方法之前,请针对您将使用的 POP3 服务器手动测试此方法。</p></dd></dl>
<p>The POP3 TOP command this method uses, unlike the RETR command, doesn't set the
 
message's seen flag; unfortunately, TOP is poorly specified in the RFCs and is
 
frequently broken in off-brand servers. Test this method by hand against the
 
POP3 servers you will use before trusting it.</p></dd></dl>
 
  
; <code>POP3.</code><code>uidl</code><span class="sig-paren">(</span>''<span class="n">which</span><span class="o">=</span><span class="default_value">None</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">POP3.</span></span><span class="sig-name descname"><span class="pre">uidl</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">which</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>''<span class="sig-paren">)</span>
: Return message digest (unique id) list. If ''which'' is specified, result contains the unique id for that message in the form <code>'response mesgnum uid</code>, otherwise result is list <code>(response, ['mesgnum uid', ...], octets)</code>.
+
: 返回消息摘要(唯一 ID)列表。 如果指定了 ''which'',则结果以 <code>'response mesgnum uid</code> 的形式包含该消息的唯一 ID,否则结果是列表 <code>(response, ['mesgnum uid', ...], octets)</code>
  
 
<dl>
 
<dl>
<dt><code>POP3.</code><code>utf8</code><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">POP3.</span></span><span class="sig-name descname"><span class="pre">utf8</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
<dd><p>Try to switch to UTF-8 mode. Returns the server response if successful,
+
<dd><p>尝试切换到 UTF-8 模式。 如果成功则返回服务器响应,否则引发 [[#poplib.error_proto|error_proto]]。 在 <span id="index-5" class="target"></span>[https://tools.ietf.org/html/rfc6856.html RFC 6856] 中指定。</p>
raises [[#poplib.error_proto|<code>error_proto</code>]] if not. Specified in <span id="index-5" class="target"></span>[https://tools.ietf.org/html/rfc6856.html '''RFC 6856'''].</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.5 新版功能.</span></p>
+
<p><span class="versionmodified added">3.5 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt><code>POP3.</code><code>stls</code><span class="sig-paren">(</span>''<span class="n">context</span><span class="o">=</span><span class="default_value">None</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">POP3.</span></span><span class="sig-name descname"><span class="pre">stls</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">context</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Start a TLS session on the active connection as specified in <span id="index-6" class="target"></span>[https://tools.ietf.org/html/rfc2595.html '''RFC 2595'''].
+
<dd><p>按照 <span id="index-6" class="target"></span>[https://tools.ietf.org/html/rfc2595.html RFC 2595] 中的规定在活动连接上启动 TLS 会话。 这仅在用户身份验证之前允许</p>
This is only allowed before user authentication</p>
+
<p>''context'' 参数是一个 [[../ssl#ssl|ssl.SSLContext]] 对象,它允许将 SSL 配置选项、证书和私钥捆绑到一个(可能是长期存在的)结构中。 请阅读 [[../ssl#ssl-security|安全注意事项]] 以获得最佳实践。</p>
<p>''context'' parameter is a [[../ssl#ssl|<code>ssl.SSLContext</code>]] object which allows
+
<p>此方法支持通过 [[../ssl#ssl.SSLContext|ssl.SSLContext.check_hostname]] ''Server Name Indication'' 进行主机名检查(请参阅 [[../ssl#ssl|ssl.HAS_SNI]])。</p>
bundling SSL configuration options, certificates and private keys into
 
a single (potentially long-lived) structure. Please read [[../ssl#ssl-security|<span class="std std-ref">Security considerations</span>]]
 
for best practices.</p>
 
<p>This method supports hostname checking via
 
[[../ssl#ssl.SSLContext|<code>ssl.SSLContext.check_hostname</code>]] and ''Server Name Indication'' (see
 
[[../ssl#ssl|<code>ssl.HAS_SNI</code>]]).</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.4 新版功能.</span></p>
+
<p><span class="versionmodified added">3.4 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
Instances of [[#poplib.POP3_SSL|<code>POP3_SSL</code>]] have no additional methods. The interface of this
+
[[#poplib.POP3_SSL|POP3_SSL]] 的实例没有其他方法。 此子类的接口与其父类相同。
subclass is identical to its parent.
 
  
  
第205行: 第159行:
 
<div id="pop3-example" class="section">
 
<div id="pop3-example" class="section">
  
<span id="id2"></span>
+
<span id="id4"></span>
== POP3 Example ==
+
== POP3 示例 ==
  
Here is a minimal example (without error checking) that opens a mailbox and
+
这是一个最小的例子(没有错误检查),它打开一个邮箱并检索和打印所有消息:
retrieves and prints all messages:
 
  
 
<div class="highlight-python3 notranslate">
 
<div class="highlight-python3 notranslate">
第215行: 第168行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>import getpass, poplib
+
<syntaxhighlight lang="python3">import getpass, poplib
  
 
M = poplib.POP3('localhost')
 
M = poplib.POP3('localhost')
第223行: 第176行:
 
for i in range(numMessages):
 
for i in range(numMessages):
 
     for j in M.retr(i+1)[1]:
 
     for j in M.retr(i+1)[1]:
         print(j)</pre>
+
         print(j)</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
At the end of the module, there is a test section that contains a more extensive
+
在模块的末尾,有一个测试部分,其中包含更广泛的使用示例。
example of usage.
+
 
  
 +
</div>
  
 
</div>
 
</div>
 +
<div class="clearer">
 +
 +
  
 
</div>
 
</div>
  
[[Category:Python 3.9 中文文档]]
+
[[Category:Python 3.9 文档]]

2021年10月31日 (日) 04:53的最新版本

poplib — POP3 协议客户端

源代码: :source:`Lib/poplib.py`



该模块定义了一个类,POP3,它封装了一个到 POP3 服务器的连接并实现了在 RFC 1939 中定义的协议。 POP3 类支持来自 RFC 1939 的最小和可选命令集。 POP3 类还支持 RFC 2595 中引入的 STLS 命令,以在已建立的连接上启用加密通信。

此外,该模块提供了一个类 POP3_SSL,它支持连接到使用 SSL 作为底层协议层的 POP3 服务器。

请注意,POP3 虽然得到广泛支持,但已过时。 POP3服务器的实现质量参差不齐,太多就很差了。 如果您的邮件服务器支持 IMAP,您最好使用 imaplib.IMAP4 类,因为 IMAP 服务器往往能更好地实现。

poplib 模块提供了两个类:

class poplib.POP3(host, port=POP3_PORT[, timeout])

这个类实现了实际的 POP3 协议。 连接是在实例初始化时创建的。 如果省略 port,则使用标准 POP3 端口 (110)。 可选的 timeout 参数指定连接尝试的超时时间(如果未指定,将使用全局默认超时设置)。

在 3.9 版更改: 如果 timeout 参数设置为零,则会引发 ValueError 以防止创建非阻塞套接字。

class poplib.POP3_SSL(host, port=POP3_SSL_PORT, keyfile=None, certfile=None, timeout=None, context=None)

这是 POP3 的子类,它通过 SSL 加密套接字连接到服务器。 如果未指定 port,则为 995,则使用标准的 POP3-over-SSL 端口。 timeoutPOP3 构造函数中工作。 context 是一个可选的 ssl.SSLContext 对象,它允许将 SSL 配置选项、证书和私钥捆绑到一个(可能是长期存在的)结构中。 请阅读 安全注意事项 以获得最佳实践。

keyfilecertfilecontext 的传统替代方案 - 它们可以分别指向 PEM 格式的私钥和证书链文件,用于 SSL 连接。

在 3.2 版更改:添加了 context 参数。

3.4 版更改: 该类现在支持使用 ssl.SSLContext.check_hostname服务器名称指示 进行主机名检查(请参阅 ssl.HAS_SNI )。

自 3.6 版起已弃用:keyfilecertfile 已弃用,取而代之的是 context。 请改用 ssl.SSLContext.load_cert_chain(),或让 ssl.create_default_context() 为您选择系统的可信 CA 证书。

在 3.9 版更改: 如果 timeout 参数设置为零,则会引发 ValueError 以防止创建非阻塞套接字。

一个例外被定义为 poplib 模块的一个属性:

exception poplib.error_proto
来自该模块的任何错误引发异常(未捕获来自 socket 模块的错误)。 异常的原因作为字符串传递给构造函数。

也可以看看

模块 imaplib
标准 Python IMAP 模块。
关于 Fetchmail 的常见问题
fetchmail POP/IMAP 客户端的 FAQ 收集了有关 POP3 服务器变体和 RFC 不合规性的信息,如果您需要编写基于 POP 协议的应用程序,这些信息可能会很有用。


POP3 对象

所有 POP3 命令都由同名的方法表示,以小写形式表示; 大多数返回服务器发送的响应文本。

POP3 实例具有以下方法:

POP3.set_debuglevel(level)
设置实例的调试级别。 这控制打印的调试输出量。 默认值 0 不产生调试输出。 1 的值会产生适量的调试输出,通常每个请求只有一行。 2 或更高的值会产生最大数量的调试输出,记录控制连接上发送和接收的每一行。
POP3.getwelcome()
返回 POP3 服务器发送的问候语字符串。
POP3.capa()

查询 RFC 2449 中指定的服务器功能。 以 {'name': ['param'...]} 形式返回字典。

3.4 版中的新功能。

POP3.user(username)
发送用户命令,响应应指示需要密码。
POP3.pass_(password)
发送密码,响应包括消息计数和邮箱大小。 注意:服务器上的邮箱被锁定,直到调用quit()
POP3.apop(user, secret)
使用更安全的 APOP 认证登录 POP3 服务器。
POP3.rpop(user)
使用 RPOP 身份验证(类似于 UNIX r-commands)登录到 POP3 服务器。
POP3.stat()
获取邮箱状态。 结果是 2 个整数的元组:(message count, mailbox size)
POP3.list([which])
请求消息列表,结果格式为(response, ['mesg_num octets', ...], octets)。 如果设置了which,就是要列出的消息。
POP3.retr(which)
检索整个消息号 which,并设置其已见标志。 结果采用 (response, ['line', ...], octets) 形式。
POP3.dele(which)
标记消息号 which 用于删除。 在大多数服务器上,直到 QUIT 才会真正执行删除(主要的例外是 Eudora QPOP,它通过在任何断开连接时执行挂起删除来故意违反 RFC)。
POP3.rset()
删除邮箱的所有删除标记。
POP3.noop()
没做什么。 可以用作保活。
POP3.quit()
签收:提交更改、解锁邮箱、断开连接。
POP3.top(which, howmuch)

在消息号 which 的头之后检索消息头加上消息的 howmuch 行。 结果采用 (response, ['line', ...], octets) 形式。

此方法使用的 POP3 TOP 命令与 RETR 命令不同,它不设置邮件的已查看标志; 不幸的是,TOP 在 RFC 中没有详细说明,并且经常在非品牌服务器中被破坏。 在信任此方法之前,请针对您将使用的 POP3 服务器手动测试此方法。

POP3.uidl(which=None)
返回消息摘要(唯一 ID)列表。 如果指定了 which,则结果以 'response mesgnum uid 的形式包含该消息的唯一 ID,否则结果是列表 (response, ['mesgnum uid', ...], octets)
POP3.utf8()

尝试切换到 UTF-8 模式。 如果成功则返回服务器响应,否则引发 error_proto。 在 RFC 6856 中指定。

3.5 版中的新功能。

POP3.stls(context=None)

按照 RFC 2595 中的规定在活动连接上启动 TLS 会话。 这仅在用户身份验证之前允许

context 参数是一个 ssl.SSLContext 对象,它允许将 SSL 配置选项、证书和私钥捆绑到一个(可能是长期存在的)结构中。 请阅读 安全注意事项 以获得最佳实践。

此方法支持通过 ssl.SSLContext.check_hostnameServer Name Indication 进行主机名检查(请参阅 ssl.HAS_SNI)。

3.4 版中的新功能。

POP3_SSL 的实例没有其他方法。 此子类的接口与其父类相同。


POP3 示例

这是一个最小的例子(没有错误检查),它打开一个邮箱并检索和打印所有消息:

import getpass, poplib

M = poplib.POP3('localhost')
M.user(getpass.getuser())
M.pass_(getpass.getpass())
numMessages = len(M.list()[1])
for i in range(numMessages):
    for j in M.retr(i+1)[1]:
        print(j)

在模块的末尾,有一个测试部分,其中包含更广泛的使用示例。