20.11. nntplib — NNTP 协议 Client 端

源代码: Lib/nntplib.py


该模块定义了实现 NNTP 协议 Client 端的NNTP类。它可以用于实现新闻阅读器或海报,或自动新闻处理程序。有关 NNTP(网络新闻传输协议)的更多信息,请参见 Internet RFC 977

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

>>> s = NNTP('news.gmane.org')
>>> 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 1071 articles, range 1 to 1071
>>> resp, subs = s.xhdr('subject', first + '-' + last)
>>> for id, sub in subs[-10:]: print id, sub
...
1062 Re: Mercurial Status?
1063 Re: [python-committers]  (Windows) buildbots on 3.x
1064 Re: Mercurial Status?
1065 Re: Mercurial Status?
1066 Python 2.6.6 status
1067 Commit Privileges for Ask Solem
1068 Re: Commit Privileges for Ask Solem
1069 Re: Commit Privileges for Ask Solem
1070 Re: Commit Privileges for Ask Solem
1071 2.6.6 rc 2
>>> s.quit()
'205 Bye!'

要发布文件中的文章(假定文章具有有效的标题,并且您有权在特定的新闻组中发布文章):

>>> s = NNTP('news.gmane.org')
>>> f = open('articlefile')
>>> s.post(f)
'240 Article posted successfully.'
>>> s.quit()
'205 Bye!'

该模块本身定义了以下各项:

在版本 2.4 中更改:添加了* usenetrc *参数。

20.11.1. NNTP 对象

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

2.4 版的新Function。

这消除了服务器的响应代码。如果需要响应代码,请使用descriptions()

2.4 版的新Function。

RFC2980 说“建议不要使用此 extensions”。请改用descriptions()description()

首页