nntplibNNTP protocol clientNNTP协议客户端

Source code: Lib/nntplib.py

Deprecated since version 3.11: 自3.11版起已弃用:The nntplib module is deprecated (see PEP 594 for details).nntplib模块已弃用(有关详细信息,请参阅PEP 594)。


This module defines the class NNTP which implements the client side of the Network News Transfer Protocol. 该模块定义了实现网络新闻传输协议客户端的NNTP类。It can be used to implement a news reader or poster, or automated news processors. 它可以用于实现新闻阅读器或海报,或自动新闻处理器。It is compatible with RFC 3977 as well as the older RFC 977 and RFC 2980.它与RFC 3977以及旧的RFC 977RFC 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:要列出新闻组的一些统计信息并打印最近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!'

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):要从二进制文件发布文章(这假设文章具有有效的标题,并且您有权在特定新闻组上发布):

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

The module itself defines the following classes:模块本身定义了以下类:

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

Return a new NNTP object, representing a connection to the NNTP server running on host host, listening at port port. 返回一个新的NNTP对象,表示与主机host上运行的NNTP服务器的连接,在端口port侦听。An optional timeout can be specified for the socket connection. 可以为套接字连接指定可选timeoutIf the optional user and password are provided, or if suitable credentials are present in /.netrc and the optional flag usenetrc is true, the AUTHINFO USER and AUTHINFO PASS commands are used to identify and authenticate the user to the server. 如果提供了可选的userpassword,或者/.netrc中存在适当的凭据,并且可选的标志usenetrctrue,则使用AUTHINFO USERAUTHINFO PASS命令来标识用户并向服务器验证用户。If the optional flag readermode is true, then a mode reader command is sent before authentication is performed. 如果可选标志readermodetrue,则在执行身份验证之前发送mode reader命令。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 group. 如果您正在连接到本地计算机上的NNTP服务器并打算调用特定于读取器的命令(例如group),则有时需要读取器模式。If you get unexpected NNTPPermanentErrors, you might need to set readermode. 如果出现意外的NNTPPermanentError,则可能需要设置readermodeThe NNTP class supports the with statement to unconditionally consume OSError exceptions and to close the NNTP connection when done, e.g.:NNTP类支持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')
>>>

Raises an auditing event nntplib.connect with arguments self, host, port.使用参数selfhostport引发审核事件nntplib.connect

All commands will raise an auditing event nntplib.putline with arguments self and line, where line is the bytes about to be sent to the remote host.所有命令都将引发带有参数selfline审计事件nntplib.putline,其中line是要发送到远程主机的字节。

Changed in version 3.2:版本3.2中更改: usenetrc is now False by default.usenetrc现在默认为False

Changed in version 3.3:版本3.3中更改: Support for the with statement was added.添加了对with语句的支持。

Changed in version 3.9:版本3.9中更改: If the timeout parameter is set to be zero, it will raise a ValueError to prevent the creation of a non-blocking socket.如果timeout参数设置为零,它将引发ValueError,以防止创建非阻塞套接字。

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

Return a new NNTP_SSL object, representing an encrypted connection to the NNTP server running on host host, listening at port port. 返回一个新的NNTP_SSL对象,表示与主机host上运行的NNTP服务器的加密连接,在端口port侦听。NNTP_SSL objects have the same methods as NNTP objects. NNTP_SSL对象具有与NNTP对象相同的方法。If port is omitted, port 563 (NNTPS) is used. 如果省略port,则使用端口563(NNTPS)。ssl_context is also optional, and is a SSLContext object. ssl_context也是可选的,并且是SSLContext对象。Please read Security considerations for best practices. 请阅读最佳实践的安全注意事项All other parameters behave the same as for NNTP.所有其他参数的行为与NNTP相同。

Note that SSL-on-563 is discouraged per RFC 4642, in favor of STARTTLS as described below. 注意,根据RFC 4642,SSL-on-563是不鼓励的,支持STARTTLS,如下所述。However, some servers only support the former.然而,有些服务器只支持前者。

Raises an auditing event nntplib.connect with arguments self, host, port.使用参数selfhostport引发审核事件nntplib.connect

All commands will raise an auditing event nntplib.putline with arguments self and line, where line is the bytes about to be sent to the remote host.所有命令都将引发带有参数selfline审计事件nntplib.putline,其中line是要发送到远程主机的字节。

New in version 3.2.版本3.2中新增。

Changed in version 3.4:版本3.4中更改: The class now supports hostname check with ssl.SSLContext.check_hostname and Server Name Indication (see ssl.HAS_SNI).该类现在支持使用ssl.SSLContext.check_hostname和服务器名称指示进行主机名检查(请参阅sslHAS_SNI)。

Changed in version 3.9:版本3.9中更改: If the timeout parameter is set to be zero, it will raise a ValueError to prevent the creation of a non-blocking socket.如果timeout参数设置为零,它将引发ValueError,以防止创建非阻塞套接字。

exceptionnntplib.NNTPError

Derived from the standard exception Exception, this is the base class for all exceptions raised by the nntplib module. 派生自标准异常Exception,这是nntplib模块引发的所有异常的基类。Instances of this class have the following attribute:此类的实例具有以下属性:

response

The response of the server if available, as a str object.服务器的响应(如果可用),作为str对象。

exceptionnntplib.NNTPReplyError

Exception raised when an unexpected reply is received from the server.从服务器收到意外答复时引发异常。

exceptionnntplib.NNTPTemporaryError

Exception raised when a response code in the range 400–499 is received.收到范围400499内的响应代码时引发异常。

exceptionnntplib.NNTPPermanentError

Exception raised when a response code in the range 500–599 is received.收到范围500599内的响应代码时引发异常。

exceptionnntplib.NNTPProtocolError

Exception raised when a reply is received from the server that does not begin with a digit in the range 1–5.当从服务器收到的答复不是以15范围内的数字开头时引发异常。

exceptionnntplib.NNTPDataError

Exception raised when there is some error in the response data.响应数据中出现错误时引发异常。

NNTP Objects

When connected, NNTP and NNTP_SSL objects support the following methods and attributes.连接后,NNTPNNTP_SSL对象支持以下方法和属性。

Attributes属性

NNTP.nntp_version

An integer representing the version of the NNTP protocol supported by the server. 表示服务器支持的NNTP协议版本的整数。In practice, this should be 2 for servers advertising RFC 3977 compliance and 1 for others.实际上,对于发布RFC 3977遵从性的服务器,这应该是2,对于其他服务器,应该是1。

New in version 3.2.版本3.2中新增。

NNTP.nntp_implementation

A string describing the software name and version of the NNTP server, or None if not advertised by the server.描述NNTP服务器的软件名称和版本的字符串,如果服务器未播发,则为None

New in version 3.2.版本3.2中新增。

Methods方法

The response that is returned as the first item in the return tuple of almost 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. 以下许多方法都采用可选的仅关键字参数fileWhen the file argument is supplied, it must be either a file object opened for binary writing, or the name of an on-disk file to be written to. 当提供file参数时,它必须是为二进制写入而打开的文件对象,或者是要写入的磁盘文件的名称。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.然后,该方法将服务器返回的任何数据(响应行和终止点除外)写入文件;该方法通常返回的任何行、元组或对象列表都将为空。

Changed in version 3.2:版本3.2中更改: Many of the following methods have been reworked and fixed, which makes them incompatible with their 3.1 counterparts.以下许多方法都经过了修改和修复,这使得它们与3.1版本的方法不兼容。

NNTP.quit()

Send a QUIT command and close the connection. 发送QUIT命令并关闭连接。Once this method has been called, no other methods of the NNTP object should be called.一旦调用了此方法,就不应调用NNTP对象的其他方法。

NNTP.getwelcome()

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.)(此消息有时包含可能与用户相关的免责声明或帮助信息。)

NNTP.getcapabilities()

Return the RFC 3977 capabilities advertised by the server, as a dict instance mapping capability names to (possibly empty) lists of values. 返回服务器通告的RFC 3977功能,作为将功能名称映射到(可能为空)值列表的dict实例。On legacy servers which don’t understand the CAPABILITIES command, an empty dictionary is returned instead.在不理解CAPABILITIES命令的旧服务器上,将返回一个空字典。

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

New in version 3.2.版本3.2中新增。

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

Send AUTHINFO commands with the user name and password. 使用用户名和密码发送AUTHINFO命令。If user and password are None and usenetrc is true, credentials from ~/.netrc will be used if possible.如果userpasswordNone,而usenetrctrue,则将尽可能使用~/.netrc中的凭据。

Unless intentionally delayed, login is normally performed during the NNTP object initialization and separately calling this function is unnecessary. 除非有意延迟,否则登录通常在NNTP对象初始化期间执行,不需要单独调用此函数。To force authentication to be delayed, you must not set user or password when creating the object, and must set usenetrc to False.要强制延迟身份验证,在创建对象时不能设置userpassword,并且必须将usenetrc设置为False

New in version 3.2.版本3.2中新增。

NNTP.starttls(context=None)

Send a STARTTLS command. 发送STARTTLS命令。This will enable encryption on the NNTP connection. 这将在NNTP连接上启用加密。The context argument is optional and should be a ssl.SSLContext object. context参数是可选的,应该是ssl.SSLContext对象。Please read Security considerations for best practices.请阅读最佳实践的安全注意事项

Note that this may not be done after authentication information has been transmitted, and authentication occurs by default if possible during a NNTP object initialization. 请注意,这可能不会在传输认证信息后进行,如果可能,在NNTP对象初始化期间,默认情况下会进行认证。See NNTP.login() for information on suppressing this behavior.有关抑制此行为的信息,请参阅NNTP.login()

New in version 3.2.版本3.2中新增。

Changed in version 3.4:版本3.4中更改: The method now supports hostname check with ssl.SSLContext.check_hostname and Server Name Indication (see ssl.HAS_SNI).该方法现在支持使用ssl.SSLContext.check_hostname服务器名称指示进行主机名检查(请参阅ssl.HAS_SNI)。

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

Send a NEWGROUPS command. 发送NEWGROUPS命令。The date argument should be a datetime.date or datetime.datetime object. date参数应为datetime.datedatetime.datetime对象。Return a pair (response, groups) where groups is a list representing the groups that are new since the given date. 返回一对(response, groups),其中groups是表示自给定date以来新的组的列表。If file is supplied, though, then groups will be empty.但若提供了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)

Send a NEWNEWS command. 发送NEWNEWS命令。Here, group is a group name or '*', and date has the same meaning as for newgroups(). 这里,group是组名或'*'date的含义与newgroups()相同。Return a pair (response, articles) where articles is a list of message ids.返回一对(response, articles),其中articles是消息ID的列表。

This command is frequently disabled by NNTP server administrators.NNTP服务器管理员经常禁用此命令。

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

Send a LIST or LIST ACTIVE command. 发送LISTLIST ACTIVE命令。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. 返回一对(response, list),其中list是表示此NNTP服务器上可用的所有组的元组列表,可选地匹配模式字符串group_patternEach 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:每个元组的形式为(group, last, first, flag),其中group是组名,lastfirst是最后一个和第一个文章编号,flag通常采用以下值之一:

  • 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.:文章改为在foo.bar组中归档。

If flag has another value, then the status of the newsgroup should be considered unknown.如果flag有另一个值,则新闻组的状态应被视为未知。

This command can return very large results, especially if group_pattern is not specified. 此命令可以返回非常大的结果,特别是在未指定group_pattern的情况下。It is best to cache the results offline unless you really need to refresh them.最好脱机缓存结果,除非您确实需要刷新它们。

Changed in version 3.2:版本3.2中更改: group_pattern was added.已添加group_pattern

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). 发送LIST NEWSGROUPS命令,其中grouppatternRFC 3977中指定的通配符字符串(基本上与DOS或UNIX shell通配符字符串相同)。Return a pair (response, descriptions), where descriptions is a dictionary mapping group names to textual descriptions.返回一对(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)

Get a description for a single group group. 获取单个组group的描述。If more than one group matches (if ‘group’ is a real wildmat string), return the first match. 如果有多个组匹配(如果“group”是真正的通配符字符串),则返回第一个匹配。If no group matches, return an empty string.如果没有匹配的组,则返回空字符串。

This elides the response code from the server. 这将消除服务器的响应代码。If the response code is needed, use descriptions().如果需要响应代码,请使用descriptions()

NNTP.group(name)

Send a GROUP command, where name is the group name. 发送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.返回一个元组(response, count, first, last, name),其中count是组中的(估计的)文章数,first是组中第一个文章数,last是组的最后一个文章号,name是组名。

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

Send an OVER command, or an XOVER command on legacy servers. 在旧服务器上发送OVER命令或XOVER命令。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.可以是表示消息id的字符串,也可以是表示当前组中文章范围的数字(first, last)元组,或者是表示从当前组中的第一个到最后一个文章的文章范围的(first, None)元组,或是选择当前组中当前文章的None

Return a pair (response, overviews). 返回一对(response, overviews)overviews is a list of (article_number, overview) tuples, one for each article selected by message_spec. overviews(article_number, overview)元组的列表,message_spec选择的每一篇文章对应一个元组。Each overview is a dictionary with the same number of items, but this number depends on the server. 每个overview都是一个条目数量相同的字典,但这个数量取决于服务器。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:NNTP规范保证存在以下项目:

  • the subject, from, date, message-id and references headerssubjectfromdatemessage-idreferences标头

  • the :bytes metadata: the number of bytes in the entire raw article (including headers and body):bytes节元数据:整个原始文章中的字节数(包括标题和正文)

  • the :lines metadata: the number of lines in the article body:lines元数据:文章正文中的行数

The value of each item is either a string, or None if not present.每个项的值为字符串,如果不存在,则为None

It is advisable to use the decode_header() function on header values when they may contain non-ASCII characters:当标头值可能包含非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>'

New in version 3.2.版本3.2中新增。

NNTP.help(*, file=None)

Send a HELP command. 发送HELP命令。Return a pair (response, list) where list is a list of help strings.返回一对(response, list),其中列表是帮助字符串的列表。

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. 发送STAT命令,其中message_spec是消息id(包含在'<''>'中)或当前组中的文章编号。If message_spec is omitted or None, the current article in the current group is considered. 如果忽略message_specNone,则考虑当前组中的当前文章。Return a triple (response, number, id) where number is the article number and id is the message id.返回一个三元组(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()

Send a NEXT command. 发送NEXT命令。Return as for stat().返回stat()

NNTP.last()

Send a LAST command. Return as for stat().发送LAST命令。返回stat()

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

Send an ARTICLE command, where message_spec has the same meaning as for stat(). 发送一条ARTICLE命令,其中message_specstat()的含义相同。Return a tuple (response, info) where info is a namedtuple with three attributes number, message_id and lines (in that order). 返回一个元组(response, info),其中info是一个具有三个属性numbermessage_idlines(按顺序)的namedtuplenumber is the article number in the group (or 0 if the information is not available), 是组中的物品编号(如果信息不可用,则为0),message_id the message id as a string, and 是作为字符串的消息id,而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. article()相同,但发送HEAD命令。The lines returned (or written to file) will only contain the message headers, not the body.返回(或写入file)的lines将只包含消息头,而不包含正文。

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

Same as article(), but sends a BODY command. article()相同,但发送BODY命令。The lines returned (or written to file) will only contain the message body, not the headers.返回(或写入file)的lines将只包含消息正文,而不包含标头。

NNTP.post(data)

Post an article using the POST command. 使用POST命令发布文章。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). data参数要么是为二进制读取而打开的文件对象,要么是任何可迭代的字节对象(表示要发布的文章的原始行)。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.post()方法自动转义以.开头的行,并附加终止线。

If the method succeeds, the server’s response is returned. 如果该方法成功,则返回服务器的响应。If the server refuses posting, a NNTPReplyError is raised.如果服务器拒绝发布,将引发NNTPReplyError

NNTP.ihave(message_id, data)

Send an IHAVE command. 发送IHAVE命令。message_id is the id of the message to send to the server (enclosed in '<' and '>'). message_id是要发送到服务器的消息的id(包含在'<''>'中)。The data parameter and the return value are the same as for post().data参数和返回值与post()相同。

NNTP.date()

Return a pair (response, date). 返回一对(response, date)date is a datetime object containing the current date and time of the server.date是包含服务器当前日期和时间的datetime对象。

NNTP.slave()

Send a SLAVE command. Return the server’s response.发送SLAVE命令。返回服务器的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. 默认值0不生成调试输出。A value of 1 produces a moderate amount of debugging output, generally a single line per request or response. 1产生适度的调试输出,通常每个请求或响应一行。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).2或更高会产生最大的调试输出量,记录连接上发送和接收的每一行(包括消息文本)。

The following are optional NNTP extensions defined in RFC 2980. 以下是RFC 2980中定义的可选NNTP扩展。Some of them have been superseded by newer commands in RFC 3977.其中一些已被RFC 3977中的新命令所取代。

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

Send an XHDR command. 发送XHDR命令。The hdr argument is a header keyword, e.g. 'subject'. hdr参数是一个标头关键字,例如'subject'The str argument should have the form 'first-last' where first and last are the first and last article numbers to search. str参数的格式应为'first-last',其中firstlast是要搜索的第一个和最后一个文章编号。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. 返回一对(response, list),其中list是一对(id, text)的列表,其中id是一个文章编号(作为字符串),text是该文章请求的标题的文本。If the file parameter is supplied, then the output of the XHDR command is stored in a file. 如果提供了file参数,则XHDR命令的输出将存储在文件中。If file is a string, then the method will open a file with that name, write to it then close it. 如果file是一个字符串,那么该方法将打开一个具有该名称的文件,写入该文件,然后关闭它。If file is a file object, then it will start calling write() on it to store the lines of the command output. 如果file文件对象,那么它将开始对其调用write()来存储命令输出的行。If file is supplied, then the returned list is an empty list.如果提供了file,则返回的list为空列表。

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

Send an XOVER command. 发送XOVER命令。start and end are article numbers delimiting the range of articles to select. startend是限定要选择的文章范围的文章编号。The return value is the same of for over(). 返回值与over()的返回值相同。It is recommended to use over() instead, since it will automatically use the newer OVER command if available.建议改用over(),因为如果可用,它将自动使用较新的OVER命令。

Utility functions实用程序功能

The module also defines the following utility function:该模块还定义了以下实用功能:

nntplib.decode_header(header_str)

Decode a header value, un-escaping any escaped non-ASCII characters. 解码标头值,取消转义任何转义的非ASCII字符。header_str must be a str object. 必须是str对象。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'