Fetching News Articles via NNTP
It should come as no surprise that a
Python module nntplib
supports the NNTP protocol. Following
the style of the other Internet-related modules, a single class
NNTP implements all functionality.
The NNTP protocol supports a wide variety of commands for determining
which articles exist on the server computer. Information on these
commands is beyond the scope of this book; you should refer to the
NNTP protocol standard or the nntplib module
itself for further information.
However, to whet your appetite, let’s create a sample program that scans a newsgroup for a list of articles with a specific word in their subject. It generates an HTML file, then fires your browser with the news articles hyperlinked:
# SimpleNewsViewer.py # Finds all news articles in a news group that have a specific word # in its subject. Then writes the results to a HTML file for # easy reading. # eg, running: # c:\> SimpleNewsViewer.py comp.lang.python python # # Will generate "comp.lang.python.html", and execute your # browser on this file. import sys, string import nntplib import win32api # to execute our browser. g_newsserver = 'news-server.c3.telstra-mm.net.au' def MakeNewsPage(groupname, subjectsearch, outfile ): print "Connecting..." nntp=nntplib.NNTP(g_newsserver) print "Fetching group information" # Most functions return the raw server response first. resp, numarts, first, last, name = nntp.group(groupname) # Get the subject line from these messages. print "Getting article ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access