Python代码
登录 news.gmane.io,查看新闻组 gmane.comp.python.committers 前十条信息,每次按下 y 键依次浏览题头和内容。
import nntplib
#默认设置
defaultsite = 'news.gmane.io'
defaultgroup = 'gmane.comp.python.committers'
defaultcount = 10
showhdrs = ['Path','From','Newsgroups','Subject','Date','Lines']
def reader(site=defaultsite,group=defaultgroup,newscount=defaultcount):
#打印网站相关信息
print('Connectiong to ',site,' for ',group)
server = nntplib.NNTP(site)
print('Version: ',server.nntp_version)
print('Implementation: ',server.nntp_implementation)
print(server.getwelcome())
#获取新闻组信息
(replay,count,first,last,name) = server.group(group)
print('%s has %s articles,from %s to %s .'%(name,count,first,last))
#请求题头
fecthfrom = str(first)+'-'+str(newscount-1)
(replay,subjects) = server.xhdr('subject',(fecthfrom))
print('Reply: ',replay,'\n',
'Subjects: ',subjects)
#显示题头,获取消息的题头和主体
for (id,subj) in subjects:
print('Article %s [%s]'%(id,subj))
if input('Display?') in ('y','Y'):
#print(server.head(id))
#hand返回响应信息和文章信息
#需要两次解读文件内容 lines
#lines 为byte类型,需要解码
(replay,articleinfo) = server.head(id)
(num,id,lines) = articleinfo
for line in lines:
line = line.decode('utf-8')
for prefix in showhdrs:
if line[:len(prefix)] == prefix:
print(line)
if input('Show body?') in ('y','Y'):
#print(server.body(id))
# body返回响应信息和文章内容
# 需要两次解读文件内容 lines
# lines 为byte类型,需要解码
(replay,articleinfo)=server.body(id)
(num, id, lines) = articleinfo
for line in lines:
print(line.decode('utf-8'))
print(server.quit())
if __name__ == '__main__':
reader()
运行结果
Connectiong to news.gmane.io for gmane.comp.python.committers
Version: 2
Implementation: INN 2.6.3
200 news.gmane.io InterNetNews NNRP server INN 2.6.3 ready (posting ok)
gmane.comp.python.committers has 7150 articles,from 1 to 7151 .
Reply: 221 Header or metadata information for subject follows (from overview)
Subjects: [('1', 'Nominate Hirokazu Yamamoto (oceancity) for commit privs.'), ('2', 'Re: Nominate Hirokazu Yamamoto (oceancity) for commit privs.'), ('3', 'Re: Nominate Hirokazu Yamamoto (oceancity) for commit privs.'), ('4', 'Re: Nominate Hirokazu Yamamoto (oceancity) for commit privs.'), ('5', 'Re: Nominate Hirokazu Yamamoto (oceancity) for commit privs.'), ('6', 'Re: [Python-Dev] next beta'), ('7', 'Re: [Python-Dev] next beta'), ('8', 'Re: [Python-Dev] next beta'), ('9', 'Re: Nominate Hirokazu Yamamoto (oceancity) for commit privs.')]
Article 1 [Nominate Hirokazu Yamamoto (oceancity) for commit privs.]
Display?y
Path: news.gmane.org!not-for-mail
From: Trent Nelson <tnelson@onresolve.com>
Newsgroups: gmane.comp.python.committers
Subject: Nominate Hirokazu Yamamoto (oceancity) for
Date: Mon, 11 Aug 2008 20:27:36 +0100
Lines: 5
Show body?y
What do people think about making Hirokazu Yamamoto a committer? I rarely see any mailing list posts from him, but he sure makes up for it in terms of patches submitted to the issue tracker. As far as I can tell (I've noticed his regular involvement via patches and whatnot for well over a year), he's a pretty switched on guy with a lot of Windows experience -- every time trunk fails to build with vc6/7/8, for example, he's got patches lined up.
Trent.
Article 2 [Re: Nominate Hirokazu Yamamoto (oceancity) for commit privs.]
Display?