nntplib — NNTP 协议客户端

源代码: Lib/nntplib.py

3.11 版后已移除: The nntplib module is deprecated (see PEP 594 for details).


此模块定义了 NNTP 类来实现网络新闻传输协议的客户端。 它可被用于实现一个新闻阅读或发布器,或是新闻自动处理程序。 它兼容了 RFC 3977 以及较旧的 RFC 977RFC 2980

Availability: not Emscripten, not WASI.

This module does not work or is not available on WebAssembly platforms wasm32-emscripten and wasm32-wasi. See WebAssembly platforms for more information.

下面是此模块的两个简单用法示例。 列出某个新闻组的一些统计数据并打印最近 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 对象,代表一个对运行于主机 host,在端口 port 上监听的 NNTP 服务器的连接。 可以为套接字连接指定可选的 timeout。 如果提供了可选的 userpassword,或者如果在 /.netrc 中存在适合的凭证并且可选的旗标 usenetrc 为真值,则会使用 AUTHINFO USERAUTHINFO PASS 命令在服务器上标识和认证用户。 如果可选的旗标 readermode 为真值,则会在执行认证之前发送 mode reader 命令。 在某些时候如果你是连接本地机器上的 NNTP 服务器并且想要调用读取者专属命令如 group 那么还必须使用读取者模式。 如果你收到预料之外的 NNTPPermanentError,你可能需要设置 readermodeNNTP 类支持使用 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')
>>>

引发一个 审计事件 nntplib.connect,附带参数 self, host, port

引发一个 审计事件 nntplib.putline,附带参数 self, line

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

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

在 3.9 版更改: 如果 timeout 参数设置为 0,创建非阻塞套接字时,它将引发 ValueError 来阻止该操作。

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

返回一个新的 NNTP_SSL 对象,代表一个对运行于主机 host,在端口 port 上监听的 NNTP 服务器的连接。 NNTP_SSL 对象具有与 NNTP 对象相同的方法。 如果 port 被省略,则会使用端口 563 (NNTPS)。 ssl_context 也是可选的,且为一个 SSLContext 对象。 请阅读 安全考量 来了解最佳实践。 所有其他形参的行为都与 NNTP 的相同。

请注意 RFC 4642 不再推荐使用 563 端口的 SSL,建议改用下文描述的 STARTTLS。 但是,某些服务器只支持前者。

引发一个 审计事件 nntplib.connect,附带参数 self, host, port

引发一个 审计事件 nntplib.putline,附带参数 self, line

3.2 新版功能.

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

在 3.9 版更改: 如果 timeout 参数设置为 0,创建非阻塞套接字时,它将引发 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_hostname服务器名称指示 (参见 ssl.HAS_SNI) 进行主机名检查。

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

发送 NEWGROUPS 命令。 date 参数应为 datetime.datedatetime.datetime 对象。 返回一个 (response, groups) 对,其中 groups 是代表给定i 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 列表。

This command is frequently disabled by NNTP server administrators.

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

Send a LIST or LIST ACTIVE command. Return a pair (response, list) 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 (group, last, first, flag), where group is a group name, last and first are the last and first article numbers, and flag usually takes one of these values:

  • y: Local postings and articles from peers are allowed.

  • m: The group is moderated and all postings must be approved.

  • n: No local postings are allowed, only articles from peers.

  • j: Articles from peers are filed in the junk group instead.

  • x: No local postings, and articles from peers are ignored.

  • =foo.bar: Articles are filed in the foo.bar group instead.

If flag has another value, then the status of the newsgroup should be considered unknown.

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.

在 3.2 版更改: group_pattern was added.

NNTP.descriptions(grouppattern)

Send a LIST NEWSGROUPS command, where grouppattern is a wildmat string as specified in RFC 3977 (it’s essentially the same as DOS or UNIX shell wildcard strings). Return a pair (response, descriptions), where descriptions is a dictionary mapping group names to textual 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)

Get a description for a single group group. If more than one group matches (if ‘group’ is a real wildmat string), return the first match. If no group matches, return an empty string.

This elides the response code from the server. If the response code is needed, use descriptions().

NNTP.group(name)

Send a GROUP command, where name is the group name. The group is selected as the current group, if it exists. Return a tuple (response, count, first, last, name) 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.

NNTP.over(message_spec, *, file=None)

Send an OVER command, or an XOVER command on legacy servers. message_spec can be either a string representing a message id, or a (first, last) tuple of numbers indicating a range of articles in the current group, or a (first, None) tuple indicating a range of articles starting from first to the last article in the current group, or None to select the current article in the current group.

Return a pair (response, overviews). overviews is a list of (article_number, overview) 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 ":"). The following items are guaranteed to be present by the NNTP specification:

  • the subject, from, date, message-id and references headers

  • the :bytes metadata: the number of bytes in the entire raw article (including headers and body)

  • the :lines metadata: the number of lines in the article body

The value of each item is either a string, or None if not present.

It is advisable to use the decode_header() function on header values when they may contain non-ASCII characters:

>>> _, _, 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)

Send a HELP command. Return a pair (response, list) where list is a list of help strings.

NNTP.stat(message_spec=None)

Send a STAT command, where message_spec is either a message id (enclosed in '<' and '>') or an article number in the current group. If message_spec is omitted or None, the current article in the current group is considered. Return a triple (response, number, id) where number is the article number and id is the message 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()

Send a NEXT command. Return as for stat().

NNTP.last()

Send a LAST command. Return as for stat().

NNTP.article(message_spec=None, *, file=None)

Send an ARTICLE command, where message_spec has the same meaning as for stat(). Return a tuple (response, info) where info is a namedtuple 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.

>>> 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)

Same as article(), but sends a HEAD command. The lines returned (or written to file) will only contain the message headers, not the body.

NNTP.body(message_spec=None, *, file=None)

Same as article(), but sends a BODY command. The lines returned (or written to file) will only contain the message body, not the headers.

NNTP.post(data)

Post an article using the POST command. The data argument is either a file object opened for binary reading, or any iterable of bytes objects (representing raw lines of the article to be posted). It should represent a well-formed news article, including the required headers. The post() method automatically escapes lines beginning with . and appends the termination line.

If the method succeeds, the server’s response is returned. If the server refuses posting, a NNTPReplyError is raised.

NNTP.ihave(message_id, data)

Send an IHAVE command. message_id is the id of the message to send to the server (enclosed in '<' and '>'). The data parameter and the return value are the same as for post().

NNTP.date()

Return a pair (response, date). date is a datetime object containing the current date and time of the server.

NNTP.slave()

Send a SLAVE command. Return the server’s response.

NNTP.set_debuglevel(level)

Set the instance’s debugging level. This controls the amount of debugging output printed. The default, 0, produces no debugging output. A value of 1 produces a moderate amount of debugging output, generally a single line per request or response. A value of 2 or higher produces the maximum amount of debugging output, logging each line sent and received on the connection (including message text).

The following are optional NNTP extensions defined in RFC 2980. Some of them have been superseded by newer commands in RFC 3977.

NNTP.xhdr(hdr, str, *, file=None)

Send an XHDR command. The hdr argument is a header keyword, e.g. 'subject'. The str argument should have the form 'first-last' where first and last are the first and last article numbers to search. Return a pair (response, list), where list is a list of pairs (id, text), 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 XHDR 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 file object, then it will start calling write() on it to store the lines of the command output. If file is supplied, then the returned list is an empty list.

NNTP.xover(start, end, *, file=None)

Send an XOVER command. start and end are article numbers delimiting the range of articles to select. The return value is the same of for over(). It is recommended to use over() instead, since it will automatically use the newer OVER command if available.

工具函数

The module also defines the following utility function:

nntplib.decode_header(header_str)

Decode a header value, un-escaping any escaped non-ASCII characters. header_str must be a str object. The unescaped value is returned. Using this function is recommended to display some headers in a human readable form:

>>> 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'