Listing the Newsgroups on a Server

This lab demonstrates how to use the module to perform one of the most common tasks in Usenet: retrieving a list of the available newsgroups on a server.

9.1.1. How Do I Do That?

Use a subclass of as your Protocol. NNTPClient is one of the older protocol implementations in Twisted, and it's starting to show its age. It was developed before the best practices for writing Twisted client protocols had really been understood. In particular, you'll notice that it doesn't make use of Deferreds. Instead, NNTPClient has an API that provides methods for sending commands to the server, and you write handler methods for receiving the server's reply.

To get the list of newsgroups from the server, call the fetchGroups method of NNTPClient. Use the gotAllGroups method as shown in Example 9-1 to handle the result, and getAllGroupsFailed to handle an error.

Example 9-1.

from import nntp

from twisted.internet import protocol, defer

class NNTPGroupListerProtocol(nntp.NNTPClient):

 def connectionMade(self):


 self.fetchGroups( )

 def gotAllGroups(self, groups):

 groupnames = [groupInfo[0] for groupInfo in groups]


 self.quit( )

 def getAllGroupsFailed(self, error):


 def connectionLost(self, error):

 if not self.factory.deferred.called:


class NNTPGroupListerFactory(protocol.ClientFactory):

 protocol = NNTPGroupListerProtocol

 def _ _init_ _(self):

 self.deferred = defer.Deferred( )

if __name__ == "_ _main_ _":

 from twisted.internet import reactor

 def printGroups(groups):

 for group in groups:

 print group

 reactor.stop( )

 def handleError(error):

 print >> sys.stderr, error.getErrorMessage( )

 reactor.stop( )

 factory = NNTPGroupListerFactory( )


 reactor.connectTCP('', 119, factory) )

Run with the hostname of an NNTP server as the only argument. It will download and print the list of available newsgroups:

 $ python










9.1.2. How Does That Work?

NNTP doesn't require authentication, so NNTPGroupListerProtocol only has to call one method: self.fetchGroups( ). When the server responds with a list of newsgroups, the gotAllGroups method is called, with the newsgroup list as an argument. This list is a set of tuples in the form (groupName, articleCount). NNTPGroupListerProtocol's gotAllGroups method extracts the group names and uses them to call back the Factory's Deferred object.

The getAllGroupsFailed method is the error handler for fetchGroups. If an error occurs, it will be passed to the factory's Deferred.

Getting Started

Building Simple Clients and Servers

Web Clients

Web Servers

Web Services and RPC


Mail Clients

Mail Servers

NNTP Clients and Servers


Services, Processes, and Logging

show all menu

Twisted Network Programming Essentials
Twisted Network Programming Essentials
ISBN: 0596100329
EAN: 2147483647
Year: 2004
Pages: 107
Authors: Abe Fettig
Similar book on Amazon © 2008-2017.
If you may any questions please contact us: