Acesso ao protocolo NNTP e leitura de notícias.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 """
5 python news.py [server] [newsgroup]
6
7 [server] : Server NNTP
8 [newsgroup] : Newsgroup of the server
9
10 Run: python news.py news.gmane.org gmane.comp.python.announce
11 """
12
13 from nntplib import *
14 from rfc822 import *
15 from string import *
16 from StringIO import *
17 from sys import *
18
19 SERVER = argv[1]
20 NEWSGROUP = argv[2]
21 server = NNTP(SERVER)
22 resp, count, first, last, name = server.group(NEWSGROUP)
23
24 print 'Group', name, 'has', count, 'articles, range', first, 'to', last
25 #ls = input('Quantos títulos deseja mostrar na tela: ')
26 print 'Please wait...'
27 resp, subs = server.xhdr('subject', first + '-' + last)
28
29 # TODO:
30 #li = ('-%s:' % ls)
31 #for id, sub in subs[li]:
32 for id, sub in subs[-10:]:
33 print id, sub
34
35 print 'Group', name, 'has', count, 'articles, range', first, 'to', last
36 id = input('Message id: ')
37 resp, id, message_id, text = server.article(str(id))
38 text = join(text, "\n")
39 file = StringIO(text)
40 message = Message(file)
41
42 headers = ['xref', 'from', 'date', 'subject']
43 for k in headers:
44 print k, '=', message[k]
45
46 # TODO: suporte a unicode
47 print message.fp.read()
Volta para CookBook.