Python筆記 之 使用nntp模組閱讀網路新聞
阿新 • • 發佈:2021-01-28
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 <[email protected].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?