O'Reilly logo

Python Programming On Win32 by Mark Hammond, Andy Robinson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required