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

来自菜鸟教程
Python/docs/3.9/library/nntplib
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
 +
{{DISPLAYTITLE:nntplib — NNTP 协议客户端 — Python 文档}}
 
<div id="module-nntplib" class="section">
 
<div id="module-nntplib" class="section">
  
 
<span id="nntplib-nntp-protocol-client"></span>
 
<span id="nntplib-nntp-protocol-client"></span>
= [[#module-nntplib|<code>nntplib</code>]] --- NNTP protocol client =
+
= nntplib NNTP 协议客户端 =
  
'''Source code:''' [https://github.com/python/cpython/tree/3.9/Lib/nntplib.py Lib/nntplib.py]
+
'''源代码:''' [[#id1|<span id="id2" class="problematic">:source:`Lib/nntplib.py`</span>]]
  
This module defines the class [[#nntplib.NNTP|<code>NNTP</code>]] which implements the client side of
 
the Network News Transfer Protocol. It can be used to implement a news reader
 
or poster, or automated news processors. It is compatible with <span id="index-1" class="target"></span>[https://tools.ietf.org/html/rfc3977.html '''RFC 3977''']
 
as well as the older <span id="index-2" class="target"></span>[https://tools.ietf.org/html/rfc977.html '''RFC 977'''] and <span id="index-3" class="target"></span>[https://tools.ietf.org/html/rfc2980.html '''RFC 2980'''].
 
  
Here are two small examples of how it can be used. To list some statistics
+
-----
about a newsgroup and print the subjects of the last 10 articles:
+
 
 +
该模块定义了类 [[#nntplib.NNTP|NNTP]],它实现了网络新闻传输协议的客户端。 它可用于实现新闻阅读器或海报,或自动化新闻处理器。 它兼容 <span id="index-1" class="target"></span>[https://tools.ietf.org/html/rfc3977.html RFC 3977] 以及旧的 <span id="index-2" class="target"></span>[https://tools.ietf.org/html/rfc977.html RFC 977] 和 <span id="index-3" class="target"></span>RFC 2980[ X121X]。
 +
 
 +
下面是两个如何使用它的小例子。 要列出有关新闻组的一些统计信息并打印最近 10 篇文章的主题:
  
 
<div class="highlight-python3 notranslate">
 
<div class="highlight-python3 notranslate">
第18行: 第18行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>&gt;&gt;&gt; s = nntplib.NNTP('news.gmane.io')
+
<syntaxhighlight lang="python3">>>> s = nntplib.NNTP('news.gmane.io')
&gt;&gt;&gt; resp, count, first, last, name = s.group('gmane.comp.python.committers')
+
>>> resp, count, first, last, name = s.group('gmane.comp.python.committers')
&gt;&gt;&gt; print('Group', name, 'has', count, 'articles, range', first, 'to', last)
+
>>> print('Group', name, 'has', count, 'articles, range', first, 'to', last)
 
Group gmane.comp.python.committers has 1096 articles, range 1 to 1096
 
Group gmane.comp.python.committers has 1096 articles, range 1 to 1096
&gt;&gt;&gt; resp, overviews = s.over((last - 9, last))
+
>>> resp, overviews = s.over((last - 9, last))
&gt;&gt;&gt; for id, over in overviews:
+
>>> for id, over in overviews:
 
...    print(id, nntplib.decode_header(over['subject']))
 
...    print(id, nntplib.decode_header(over['subject']))
 
...
 
...
第36行: 第36行:
 
1095 Hello fellow committers!
 
1095 Hello fellow committers!
 
1096 Re: Hello fellow committers!
 
1096 Re: Hello fellow committers!
&gt;&gt;&gt; s.quit()
+
>>> s.quit()
'205 Bye!'</pre>
+
'205 Bye!'</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
To post an article from a binary file (this assumes that the article has valid
+
从二进制文件发布文章(假设文章具有有效标题,并且您有权在特定新闻组上发布):
headers, and that you have right to post on the particular newsgroup):
 
  
 
<div class="highlight-python3 notranslate">
 
<div class="highlight-python3 notranslate">
第49行: 第48行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>&gt;&gt;&gt; s = nntplib.NNTP('news.gmane.io')
+
<syntaxhighlight lang="python3">>>> s = nntplib.NNTP('news.gmane.io')
&gt;&gt;&gt; f = open('article.txt', 'rb')
+
>>> f = open('article.txt', 'rb')
&gt;&gt;&gt; s.post(f)
+
>>> s.post(f)
 
'240 Article posted successfully.'
 
'240 Article posted successfully.'
&gt;&gt;&gt; s.quit()
+
>>> s.quit()
'205 Bye!'</pre>
+
'205 Bye!'</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
The module itself defines the following classes:
+
模块本身定义了以下类:
  
 
<dl>
 
<dl>
<dt>''class'' <code>nntplib.</code><code>NNTP</code><span class="sig-paren">(</span>''host'', ''port=119'', ''user=None'', ''password=None'', ''readermode=None'', ''usenetrc=False''<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">nntplib.</span></span><span class="sig-name descname"><span class="pre">NNTP</span></span><span class="sig-paren">(</span>''<span class="pre">host</span>'', ''<span class="pre">port=119</span>'', ''<span class="pre">user=None</span>'', ''<span class="pre">password=None</span>'', ''<span class="pre">readermode=None</span>'', ''<span class="pre">usenetrc=False</span>''<span class="optional">[</span>, ''<span class="pre">timeout</span>''<span class="optional">]</span><span class="sig-paren">)</span></dt>
<dd><p>Return a new [[#nntplib.NNTP|<code>NNTP</code>]] object, representing a connection
+
<dd><p>返回一个新的 [[#nntplib.NNTP|NNTP]] 对象,表示与运行在主机 ''主机'' 上的 NNTP 服务器的连接,监听端口 ''端口'' 。 可以为套接字连接指定可选的 ''timeout''。 如果提供了可选的 ''user'' ''password'',或者 <code>/.netrc</code> 中存在合适的凭据并且可选标志 ''usenetrc'' 为真,则 [ X171X] 和 <code>AUTHINFO PASS</code> 命令用于向服务器识别和验证用户。 如果可选标志 ''readermode'' 为真,则在执行身份验证之前发送 <code>mode reader</code> 命令。 如果您连接到本地机器上的 NNTP 服务器并打算调用特定于阅读器的命令,例如 <code>group</code>,有时需要阅读器模式。 如果您收到意外的 [[#nntplib.NNTPPermanentError|NNTPPermanentError]]s,您可能需要设置 ''readermode''[[#nntplib.NNTP|NNTP]] 类支持 [X39X]with 语句无条件消耗 [[../exceptions#OSError|OSError]] 异常并在完成后关闭 NNTP 连接,例如:</p>
to the NNTP server running on host ''host'', listening at port ''port''.
 
An optional ''timeout'' can be specified for the socket connection.
 
If the optional ''user'' and ''password'' are provided, or if suitable
 
credentials are present in <code>/.netrc</code> and the optional flag ''usenetrc''
 
is true, the <code>AUTHINFO USER</code> and <code>AUTHINFO PASS</code> commands are used
 
to identify and authenticate the user to the server. If the optional
 
flag ''readermode'' is true, then a <code>mode reader</code> command is sent before
 
authentication is performed. Reader mode is sometimes necessary if you are
 
connecting to an NNTP server on the local machine and intend to call
 
reader-specific commands, such as <code>group</code>. If you get unexpected
 
[[#nntplib.NNTPPermanentError|<code>NNTPPermanentError</code>]]s, you might need to set ''readermode''.
 
The [[#nntplib.NNTP|<code>NNTP</code>]] class supports the [[../../reference/compound_stmts#with|<code>with</code>]] statement to
 
unconditionally consume [[../exceptions#OSError|<code>OSError</code>]] exceptions and to close the NNTP
 
connection when done, e.g.:</p>
 
 
<div class="doctest highlight-default notranslate">
 
<div class="doctest highlight-default notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>&gt;&gt;&gt; from nntplib import NNTP
+
<syntaxhighlight lang="python">>>> from nntplib import NNTP
&gt;&gt;&gt; with NNTP('news.gmane.io') as n:
+
>>> with NNTP('news.gmane.io') as n:
 
...    n.group('gmane.comp.python.committers')
 
...    n.group('gmane.comp.python.committers')
 
...  
 
...  
 
('211 1755 1 1755 gmane.comp.python.committers', 1755, 1, 1755, 'gmane.comp.python.committers')
 
('211 1755 1 1755 gmane.comp.python.committers', 1755, 1, 1755, 'gmane.comp.python.committers')
&gt;&gt;&gt;</pre>
+
>>></syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
<p>Raises an [[../sys#auditing|<span class="std std-ref">auditing event</span>]] <code>nntplib.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>nntplib.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>''usenetrc'' is now <code>False</code> by default.</p>
+
<p><span class="versionmodified changed">在 3.2 版更改:</span>''usenetrc'' 现在默认为 <code>False</code></p>
  
 
</div>
 
</div>
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<p><span class="versionmodified changed">在 3.3 版更改: </span>Support for the [[../../reference/compound_stmts#with|<code>with</code>]] statement was added.</p>
+
<p><span class="versionmodified changed">在 3.3 版更改:</span>添加了对 [[../../reference/compound_stmts#with|with]] 语句的支持。</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>
  
 
<dl>
 
<dl>
<dt>''class'' <code>nntplib.</code><code>NNTP_SSL</code><span class="sig-paren">(</span>''host'', ''port=563'', ''user=None'', ''password=None'', ''ssl_context=None'', ''readermode=None'', ''usenetrc=False''<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">nntplib.</span></span><span class="sig-name descname"><span class="pre">NNTP_SSL</span></span><span class="sig-paren">(</span>''<span class="pre">host</span>'', ''<span class="pre">port=563</span>'', ''<span class="pre">user=None</span>'', ''<span class="pre">password=None</span>'', ''<span class="pre">ssl_context=None</span>'', ''<span class="pre">readermode=None</span>'', ''<span class="pre">usenetrc=False</span>''<span class="optional">[</span>, ''<span class="pre">timeout</span>''<span class="optional">]</span><span class="sig-paren">)</span></dt>
<dd><p>Return a new [[#nntplib.NNTP_SSL|<code>NNTP_SSL</code>]] object, representing an encrypted
+
<dd><p>返回一个新的 [[#nntplib.NNTP_SSL|NNTP_SSL]] 对象,表示与运行在主机 ''主机'' 上的 NNTP 服务器的加密连接,监听端口 ''端口'' [[#nntplib.NNTP_SSL|NNTP_SSL]] 对象与 [[#nntplib.NNTP|NNTP]] 对象具有相同的方法。 如果省略 ''port'',则使用端口 563 (NNTPS)''ssl_context'' 也是可选的,是一个 [[../ssl#ssl|SSLContext]] 对象。 请阅读 [[../ssl#ssl-security|安全注意事项]] 以获得最佳实践。 所有其他参数的行为与 [[#nntplib.NNTP|NNTP]] 相同。</p>
connection to the NNTP server running on host ''host'', listening at
+
<p>请注意,根据 <span id="index-4" class="target"></span>[https://tools.ietf.org/html/rfc4642.html RFC 4642],不鼓励 SSL-on-563,支持 STARTTLS,如下所述。 但是,有些服务器只支持前者。</p>
port ''port''. [[#nntplib.NNTP_SSL|<code>NNTP_SSL</code>]] objects have the same methods as
 
[[#nntplib.NNTP|<code>NNTP</code>]] objects. If ''port'' is omitted, port 563 (NNTPS) is used.
 
''ssl_context'' is also optional, and is a [[../ssl#ssl|<code>SSLContext</code>]] object.
 
Please read [[../ssl#ssl-security|<span class="std std-ref">Security considerations</span>]] for best practices.
 
All other parameters behave the same as for [[#nntplib.NNTP|<code>NNTP</code>]].</p>
 
<p>Note that SSL-on-563 is discouraged per <span id="index-4" class="target"></span>[https://tools.ietf.org/html/rfc4642.html '''RFC 4642'''], in favor of
 
STARTTLS as described below. However, some servers only support the
 
former.</p>
 
<p>Raises an [[../sys#auditing|<span class="std std-ref">auditing event</span>]] <code>nntplib.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>nntplib.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="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.2 新版功能.</span></p>
+
<p><span class="versionmodified added">3.2 版中的新功能。</span></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="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>
  
; ''exception'' <code>nntplib.</code><code>NNTPError</code>
+
; ''<span class="pre">exception</span>'' <span class="sig-prename descclassname"><span class="pre">nntplib.</span></span><span class="sig-name descname"><span class="pre">NNTPError</span></span>
: Derived from the standard exception [[../exceptions#Exception|<code>Exception</code>]], this is the base class for all exceptions raised by the [[#module-nntplib|<code>nntplib</code>]] module. Instances of this class have the following attribute:
+
: 派生自标准异常 [[../exceptions#Exception|Exception]],这是由 [[#module-nntplib|nntplib]] 模块引发的所有异常的基类。 此类的实例具有以下属性:
;; <code>response</code>
+
;; <span class="sig-name descname"><span class="pre">response</span></span>
;: The response of the server if available, as a [[../stdtypes#str|<code>str</code>]] object.
+
;: 服务器的响应(如果可用),作为 [[../stdtypes#str|str]] 对象。
  
; ''exception'' <code>nntplib.</code><code>NNTPReplyError</code>
+
; ''<span class="pre">exception</span>'' <span class="sig-prename descclassname"><span class="pre">nntplib.</span></span><span class="sig-name descname"><span class="pre">NNTPReplyError</span></span>
: Exception raised when an unexpected reply is received from the server.
+
: 从服务器收到意外回复时引发异常。
  
; ''exception'' <code>nntplib.</code><code>NNTPTemporaryError</code>
+
; ''<span class="pre">exception</span>'' <span class="sig-prename descclassname"><span class="pre">nntplib.</span></span><span class="sig-name descname"><span class="pre">NNTPTemporaryError</span></span>
: Exception raised when a response code in the range 400--499 is received.
+
: 收到 400-499 范围内的响应代码时引发异常。
  
; ''exception'' <code>nntplib.</code><code>NNTPPermanentError</code>
+
; ''<span class="pre">exception</span>'' <span class="sig-prename descclassname"><span class="pre">nntplib.</span></span><span class="sig-name descname"><span class="pre">NNTPPermanentError</span></span>
: Exception raised when a response code in the range 500--599 is received.
+
: 收到 500-599 范围内的响应代码时引发异常。
  
; ''exception'' <code>nntplib.</code><code>NNTPProtocolError</code>
+
; ''<span class="pre">exception</span>'' <span class="sig-prename descclassname"><span class="pre">nntplib.</span></span><span class="sig-name descname"><span class="pre">NNTPProtocolError</span></span>
: Exception raised when a reply is received from the server that does not begin with a digit in the range 1--5.
+
: 当从服务器收到不以 1-5 范围内的数字开头的回复时引发异常。
  
; ''exception'' <code>nntplib.</code><code>NNTPDataError</code>
+
; ''<span class="pre">exception</span>'' <span class="sig-prename descclassname"><span class="pre">nntplib.</span></span><span class="sig-name descname"><span class="pre">NNTPDataError</span></span>
: Exception raised when there is some error in the response data.
+
: 响应数据中存在错误时引发异常。
  
 
<div id="nntp-objects" class="section">
 
<div id="nntp-objects" class="section">
  
<span id="id1"></span>
+
<span id="id3"></span>
== NNTP Objects ==
+
== NNTP 对象 ==
  
When connected, [[#nntplib.NNTP|<code>NNTP</code>]] and [[#nntplib.NNTP_SSL|<code>NNTP_SSL</code>]] objects support the
+
连接时,[[#nntplib.NNTP|NNTP]] [[#nntplib.NNTP_SSL|NNTP_SSL]] 对象支持以下方法和属性。
following methods and attributes.
 
  
 
<div id="attributes" class="section">
 
<div id="attributes" class="section">
  
=== Attributes ===
+
=== 属性 ===
  
 
<dl>
 
<dl>
<dt><code>NNTP.</code><code>nntp_version</code></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">NNTP.</span></span><span class="sig-name descname"><span class="pre">nntp_version</span></span></dt>
<dd><p>An integer representing the version of the NNTP protocol supported by the
+
<dd><p>一个整数,表示服务器支持的 NNTP 协议版本。 实际上,对于广告 <span id="index-5" class="target"></span>[https://tools.ietf.org/html/rfc3977.html RFC 3977] 合规性的服务器,这应该是 <code>2</code>,而对于其他服务器,这应该是 <code>1</code></p>
server. In practice, this should be <code>2</code> for servers advertising
 
<span id="index-5" class="target"></span>[https://tools.ietf.org/html/rfc3977.html '''RFC 3977'''] compliance and <code>1</code> for others.</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.2 新版功能.</span></p>
+
<p><span class="versionmodified added">3.2 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt><code>NNTP.</code><code>nntp_implementation</code></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">NNTP.</span></span><span class="sig-name descname"><span class="pre">nntp_implementation</span></span></dt>
<dd><p>A string describing the software name and version of the NNTP server,
+
<dd><p>描述 NNTP 服务器的软件名称和版本的字符串,如果服务器未通告,则为 [[../constants#None|None]]</p>
or [[../constants#None|<code>None</code>]] if not advertised by the server.</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.2 新版功能.</span></p>
+
<p><span class="versionmodified added">3.2 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
第205行: 第166行:
 
<div id="methods" class="section">
 
<div id="methods" class="section">
  
=== Methods ===
+
=== 方法 ===
  
The ''response'' that is returned as the first item in the return tuple of almost
+
在几乎所有方法的返回元组中作为第一项返回的 ''response'' 是服务器的响应:一个以三位数代码开头的字符串。 如果服务器的响应指示错误,则该方法会引发上述异常之一。
all methods is the server's response: a string beginning with a three-digit
 
code. If the server's response indicates an error, the method raises one of
 
the above exceptions.
 
  
Many of the following methods take an optional keyword-only argument ''file''.
+
以下许多方法采用可选的仅关键字参数 ''file''。 当提供 ''file'' 参数时,它必须是为二进制写入打开的 [[../../glossary#term-file-object|file object]],或要写入的磁盘文件的名称。 然后该方法会将服务器返回的任何数据(响应行和终止点除外)写入文件; 该方法通常返回的任何行、元组或对象列表都将为空。
When the ''file'' argument is supplied, it must be either a [[../../glossary#term-file-object|<span class="xref std std-term">file object</span>]]
 
opened for binary writing, or the name of an on-disk file to be written to.
 
The method will then write any data returned by the server (except for the
 
response line and the terminating dot) to the file; any list of lines,
 
tuples or objects that the method normally returns will be empty.
 
  
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<span class="versionmodified changed">3.2 版更改: </span>Many of the following methods have been reworked and fixed, which makes
+
<span class="versionmodified changed"> 3.2 版更改: </span> 以下许多方法已被重新设计和修复,这使得它们与 3.1 版本不兼容。
them incompatible with their 3.1 counterparts.
 
  
  
 
</div>
 
</div>
; <code>NNTP.</code><code>quit</code><span class="sig-paren">(</span><span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">NNTP.</span></span><span class="sig-name descname"><span class="pre">quit</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span>
: Send a <code>QUIT</code> command and close the connection. Once this method has been called, no other methods of the NNTP object should be called.
+
: 发送 <code>QUIT</code> 命令并关闭连接。 一旦调用了此方法,就不应调用 NNTP 对象的其他方法。
  
; <code>NNTP.</code><code>getwelcome</code><span class="sig-paren">(</span><span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">NNTP.</span></span><span class="sig-name descname"><span class="pre">getwelcome</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span>
: Return the welcome message sent by the server in reply to the initial connection. (This message sometimes contains disclaimers or help information that may be relevant to the user.)
+
: 返回服务器发送的欢迎消息以回复初始连接。 (此消息有时包含可能与用户相关的免责声明或帮助信息。)
  
 
<dl>
 
<dl>
<dt><code>NNTP.</code><code>getcapabilities</code><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">NNTP.</span></span><span class="sig-name descname"><span class="pre">getcapabilities</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
<dd><p>Return the <span id="index-6" class="target"></span>[https://tools.ietf.org/html/rfc3977.html '''RFC 3977'''] capabilities advertised by the server, as a
+
<dd><p>返回服务器通告的 <span id="index-6" class="target"></span>[https://tools.ietf.org/html/rfc3977.html RFC 3977] 能力,作为 [[../stdtypes#dict|dict]] 实例映射能力名称到(可能为空)值列表。 在不理解 <code>CAPABILITIES</code> 命令的旧服务器上,将返回一个空字典。</p>
[[../stdtypes#dict|<code>dict</code>]] instance mapping capability names to (possibly empty) lists
 
of values. On legacy servers which don't understand the <code>CAPABILITIES</code>
 
command, an empty dictionary is returned instead.</p>
 
 
<div class="doctest highlight-default notranslate">
 
<div class="doctest highlight-default notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>&gt;&gt;&gt; s = NNTP('news.gmane.io')
+
<syntaxhighlight lang="python">>>> s = NNTP('news.gmane.io')
&gt;&gt;&gt; 'POST' in s.getcapabilities()
+
>>> 'POST' in s.getcapabilities()
True</pre>
+
True</syntaxhighlight>
  
 
</div>
 
</div>
第251行: 第200行:
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.2 新版功能.</span></p>
+
<p><span class="versionmodified added">3.2 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt><code>NNTP.</code><code>login</code><span class="sig-paren">(</span>''<span class="n">user</span><span class="o">=</span><span class="default_value">None</span>'', ''<span class="n">password</span><span class="o">=</span><span class="default_value">None</span>'', ''<span class="n">usenetrc</span><span class="o">=</span><span class="default_value">True</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">NNTP.</span></span><span class="sig-name descname"><span class="pre">login</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">user</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">password</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">usenetrc</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Send <code>AUTHINFO</code> commands with the user name and password. If ''user''
+
<dd><p>使用用户名和密码发送 <code>AUTHINFO</code> 命令。 如果 ''user'' ''password'' <code>None</code> ''usenetrc'' 为真,则将尽可能使用来自 <code>~/.netrc</code> 的凭据。</p>
and ''password'' are <code>None</code> and ''usenetrc'' is true, credentials from
+
<p>除非故意延迟,登录通常在[[#nntplib.NNTP|NNTP]]对象初始化期间进行,不需要单独调用该函数。 要强制延迟认证,在创建对象时不能设置''user''''password'',必须设置''usenetrc''为False。</p>
<code>~/.netrc</code> will be used if possible.</p>
 
<p>Unless intentionally delayed, login is normally performed during the
 
[[#nntplib.NNTP|<code>NNTP</code>]] object initialization and separately calling this function
 
is unnecessary. To force authentication to be delayed, you must not set
 
''user'' or ''password'' when creating the object, and must set ''usenetrc'' to
 
False.</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.2 新版功能.</span></p>
+
<p><span class="versionmodified added">3.2 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt><code>NNTP.</code><code>starttls</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">NNTP.</span></span><span class="sig-name descname"><span class="pre">starttls</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>Send a <code>STARTTLS</code> command. This will enable encryption on the NNTP
+
<dd><p>发送 <code>STARTTLS</code> 命令。 这将在 NNTP 连接上启用加密。 ''context'' 参数是可选的,应该是一个 [[../ssl#ssl|ssl.SSLContext]] 对象。 请阅读 [[../ssl#ssl-security|安全注意事项]] 以获得最佳实践。</p>
connection. The ''context'' argument is optional and should be a
+
<p>请注意,这可能不会在传输身份验证信息后进行,并且在 [[#nntplib.NNTP|NNTP]] 对象初始化期间,如果可能,默认情况下会进行身份验证。 有关抑制此行为的信息,请参阅 [[#nntplib.NNTP.login|NNTP.login()]]</p>
[[../ssl#ssl|<code>ssl.SSLContext</code>]] object. Please read [[../ssl#ssl-security|<span class="std std-ref">Security considerations</span>]] for best
 
practices.</p>
 
<p>Note that this may not be done after authentication information has
 
been transmitted, and authentication occurs by default if possible during a
 
[[#nntplib.NNTP|<code>NNTP</code>]] object initialization. See [[#nntplib.NNTP.login|<code>NNTP.login()</code>]] for information
 
on suppressing this behavior.</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.2 新版功能.</span></p>
+
<p><span class="versionmodified added">3.2 版中的新功能。</span></p>
  
 
</div>
 
</div>
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<p><span class="versionmodified changed">在 3.4 版更改: </span>The method now supports hostname check with
+
<p><span class="versionmodified changed">在 3.4 版更改:</span>该方法现在支持使用 [[../ssl#ssl.SSLContext|ssl.SSLContext.check_hostname]] ''Server Name Indication'' 进行主机名检查(请参阅 [[../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></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt><code>NNTP.</code><code>newgroups</code><span class="sig-paren">(</span>''<span class="n">date</span>'', ''<span class="o">*</span>'', ''<span class="n">file</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">NNTP.</span></span><span class="sig-name descname"><span class="pre">newgroups</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">date</span></span>'', ''<span class="o"><span class="pre">*</span></span>'', ''<span class="n"><span class="pre">file</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>Send a <code>NEWGROUPS</code> command. The ''date'' argument should be a
+
<dd><p>发送 <code>NEWGROUPS</code> 命令。 ''date'' 参数应该是 [[../datetime#datetime|datetime.date]] [[../datetime#datetime|datetime.datetime]] 对象。 返回一对 <code>(response, groups)</code>,其中 ''groups'' 是一个列表,表示自给定 ''date'' 以来新的组。 但是,如果提供 ''file'',则 ''groups'' 将为空。</p>
[[../datetime#datetime|<code>datetime.date</code>]] or [[../datetime#datetime|<code>datetime.datetime</code>]] object.
 
Return a pair <code>(response, groups)</code> where ''groups'' is a list representing
 
the groups that are new since the given ''date''. If ''file'' is supplied,
 
though, then ''groups'' will be empty.</p>
 
 
<div class="doctest highlight-default notranslate">
 
<div class="doctest highlight-default notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>&gt;&gt;&gt; from datetime import date, timedelta
+
<syntaxhighlight lang="python">>>> from datetime import date, timedelta
&gt;&gt;&gt; resp, groups = s.newgroups(date.today() - timedelta(days=3))
+
>>> resp, groups = s.newgroups(date.today() - timedelta(days=3))
&gt;&gt;&gt; len(groups)  
+
>>> len(groups)  
 
85
 
85
&gt;&gt;&gt; groups[0]  
+
>>> groups[0]  
GroupInfo(group='gmane.network.tor.devel', last='4', first='1', flag='m')</pre>
+
GroupInfo(group='gmane.network.tor.devel', last='4', first='1', flag='m')</syntaxhighlight>
  
 
</div>
 
</div>
第317行: 第248行:
  
 
<dl>
 
<dl>
<dt><code>NNTP.</code><code>newnews</code><span class="sig-paren">(</span>''<span class="n">group</span>'', ''<span class="n">date</span>'', ''<span class="o">*</span>'', ''<span class="n">file</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">NNTP.</span></span><span class="sig-name descname"><span class="pre">newnews</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">group</span></span>'', ''<span class="n"><span class="pre">date</span></span>'', ''<span class="o"><span class="pre">*</span></span>'', ''<span class="n"><span class="pre">file</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>Send a <code>NEWNEWS</code> command. Here, ''group'' is a group name or <code>'*'</code>, and
+
<dd><p>发送 <code>NEWNEWS</code> 命令。 这里,''group'' 是组名或 <code>'*'</code>''date'' [[#nntplib.NNTP.newgroups|newgroups()]] 具有相同的含义。 返回一对 <code>(response, articles)</code>,其中 ''articles'' 是消息 ID 列表。</p>
''date'' has the same meaning as for [[#nntplib.NNTP.newgroups|<code>newgroups()</code>]]. Return a pair
+
<p>NNTP 服务器管理员经常禁用此命令。</p></dd></dl>
<code>(response, articles)</code> where ''articles'' is a list of message ids.</p>
 
<p>This command is frequently disabled by NNTP server administrators.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt><code>NNTP.</code><code>list</code><span class="sig-paren">(</span>''<span class="n">group_pattern</span><span class="o">=</span><span class="default_value">None</span>'', ''<span class="o">*</span>'', ''<span class="n">file</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">NNTP.</span></span><span class="sig-name descname"><span class="pre">list</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">group_pattern</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>'', ''<span class="o"><span class="pre">*</span></span>'', ''<span class="n"><span class="pre">file</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>Send a <code>LIST</code> or <code>LIST ACTIVE</code> command. Return a pair
+
<dd><p>发送 <code>LIST</code> <code>LIST ACTIVE</code> 命令。 返回一对 <code>(response, list)</code>,其中 ''list'' 是表示此 NNTP 服务器可用的所有组的元组列表,可选择匹配模式字符串 ''group_pattern''。 每个元组的格式为 <code>(group, last, first, flag)</code>,其中 ''group'' 是组名,''last'' ''first'' 是最后和第一个文章编号,[ X150X]flag 通常采用以下值之一:</p>
<code>(response, list)</code> where ''list'' is a list of tuples representing all
 
the groups available from this NNTP server, optionally matching the
 
pattern string ''group_pattern''. Each tuple has the form
 
<code>(group, last, first, flag)</code>, where ''group'' is a group name, ''last''
 
and ''first'' are the last and first article numbers, and ''flag'' usually
 
takes one of these values:</p>
 
 
<ul>
 
<ul>
<li><p><code>y</code>: Local postings and articles from peers are allowed.</p></li>
+
<li><p><code>y</code>:允许本地发帖和同行文章。</p></li>
<li><p><code>m</code>: The group is moderated and all postings must be approved.</p></li>
+
<li><p><code>m</code>:该群组已审核,所有帖子必须获得批准。</p></li>
<li><p><code>n</code>: No local postings are allowed, only articles from peers.</p></li>
+
<li><p><code>n</code>:不允许本地发帖,只能发同行文章。</p></li>
<li><p><code>j</code>: Articles from peers are filed in the junk group instead.</p></li>
+
<li><p><code>j</code>:来自同行的文章被提交到垃圾组。</p></li>
<li><p><code>x</code>: No local postings, and articles from peers are ignored.</p></li>
+
<li><p><code>x</code>:无本地发帖,忽略同行文章。</p></li>
<li><p><code>=foo.bar</code>: Articles are filed in the <code>foo.bar</code> group instead.</p></li></ul>
+
<li><p><code>=foo.bar</code>:文章改为在<code>foo.bar</code>组中归档。</p></li></ul>
  
<p>If ''flag'' has another value, then the status of the newsgroup should be
+
<p>如果 ''flag'' 有另一个值,那么新闻组的状态应该被认为是未知的。</p>
considered unknown.</p>
+
<p>此命令可以返回非常大的结果,尤其是在未指定 ''group_pattern'' 的情况下。 最好离线缓存结果,除非您确实需要刷新它们。</p>
<p>This command can return very large results, especially if ''group_pattern''
 
is not specified. It is best to cache the results offline unless you
 
really need to refresh them.</p>
 
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<p><span class="versionmodified changed">3.2 版更改: </span>''group_pattern'' was added.</p>
+
<p><span class="versionmodified changed"> 3.2 版更改:添加了 </span>''group_pattern''</p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt><code>NNTP.</code><code>descriptions</code><span class="sig-paren">(</span>''<span class="n">grouppattern</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">NNTP.</span></span><span class="sig-name descname"><span class="pre">descriptions</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">grouppattern</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Send a <code>LIST NEWSGROUPS</code> command, where ''grouppattern'' is a wildmat string as
+
<dd><p>发送 <code>LIST NEWSGROUPS</code> 命令,其中 ''grouppattern'' <span id="index-7" class="target"></span>[https://tools.ietf.org/html/rfc3977.html RFC 3977] 中指定的通配符字符串(它本质上与 DOS UNIX shell 通配符相同字符串)。 返回一对 <code>(response, descriptions)</code>,其中 ''descriptions'' 是将组名称映射到文本描述的字典。</p>
specified in <span id="index-7" class="target"></span>[https://tools.ietf.org/html/rfc3977.html '''RFC 3977'''] (it's essentially the same as DOS or UNIX shell wildcard
 
strings). Return a pair <code>(response, descriptions)</code>, where ''descriptions''
 
is a dictionary mapping group names to textual descriptions.</p>
 
 
<div class="doctest highlight-default notranslate">
 
<div class="doctest highlight-default notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>&gt;&gt;&gt; resp, descs = s.descriptions('gmane.comp.python.*')
+
<syntaxhighlight lang="python">>>> resp, descs = s.descriptions('gmane.comp.python.*')
&gt;&gt;&gt; len(descs)  
+
>>> len(descs)  
 
295
 
295
&gt;&gt;&gt; descs.popitem()  
+
>>> descs.popitem()  
('gmane.comp.python.bio.general', 'BioPython discussion list (Moderated)')</pre>
+
('gmane.comp.python.bio.general', 'BioPython discussion list (Moderated)')</syntaxhighlight>
  
 
</div>
 
</div>
第372行: 第289行:
  
 
<dl>
 
<dl>
<dt><code>NNTP.</code><code>description</code><span class="sig-paren">(</span>''<span class="n">group</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">NNTP.</span></span><span class="sig-name descname"><span class="pre">description</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">group</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Get a description for a single group ''group''. If more than one group matches
+
<dd><p>获取单个组 '''' 的描述。 如果多个组匹配(如果 'group' 是真正的 Wildmat 字符串),则返回第一个匹配项。 如果没有组匹配,则返回一个空字符串。</p>
(if 'group' is a real wildmat string), return the first match. If no group
+
<p>这省略了来自服务器的响应代码。 如果需要响应代码,请使用 [[#nntplib.NNTP.descriptions|descriptions()]]</p></dd></dl>
matches, return an empty string.</p>
 
<p>This elides the response code from the server. If the response code is needed,
 
use [[#nntplib.NNTP.descriptions|<code>descriptions()</code>]].</p></dd></dl>
 
  
; <code>NNTP.</code><code>group</code><span class="sig-paren">(</span>''<span class="n">name</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">NNTP.</span></span><span class="sig-name descname"><span class="pre">group</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">name</span></span>''<span class="sig-paren">)</span>
: Send a <code>GROUP</code> command, where ''name'' is the group name. The group is selected as the current group, if it exists. Return a tuple <code>(response, count, first, last, name)</code> where ''count'' is the (estimated) number of articles in the group, ''first'' is the first article number in the group, ''last'' is the last article number in the group, and ''name'' is the group name.
+
: 发送 <code>GROUP</code> 命令,其中 ''name'' 是组名。 该组被选为当前组(如果存在)。 返回一个元组 <code>(response, count, first, last, name)</code>,其中 ''count'' 是组中的(估计)文章数,''first'' 是组中的第一个文章编号,last[ X163X]为组中最后一个货号,''name''为组名。
  
 
<dl>
 
<dl>
<dt><code>NNTP.</code><code>over</code><span class="sig-paren">(</span>''<span class="n">message_spec</span>'', ''<span class="o">*</span>'', ''<span class="n">file</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">NNTP.</span></span><span class="sig-name descname"><span class="pre">over</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">message_spec</span></span>'', ''<span class="o"><span class="pre">*</span></span>'', ''<span class="n"><span class="pre">file</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>Send an <code>OVER</code> command, or an <code>XOVER</code> command on legacy servers.
+
<dd><p>在旧服务器上发送 <code>OVER</code> 命令或 <code>XOVER</code> 命令。 ''message_spec'' 可以是表示消息 ID 的字符串,也可以是表示当前组中文章范围的 <code>(first, last)</code> 数字元组,或者表示范围的 <code>(first, None)</code> 元组文章从''first''到当前组中的最后一篇,或[[../constants#None|None]]选择当前组中的当前文章。</p>
''message_spec'' can be either a string representing a message id, or
+
<p>返回一对 <code>(response, overviews)</code>''overviews'' 是一个 <code>(article_number, overview)</code> 元组列表,每个 ''message_spec'' 选择的文章对应一个。 每个 ''overview'' 都是一个字典,项目数相同,但这个数字取决于服务器。 这些项目要么是消息标题(关键是小写的标题名称),要么是元数据项目(关键是元数据名称加上 <code>&quot;:&quot;</code>)。 NNTP 规范保证以下项目存在:</p>
a <code>(first, last)</code> tuple of numbers indicating a range of articles in
 
the current group, or a <code>(first, None)</code> tuple indicating a range of
 
articles starting from ''first'' to the last article in the current group,
 
or [[../constants#None|<code>None</code>]] to select the current article in the current group.</p>
 
<p>Return a pair <code>(response, overviews)</code>. ''overviews'' is a list of
 
<code>(article_number, overview)</code> tuples, one for each article selected
 
by ''message_spec''. Each ''overview'' is a dictionary with the same number
 
of items, but this number depends on the server. These items are either
 
message headers (the key is then the lower-cased header name) or metadata
 
items (the key is then the metadata name prepended with <code>&quot;:&quot;</code>). The
 
following items are guaranteed to be present by the NNTP specification:</p>
 
 
<ul>
 
<ul>
<li><p>the <code>subject</code>, <code>from</code>, <code>date</code>, <code>message-id</code> and <code>references</code>
+
<li><p><code>subject</code><code>from</code><code>date</code><code>message-id</code> <code>references</code> 标头</p></li>
headers</p></li>
+
<li><p><code>:bytes</code> 元数据:整个原始文章的字节数(包括标题和正文)</p></li>
<li><p>the <code>:bytes</code> metadata: the number of bytes in the entire raw article
+
<li><p><code>:lines</code> 元数据:文章正文的行数</p></li></ul>
(including headers and body)</p></li>
 
<li><p>the <code>:lines</code> metadata: the number of lines in the article body</p></li></ul>
 
  
<p>The value of each item is either a string, or [[../constants#None|<code>None</code>]] if not present.</p>
+
<p>每个项目的值要么是一个字符串,要么是 [[../constants#None|None]](如果不存在)。</p>
<p>It is advisable to use the [[#nntplib.decode_header|<code>decode_header()</code>]] function on header
+
<p>当标头值可能包含非 ASCII 字符时,建议对标头值使用 [[#nntplib.decode_header|decode_header()]] 函数:</p>
values when they may contain non-ASCII characters:</p>
 
 
<div class="highlight-python3 notranslate">
 
<div class="highlight-python3 notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>&gt;&gt;&gt; _, _, first, last, _ = s.group('gmane.comp.python.devel')
+
<syntaxhighlight lang="python3">>>> _, _, first, last, _ = s.group('gmane.comp.python.devel')
&gt;&gt;&gt; resp, overviews = s.over((last, last))
+
>>> resp, overviews = s.over((last, last))
&gt;&gt;&gt; art_num, over = overviews[0]
+
>>> art_num, over = overviews[0]
&gt;&gt;&gt; art_num
+
>>> art_num
 
117216
 
117216
&gt;&gt;&gt; list(over.keys())
+
>>> list(over.keys())
 
['xref', 'from', ':lines', ':bytes', 'references', 'date', 'message-id', 'subject']
 
['xref', 'from', ':lines', ':bytes', 'references', 'date', 'message-id', 'subject']
&gt;&gt;&gt; over['from']
+
>>> over['from']
'=?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= &lt;martin@v.loewis.de&gt;'
+
'=?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= <martin@v.loewis.de>'
&gt;&gt;&gt; nntplib.decode_header(over['from'])
+
>>> nntplib.decode_header(over['from'])
'&quot;Martin v. Löwis&quot; &lt;martin@v.loewis.de&gt;'</pre>
+
'"Martin v. Löwis" <martin@v.loewis.de>'</syntaxhighlight>
  
 
</div>
 
</div>
第428行: 第328行:
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.2 新版功能.</span></p>
+
<p><span class="versionmodified added">3.2 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
; <code>NNTP.</code><code>help</code><span class="sig-paren">(</span>''<span class="o">*</span>'', ''<span class="n">file</span><span class="o">=</span><span class="default_value">None</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">NNTP.</span></span><span class="sig-name descname"><span class="pre">help</span></span><span class="sig-paren">(</span>''<span class="o"><span class="pre">*</span></span>'', ''<span class="n"><span class="pre">file</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>
: Send a <code>HELP</code> command. Return a pair <code>(response, list)</code> where ''list'' is a list of help strings.
+
: 发送 <code>HELP</code> 命令。 返回一对 <code>(response, list)</code>,其中 ''list'' 是帮助字符串列表。
  
 
<dl>
 
<dl>
<dt><code>NNTP.</code><code>stat</code><span class="sig-paren">(</span>''<span class="n">message_spec</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">NNTP.</span></span><span class="sig-name descname"><span class="pre">stat</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">message_spec</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>Send a <code>STAT</code> command, where ''message_spec'' is either a message id
+
<dd><p>发送 <code>STAT</code> 命令,其中 ''message_spec'' 是消息 ID(包含在 <code>'&lt;'</code> <code>'&gt;'</code> 中)或当前组中的文章编号。 如果省略 ''message_spec'' [[../constants#None|None]],则考虑当前组中的当前文章。 返回一个三元组 <code>(response, number, id)</code>,其中 ''number'' 是文章编号,''id'' 是消息 ID。</p>
(enclosed in <code>'&lt;'</code> and <code>'&gt;'</code>) or an article number in the current group.
 
If ''message_spec'' is omitted or [[../constants#None|<code>None</code>]], the current article in the
 
current group is considered. Return a triple <code>(response, number, id)</code>
 
where ''number'' is the article number and ''id'' is the message id.</p>
 
 
<div class="doctest highlight-default notranslate">
 
<div class="doctest highlight-default notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>&gt;&gt;&gt; _, _, first, last, _ = s.group('gmane.comp.python.devel')
+
<syntaxhighlight lang="python">>>> _, _, first, last, _ = s.group('gmane.comp.python.devel')
&gt;&gt;&gt; resp, number, message_id = s.stat(first)
+
>>> resp, number, message_id = s.stat(first)
&gt;&gt;&gt; number, message_id
+
>>> number, message_id
(9099, '&lt;20030112190404.GE29873@epoch.metaslash.com&gt;')</pre>
+
(9099, '<20030112190404.GE29873@epoch.metaslash.com>')</syntaxhighlight>
  
 
</div>
 
</div>
第455行: 第351行:
 
</div></dd></dl>
 
</div></dd></dl>
  
; <code>NNTP.</code><code>next</code><span class="sig-paren">(</span><span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">NNTP.</span></span><span class="sig-name descname"><span class="pre">next</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span>
: Send a <code>NEXT</code> command. Return as for [[#nntplib.NNTP.stat|<code>stat()</code>]].
+
: 发送 <code>NEXT</code> 命令。 返回 [[#nntplib.NNTP.stat|stat()]]
  
; <code>NNTP.</code><code>last</code><span class="sig-paren">(</span><span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">NNTP.</span></span><span class="sig-name descname"><span class="pre">last</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span>
: Send a <code>LAST</code> command. Return as for [[#nntplib.NNTP.stat|<code>stat()</code>]].
+
: 发送 <code>LAST</code> 命令。 返回 [[#nntplib.NNTP.stat|stat()]]
  
 
<dl>
 
<dl>
<dt><code>NNTP.</code><code>article</code><span class="sig-paren">(</span>''<span class="n">message_spec</span><span class="o">=</span><span class="default_value">None</span>'', ''<span class="o">*</span>'', ''<span class="n">file</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">NNTP.</span></span><span class="sig-name descname"><span class="pre">article</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">message_spec</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>'', ''<span class="o"><span class="pre">*</span></span>'', ''<span class="n"><span class="pre">file</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>Send an <code>ARTICLE</code> command, where ''message_spec'' has the same meaning as
+
<dd><p>发送 <code>ARTICLE</code> 命令,其中 ''message_spec'' [[#nntplib.NNTP.stat|stat()]] 的含义相同。 返回一个元组 <code>(response, info)</code>,其中 ''info'' 是一个 [[../collections#collections|namedtuple]],具有三个属性 ''number''''message_id'' lines[ X142X](按此顺序)。 ''number'' 是群中的文章编号(如果信息不可用则为 0),''message_id'' 为字符串的消息 ID,以及 ''lines'' 列表包含原始消息的行(不终止换行符),包括标题和正文。</p>
for [[#nntplib.NNTP.stat|<code>stat()</code>]]. Return a tuple <code>(response, info)</code> where ''info''
 
is a [[../collections#collections|<code>namedtuple</code>]] with three attributes ''number'',
 
''message_id'' and ''lines'' (in that order). ''number'' is the article number
 
in the group (or 0 if the information is not available), ''message_id'' the
 
message id as a string, and ''lines'' a list of lines (without terminating
 
newlines) comprising the raw message including headers and body.</p>
 
 
<div class="doctest highlight-default notranslate">
 
<div class="doctest highlight-default notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>&gt;&gt;&gt; resp, info = s.article('&lt;20030112190404.GE29873@epoch.metaslash.com&gt;')
+
<syntaxhighlight lang="python">>>> resp, info = s.article('<20030112190404.GE29873@epoch.metaslash.com>')
&gt;&gt;&gt; info.number
+
>>> info.number
 
0
 
0
&gt;&gt;&gt; info.message_id
+
>>> info.message_id
'&lt;20030112190404.GE29873@epoch.metaslash.com&gt;'
+
'<20030112190404.GE29873@epoch.metaslash.com>'
&gt;&gt;&gt; len(info.lines)
+
>>> len(info.lines)
 
65
 
65
&gt;&gt;&gt; info.lines[0]
+
>>> info.lines[0]
 
b'Path: main.gmane.org!not-for-mail'
 
b'Path: main.gmane.org!not-for-mail'
&gt;&gt;&gt; info.lines[1]
+
>>> info.lines[1]
b'From: Neal Norwitz &lt;neal@metaslash.com&gt;'
+
b'From: Neal Norwitz <neal@metaslash.com>'
&gt;&gt;&gt; info.lines[-3:]
+
>>> info.lines[-3:]
[b'There is a patch for 2.3 as well as 2.2.', b'', b'Neal']</pre>
+
[b'There is a patch for 2.3 as well as 2.2.', b'', b'Neal']</syntaxhighlight>
  
 
</div>
 
</div>
第492行: 第382行:
 
</div></dd></dl>
 
</div></dd></dl>
  
; <code>NNTP.</code><code>head</code><span class="sig-paren">(</span>''<span class="n">message_spec</span><span class="o">=</span><span class="default_value">None</span>'', ''<span class="o">*</span>'', ''<span class="n">file</span><span class="o">=</span><span class="default_value">None</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">NNTP.</span></span><span class="sig-name descname"><span class="pre">head</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">message_spec</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>'', ''<span class="o"><span class="pre">*</span></span>'', ''<span class="n"><span class="pre">file</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>
: Same as [[#nntplib.NNTP.article|<code>article()</code>]], but sends a <code>HEAD</code> command. The ''lines'' returned (or written to ''file'') will only contain the message headers, not the body.
+
: [[#nntplib.NNTP.article|article()]] 相同,但发送 <code>HEAD</code> 命令。 返回(或写入 ''file'')的 ''lines'' 将仅包含消息标题,而不包含正文。
  
; <code>NNTP.</code><code>body</code><span class="sig-paren">(</span>''<span class="n">message_spec</span><span class="o">=</span><span class="default_value">None</span>'', ''<span class="o">*</span>'', ''<span class="n">file</span><span class="o">=</span><span class="default_value">None</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">NNTP.</span></span><span class="sig-name descname"><span class="pre">body</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">message_spec</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>'', ''<span class="o"><span class="pre">*</span></span>'', ''<span class="n"><span class="pre">file</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>
: Same as [[#nntplib.NNTP.article|<code>article()</code>]], but sends a <code>BODY</code> command. The ''lines'' returned (or written to ''file'') will only contain the message body, not the headers.
+
: [[#nntplib.NNTP.article|article()]] 相同,但发送 <code>BODY</code> 命令。 返回(或写入 ''file'')的 ''lines'' 将仅包含邮件正文,而不包含标题。
  
 
<dl>
 
<dl>
<dt><code>NNTP.</code><code>post</code><span class="sig-paren">(</span>''<span class="n">data</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">NNTP.</span></span><span class="sig-name descname"><span class="pre">post</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">data</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Post an article using the <code>POST</code> command. The ''data'' argument is either
+
<dd><p>使用 <code>POST</code> 命令发表文章。 ''data'' 参数是为二进制读取而打开的 [[../../glossary#term-file-object|文件对象]] ,或任何可迭代的字节对象(代表要发布的文章的原始行)。 它应该代表一篇格式良好的新闻文章,包括所需的标题。 [[#nntplib.NNTP.post|post()]] 方法自动转义以 <code>.</code> 开头的行并附加终止行。</p>
a [[../../glossary#term-file-object|<span class="xref std std-term">file object</span>]] opened for binary reading, or any iterable of bytes
+
<p>如果该方法成功,则返回服务器的响应。 如果服务器拒绝发布,则会引发 [[#nntplib.NNTPReplyError|NNTPReplyError]]</p></dd></dl>
objects (representing raw lines of the article to be posted). It should
 
represent a well-formed news article, including the required headers. The
 
[[#nntplib.NNTP.post|<code>post()</code>]] method automatically escapes lines beginning with <code>.</code> and
 
appends the termination line.</p>
 
<p>If the method succeeds, the server's response is returned. If the server
 
refuses posting, a [[#nntplib.NNTPReplyError|<code>NNTPReplyError</code>]] is raised.</p></dd></dl>
 
  
; <code>NNTP.</code><code>ihave</code><span class="sig-paren">(</span>''<span class="n">message_id</span>'', ''<span class="n">data</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">NNTP.</span></span><span class="sig-name descname"><span class="pre">ihave</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">message_id</span></span>'', ''<span class="n"><span class="pre">data</span></span>''<span class="sig-paren">)</span>
: Send an <code>IHAVE</code> command. ''message_id'' is the id of the message to send to the server (enclosed in <code>'&lt;'</code> and <code>'&gt;'</code>). The ''data'' parameter and the return value are the same as for [[#nntplib.NNTP.post|<code>post()</code>]].
+
: 发送 <code>IHAVE</code> 命令。 ''message_id'' 是要发送到服务器的消息的 id(包含在 <code>'&lt;'</code> <code>'&gt;'</code> 中)。 ''data'' 参数和返回值与 [[#nntplib.NNTP.post|post()]] 相同。
  
; <code>NNTP.</code><code>date</code><span class="sig-paren">(</span><span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">NNTP.</span></span><span class="sig-name descname"><span class="pre">date</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span>
: Return a pair <code>(response, date)</code>. ''date'' is a [[../datetime#datetime|<code>datetime</code>]] object containing the current date and time of the server.
+
: 返回一对 <code>(response, date)</code>''date'' 是一个 [[../datetime#datetime|datetime]] 对象,包含服务器的当前日期和时间。
  
; <code>NNTP.</code><code>slave</code><span class="sig-paren">(</span><span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">NNTP.</span></span><span class="sig-name descname"><span class="pre">slave</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span>
: Send a <code>SLAVE</code> command. Return the server's ''response''.
+
: 发送 <code>SLAVE</code> 命令。 返回服务器的 ''响应''
  
; <code>NNTP.</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">NNTP.</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 or response. A value of <code>2</code> or higher produces the maximum amount of debugging output, logging each line sent and received on the connection (including message text).
+
: 设置实例的调试级别。 这控制打印的调试输出量。 默认值 <code>0</code> 不产生调试输出。 <code>1</code> 的值会产生适量的调试输出,通常每个请求或响应只有一行。 <code>2</code> 或更高的值会产生最大数量的调试输出,记录连接上发送和接收的每一行(包括消息文本)。
  
The following are optional NNTP extensions defined in <span id="index-8" class="target"></span>[https://tools.ietf.org/html/rfc2980.html '''RFC 2980''']. Some of
+
以下是 <span id="index-8" class="target"></span>[https://tools.ietf.org/html/rfc2980.html RFC 2980] 中定义的可选 NNTP 扩展。 其中一些已被 <span id="index-9" class="target"></span>[https://tools.ietf.org/html/rfc3977.html RFC 3977] 中的新命令取代。
them have been superseded by newer commands in <span id="index-9" class="target"></span>[https://tools.ietf.org/html/rfc3977.html '''RFC 3977'''].
 
  
; <code>NNTP.</code><code>xhdr</code><span class="sig-paren">(</span>''<span class="n">hdr</span>'', ''<span class="n">str</span>'', ''<span class="o">*</span>'', ''<span class="n">file</span><span class="o">=</span><span class="default_value">None</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">NNTP.</span></span><span class="sig-name descname"><span class="pre">xhdr</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">hdr</span></span>'', ''<span class="n"><span class="pre">str</span></span>'', ''<span class="o"><span class="pre">*</span></span>'', ''<span class="n"><span class="pre">file</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>
: Send an <code>XHDR</code> command. The ''hdr'' argument is a header keyword, e.g. <code>'subject'</code>. The ''str'' argument should have the form <code>'first-last'</code> where ''first'' and ''last'' are the first and last article numbers to search. Return a pair <code>(response, list)</code>, where ''list'' is a list of pairs <code>(id, text)</code>, where ''id'' is an article number (as a string) and ''text'' is the text of the requested header for that article. If the ''file'' parameter is supplied, then the output of the <code>XHDR</code> command is stored in a file. If ''file'' is a string, then the method will open a file with that name, write to it then close it. If ''file'' is a [[../../glossary#term-file-object|<span class="xref std std-term">file object</span>]], then it will start calling <code>write()</code> on it to store the lines of the command output. If ''file'' is supplied, then the returned ''list'' is an empty list.
+
: 发送 <code>XHDR</code> 命令。 ''hdr'' 参数是标题关键字,例如 <code>'subject'</code>''str'' 参数的格式应为 <code>'first-last'</code>,其中 ''first'' ''last'' 是要搜索的第一个和最后一个文章编号。 返回一对 <code>(response, list)</code>,其中 ''list'' 是对 <code>(id, text)</code> 的列表,其中 ''id'' 是商品编号(作为字符串)和 '']text'' 是该文章请求标题的文本。 如果提供 ''file'' 参数,则 <code>XHDR</code> 命令的输出存储在文件中。 如果 ''file'' 是一个字符串,那么该方法将打开一个具有该名称的文件,写入然后关闭它。 如果 ''file'' 是一个 [[../../glossary#term-file-object|file object]],那么它将开始调用 <code>write()</code> 来存储命令输出的行。 如果提供了 ''file'',则返回的 ''list'' 是一个空列表。
  
; <code>NNTP.</code><code>xover</code><span class="sig-paren">(</span>''<span class="n">start</span>'', ''<span class="n">end</span>'', ''<span class="o">*</span>'', ''<span class="n">file</span><span class="o">=</span><span class="default_value">None</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">NNTP.</span></span><span class="sig-name descname"><span class="pre">xover</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">start</span></span>'', ''<span class="n"><span class="pre">end</span></span>'', ''<span class="o"><span class="pre">*</span></span>'', ''<span class="n"><span class="pre">file</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>
: Send an <code>XOVER</code> command. ''start'' and ''end'' are article numbers delimiting the range of articles to select. The return value is the same of for [[#nntplib.NNTP.over|<code>over()</code>]]. It is recommended to use [[#nntplib.NNTP.over|<code>over()</code>]] instead, since it will automatically use the newer <code>OVER</code> command if available.
+
: 发送 <code>XOVER</code> 命令。 ''start'' ''end'' 是界定要选择的文章范围的文章编号。 返回值与 [[#nntplib.NNTP.over|over()]] 相同。 建议改用 [[#nntplib.NNTP.over|over()]],因为它会自动使用较新的 <code>OVER</code> 命令(如果可用)。
  
  
第536行: 第419行:
 
<div id="utility-functions" class="section">
 
<div id="utility-functions" class="section">
  
== Utility functions ==
+
== 实用功能 ==
  
The module also defines the following utility function:
+
该模块还定义了以下实用程序函数:
  
 
<dl>
 
<dl>
<dt><code>nntplib.</code><code>decode_header</code><span class="sig-paren">(</span>''<span class="n">header_str</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">nntplib.</span></span><span class="sig-name descname"><span class="pre">decode_header</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">header_str</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Decode a header value, un-escaping any escaped non-ASCII characters.
+
<dd><p>解码标头值,取消转义任何转义的非 ASCII 字符。 ''header_str'' 必须是一个 [[../stdtypes#str|str]] 对象。 返回未转义的值。 建议使用此函数以人类可读的形式显示一些标题:</p>
''header_str'' must be a [[../stdtypes#str|<code>str</code>]] object. The unescaped value is
 
returned. Using this function is recommended to display some headers
 
in a human readable form:</p>
 
 
<div class="highlight-python3 notranslate">
 
<div class="highlight-python3 notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>&gt;&gt;&gt; decode_header(&quot;Some subject&quot;)
+
<syntaxhighlight lang="python3">>>> decode_header("Some subject")
 
'Some subject'
 
'Some subject'
&gt;&gt;&gt; decode_header(&quot;=?ISO-8859-15?Q?D=E9buter_en_Python?=&quot;)
+
>>> decode_header("=?ISO-8859-15?Q?D=E9buter_en_Python?=")
 
'Débuter en Python'
 
'Débuter en Python'
&gt;&gt;&gt; decode_header(&quot;Re: =?UTF-8?B?cHJvYmzDqG1lIGRlIG1hdHJpY2U=?=&quot;)
+
>>> decode_header("Re: =?UTF-8?B?cHJvYmzDqG1lIGRlIG1hdHJpY2U=?=")
'Re: problème de matrice'</pre>
+
'Re: problème de matrice'</syntaxhighlight>
  
 
</div>
 
</div>
第563行: 第443行:
  
 
</div>
 
</div>
 +
 +
</div>
 +
<div class="clearer">
 +
 +
  
 
</div>
 
</div>
  
[[Category:Python 3.9 中文文档]]
+
[[Category:Python 3.9 文档]]

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

nntplib — NNTP 协议客户端

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



该模块定义了类 NNTP,它实现了网络新闻传输协议的客户端。 它可用于实现新闻阅读器或海报,或自动化新闻处理器。 它兼容 RFC 3977 以及旧的 RFC 977RFC 2980[ X121X]。

下面是两个如何使用它的小例子。 要列出有关新闻组的一些统计信息并打印最近 10 篇文章的主题:

>>> s = nntplib.NNTP('news.gmane.io')
>>> resp, count, first, last, name = s.group('gmane.comp.python.committers')
>>> print('Group', name, 'has', count, 'articles, range', first, 'to', last)
Group gmane.comp.python.committers has 1096 articles, range 1 to 1096
>>> resp, overviews = s.over((last - 9, last))
>>> for id, over in overviews:
...     print(id, nntplib.decode_header(over['subject']))
...
1087 Re: Commit privileges for Łukasz Langa
1088 Re: 3.2 alpha 2 freeze
1089 Re: 3.2 alpha 2 freeze
1090 Re: Commit privileges for Łukasz Langa
1091 Re: Commit privileges for Łukasz Langa
1092 Updated ssh key
1093 Re: Updated ssh key
1094 Re: Updated ssh key
1095 Hello fellow committers!
1096 Re: Hello fellow committers!
>>> s.quit()
'205 Bye!'

从二进制文件发布文章(假设文章具有有效标题,并且您有权在特定新闻组上发布):

>>> s = nntplib.NNTP('news.gmane.io')
>>> f = open('article.txt', 'rb')
>>> s.post(f)
'240 Article posted successfully.'
>>> s.quit()
'205 Bye!'

模块本身定义了以下类:

class nntplib.NNTP(host, port=119, user=None, password=None, readermode=None, usenetrc=False[, timeout])

返回一个新的 NNTP 对象,表示与运行在主机 主机 上的 NNTP 服务器的连接,监听端口 端口 。 可以为套接字连接指定可选的 timeout。 如果提供了可选的 userpassword,或者 /.netrc 中存在合适的凭据并且可选标志 usenetrc 为真,则 [ X171X] 和 AUTHINFO PASS 命令用于向服务器识别和验证用户。 如果可选标志 readermode 为真,则在执行身份验证之前发送 mode reader 命令。 如果您连接到本地机器上的 NNTP 服务器并打算调用特定于阅读器的命令,例如 group,有时需要阅读器模式。 如果您收到意外的 NNTPPermanentErrors,您可能需要设置 readermodeNNTP 类支持 [X39X]with 语句无条件消耗 OSError 异常并在完成后关闭 NNTP 连接,例如:

>>> from nntplib import NNTP
>>> with NNTP('news.gmane.io') as n:
...     n.group('gmane.comp.python.committers')
... 
('211 1755 1 1755 gmane.comp.python.committers', 1755, 1, 1755, 'gmane.comp.python.committers')
>>>

在 3.2 版更改:usenetrc 现在默认为 False

在 3.3 版更改:添加了对 with 语句的支持。

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

class nntplib.NNTP_SSL(host, port=563, user=None, password=None, ssl_context=None, readermode=None, usenetrc=False[, timeout])

返回一个新的 NNTP_SSL 对象,表示与运行在主机 主机 上的 NNTP 服务器的加密连接,监听端口 端口NNTP_SSL 对象与 NNTP 对象具有相同的方法。 如果省略 port,则使用端口 563 (NNTPS)。 ssl_context 也是可选的,是一个 SSLContext 对象。 请阅读 安全注意事项 以获得最佳实践。 所有其他参数的行为与 NNTP 相同。

请注意,根据 RFC 4642,不鼓励 SSL-on-563,支持 STARTTLS,如下所述。 但是,有些服务器只支持前者。

3.2 版中的新功能。

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

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

exception nntplib.NNTPError
派生自标准异常 Exception,这是由 nntplib 模块引发的所有异常的基类。 此类的实例具有以下属性:
response
服务器的响应(如果可用),作为 str 对象。
exception nntplib.NNTPReplyError
从服务器收到意外回复时引发异常。
exception nntplib.NNTPTemporaryError
收到 400-499 范围内的响应代码时引发异常。
exception nntplib.NNTPPermanentError
收到 500-599 范围内的响应代码时引发异常。
exception nntplib.NNTPProtocolError
当从服务器收到不以 1-5 范围内的数字开头的回复时引发异常。
exception nntplib.NNTPDataError
响应数据中存在错误时引发异常。

NNTP 对象

连接时,NNTPNNTP_SSL 对象支持以下方法和属性。

属性

NNTP.nntp_version

一个整数,表示服务器支持的 NNTP 协议版本。 实际上,对于广告 RFC 3977 合规性的服务器,这应该是 2,而对于其他服务器,这应该是 1

3.2 版中的新功能。

NNTP.nntp_implementation

描述 NNTP 服务器的软件名称和版本的字符串,如果服务器未通告,则为 None

3.2 版中的新功能。


方法

在几乎所有方法的返回元组中作为第一项返回的 response 是服务器的响应:一个以三位数代码开头的字符串。 如果服务器的响应指示错误,则该方法会引发上述异常之一。

以下许多方法采用可选的仅关键字参数 file。 当提供 file 参数时,它必须是为二进制写入打开的 file object,或要写入的磁盘文件的名称。 然后该方法会将服务器返回的任何数据(响应行和终止点除外)写入文件; 该方法通常返回的任何行、元组或对象列表都将为空。

3.2 版更改: 以下许多方法已被重新设计和修复,这使得它们与 3.1 版本不兼容。


NNTP.quit()
发送 QUIT 命令并关闭连接。 一旦调用了此方法,就不应调用 NNTP 对象的其他方法。
NNTP.getwelcome()
返回服务器发送的欢迎消息以回复初始连接。 (此消息有时包含可能与用户相关的免责声明或帮助信息。)
NNTP.getcapabilities()

返回服务器通告的 RFC 3977 能力,作为 dict 实例映射能力名称到(可能为空)值列表。 在不理解 CAPABILITIES 命令的旧服务器上,将返回一个空字典。

>>> s = NNTP('news.gmane.io')
>>> 'POST' in s.getcapabilities()
True

3.2 版中的新功能。

NNTP.login(user=None, password=None, usenetrc=True)

使用用户名和密码发送 AUTHINFO 命令。 如果 userpasswordNoneusenetrc 为真,则将尽可能使用来自 ~/.netrc 的凭据。

除非故意延迟,登录通常在NNTP对象初始化期间进行,不需要单独调用该函数。 要强制延迟认证,在创建对象时不能设置userpassword,必须设置usenetrc为False。

3.2 版中的新功能。

NNTP.starttls(context=None)

发送 STARTTLS 命令。 这将在 NNTP 连接上启用加密。 context 参数是可选的,应该是一个 ssl.SSLContext 对象。 请阅读 安全注意事项 以获得最佳实践。

请注意,这可能不会在传输身份验证信息后进行,并且在 NNTP 对象初始化期间,如果可能,默认情况下会进行身份验证。 有关抑制此行为的信息,请参阅 NNTP.login()

3.2 版中的新功能。

在 3.4 版更改:该方法现在支持使用 ssl.SSLContext.check_hostnameServer Name Indication 进行主机名检查(请参阅 ssl.HAS_SNI )。

NNTP.newgroups(date, *, file=None)

发送 NEWGROUPS 命令。 date 参数应该是 datetime.datedatetime.datetime 对象。 返回一对 (response, groups),其中 groups 是一个列表,表示自给定 date 以来新的组。 但是,如果提供 file,则 groups 将为空。

>>> from datetime import date, timedelta
>>> resp, groups = s.newgroups(date.today() - timedelta(days=3))
>>> len(groups) 
85
>>> groups[0] 
GroupInfo(group='gmane.network.tor.devel', last='4', first='1', flag='m')
NNTP.newnews(group, date, *, file=None)

发送 NEWNEWS 命令。 这里,group 是组名或 '*'datenewgroups() 具有相同的含义。 返回一对 (response, articles),其中 articles 是消息 ID 列表。

NNTP 服务器管理员经常禁用此命令。

NNTP.list(group_pattern=None, *, file=None)

发送 LISTLIST ACTIVE 命令。 返回一对 (response, list),其中 list 是表示此 NNTP 服务器可用的所有组的元组列表,可选择匹配模式字符串 group_pattern。 每个元组的格式为 (group, last, first, flag),其中 group 是组名,lastfirst 是最后和第一个文章编号,[ X150X]flag 通常采用以下值之一:

  • y:允许本地发帖和同行文章。

  • m:该群组已审核,所有帖子必须获得批准。

  • n:不允许本地发帖,只能发同行文章。

  • j:来自同行的文章被提交到垃圾组。

  • x:无本地发帖,忽略同行文章。

  • =foo.bar:文章改为在foo.bar组中归档。

如果 flag 有另一个值,那么新闻组的状态应该被认为是未知的。

此命令可以返回非常大的结果,尤其是在未指定 group_pattern 的情况下。 最好离线缓存结果,除非您确实需要刷新它们。

3.2 版更改:添加了 group_pattern

NNTP.descriptions(grouppattern)

发送 LIST NEWSGROUPS 命令,其中 grouppatternRFC 3977 中指定的通配符字符串(它本质上与 DOS 或 UNIX shell 通配符相同字符串)。 返回一对 (response, descriptions),其中 descriptions 是将组名称映射到文本描述的字典。

>>> resp, descs = s.descriptions('gmane.comp.python.*')
>>> len(descs) 
295
>>> descs.popitem() 
('gmane.comp.python.bio.general', 'BioPython discussion list (Moderated)')
NNTP.description(group)

获取单个组 的描述。 如果多个组匹配(如果 'group' 是真正的 Wildmat 字符串),则返回第一个匹配项。 如果没有组匹配,则返回一个空字符串。

这省略了来自服务器的响应代码。 如果需要响应代码,请使用 descriptions()

NNTP.group(name)
发送 GROUP 命令,其中 name 是组名。 该组被选为当前组(如果存在)。 返回一个元组 (response, count, first, last, name),其中 count 是组中的(估计)文章数,first 是组中的第一个文章编号,last[ X163X]为组中最后一个货号,name为组名。
NNTP.over(message_spec, *, file=None)

在旧服务器上发送 OVER 命令或 XOVER 命令。 message_spec 可以是表示消息 ID 的字符串,也可以是表示当前组中文章范围的 (first, last) 数字元组,或者表示范围的 (first, None) 元组文章从first到当前组中的最后一篇,或None选择当前组中的当前文章。

返回一对 (response, overviews)overviews 是一个 (article_number, overview) 元组列表,每个 message_spec 选择的文章对应一个。 每个 overview 都是一个字典,项目数相同,但这个数字取决于服务器。 这些项目要么是消息标题(关键是小写的标题名称),要么是元数据项目(关键是元数据名称加上 ":")。 NNTP 规范保证以下项目存在:

  • subjectfromdatemessage-idreferences 标头

  • :bytes 元数据:整个原始文章的字节数(包括标题和正文)

  • :lines 元数据:文章正文的行数

每个项目的值要么是一个字符串,要么是 None(如果不存在)。

当标头值可能包含非 ASCII 字符时,建议对标头值使用 decode_header() 函数:

>>> _, _, first, last, _ = s.group('gmane.comp.python.devel')
>>> resp, overviews = s.over((last, last))
>>> art_num, over = overviews[0]
>>> art_num
117216
>>> list(over.keys())
['xref', 'from', ':lines', ':bytes', 'references', 'date', 'message-id', 'subject']
>>> over['from']
'=?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= <martin@v.loewis.de>'
>>> nntplib.decode_header(over['from'])
'"Martin v. Löwis" <martin@v.loewis.de>'

3.2 版中的新功能。

NNTP.help(*, file=None)
发送 HELP 命令。 返回一对 (response, list),其中 list 是帮助字符串列表。
NNTP.stat(message_spec=None)

发送 STAT 命令,其中 message_spec 是消息 ID(包含在 '<''>' 中)或当前组中的文章编号。 如果省略 message_specNone,则考虑当前组中的当前文章。 返回一个三元组 (response, number, id),其中 number 是文章编号,id 是消息 ID。

>>> _, _, first, last, _ = s.group('gmane.comp.python.devel')
>>> resp, number, message_id = s.stat(first)
>>> number, message_id
(9099, '<20030112190404.GE29873@epoch.metaslash.com>')
NNTP.next()
发送 NEXT 命令。 返回 stat()
NNTP.last()
发送 LAST 命令。 返回 stat()
NNTP.article(message_spec=None, *, file=None)

发送 ARTICLE 命令,其中 message_specstat() 的含义相同。 返回一个元组 (response, info),其中 info 是一个 namedtuple,具有三个属性 numbermessage_id 和 lines[ X142X](按此顺序)。 number 是群中的文章编号(如果信息不可用则为 0),message_id 为字符串的消息 ID,以及 lines 列表包含原始消息的行(不终止换行符),包括标题和正文。

>>> resp, info = s.article('<20030112190404.GE29873@epoch.metaslash.com>')
>>> info.number
0
>>> info.message_id
'<20030112190404.GE29873@epoch.metaslash.com>'
>>> len(info.lines)
65
>>> info.lines[0]
b'Path: main.gmane.org!not-for-mail'
>>> info.lines[1]
b'From: Neal Norwitz <neal@metaslash.com>'
>>> info.lines[-3:]
[b'There is a patch for 2.3 as well as 2.2.', b'', b'Neal']
NNTP.head(message_spec=None, *, file=None)
article() 相同,但发送 HEAD 命令。 返回(或写入 file)的 lines 将仅包含消息标题,而不包含正文。
NNTP.body(message_spec=None, *, file=None)
article() 相同,但发送 BODY 命令。 返回(或写入 file)的 lines 将仅包含邮件正文,而不包含标题。
NNTP.post(data)

使用 POST 命令发表文章。 data 参数是为二进制读取而打开的 文件对象 ,或任何可迭代的字节对象(代表要发布的文章的原始行)。 它应该代表一篇格式良好的新闻文章,包括所需的标题。 post() 方法自动转义以 . 开头的行并附加终止行。

如果该方法成功,则返回服务器的响应。 如果服务器拒绝发布,则会引发 NNTPReplyError

NNTP.ihave(message_id, data)
发送 IHAVE 命令。 message_id 是要发送到服务器的消息的 id(包含在 '<''>' 中)。 data 参数和返回值与 post() 相同。
NNTP.date()
返回一对 (response, date)date 是一个 datetime 对象,包含服务器的当前日期和时间。
NNTP.slave()
发送 SLAVE 命令。 返回服务器的 响应
NNTP.set_debuglevel(level)
设置实例的调试级别。 这控制打印的调试输出量。 默认值 0 不产生调试输出。 1 的值会产生适量的调试输出,通常每个请求或响应只有一行。 2 或更高的值会产生最大数量的调试输出,记录连接上发送和接收的每一行(包括消息文本)。

以下是 RFC 2980 中定义的可选 NNTP 扩展。 其中一些已被 RFC 3977 中的新命令取代。

NNTP.xhdr(hdr, str, *, file=None)
发送 XHDR 命令。 hdr 参数是标题关键字,例如 'subject'str 参数的格式应为 'first-last',其中 firstlast 是要搜索的第一个和最后一个文章编号。 返回一对 (response, list),其中 list 是对 (id, text) 的列表,其中 id 是商品编号(作为字符串)和 ]text 是该文章请求标题的文本。 如果提供 file 参数,则 XHDR 命令的输出存储在文件中。 如果 file 是一个字符串,那么该方法将打开一个具有该名称的文件,写入然后关闭它。 如果 file 是一个 file object,那么它将开始调用 write() 来存储命令输出的行。 如果提供了 file,则返回的 list 是一个空列表。
NNTP.xover(start, end, *, file=None)
发送 XOVER 命令。 startend 是界定要选择的文章范围的文章编号。 返回值与 over() 相同。 建议改用 over(),因为它会自动使用较新的 OVER 命令(如果可用)。


实用功能

该模块还定义了以下实用程序函数:

nntplib.decode_header(header_str)

解码标头值,取消转义任何转义的非 ASCII 字符。 header_str 必须是一个 str 对象。 返回未转义的值。 建议使用此函数以人类可读的形式显示一些标题:

>>> decode_header("Some subject")
'Some subject'
>>> decode_header("=?ISO-8859-15?Q?D=E9buter_en_Python?=")
'Débuter en Python'
>>> decode_header("Re: =?UTF-8?B?cHJvYmzDqG1lIGRlIG1hdHJpY2U=?=")
'Re: problème de matrice'