Running a Basic NNTP Server

The twisted.news package includes classes for building NNTP servers. The server architecture is designed in two layers: the Factory and Protocol, which communicate with clients; and the news storage interface, which is responsible for reading and storing articles. twisted.news comes with working examples of the news storage interface, which you can use to create a functional NNTP server in just a few lines of code.

9.4.1. How Do I Do That?

Create a database.NewsShelf object, which implements the database.INewsStorage interface. Pass the NewsShelf object to a news.NNTPFactory object, and listen on port 119. As Example 9-4 shows, this is very easy to do.

Example 9-4. tinynntpserver.py


from twisted.internet import reactor

from twisted.news import database, news, nntp



GROUPS = [

 'local.programming',

 'local.programming.python',

 'local.knitting',

 ]

SMTP_SERVER = 'upstream-server.com'

STORAGE_DIR = 'storage'



newsStorage = database.NewsShelf(SMTP_SERVER, STORAGE_DIR)

for group in GROUPS:

 newsStorage.addGroup(group, [])

factory = news.NNTPFactory(newsStorage)

reactor.listenTCP(119, factory)

reactor.run( )

Run this example from the command line, without any arguments:


 $ python tinynntpserver.py

Then connect to localhost using a newsreader application. You should be able to see the list of available newsgroups, as in Figure 9-1.

Figure 9-1. Browsing the list of newsgroups

Subscribe to the newsgroups, and you should be able to read and post articles. If you connect with multiple clients, you'll be able to start discussions and reply to each other's articles, as shown in Figure 9-2.

Figure 9-2. Reading and posting messages

 

9.4.2. How Does That Work?

The database.NewsShelf class is an implementation of database.INewsStorage that stores articles using a twisted.persisted.dirdbm.Shelf. dirdbm.Shelf is a useful class that works like a dictionary, but writes its data to a directory so that it can persist after the program exists. The NewsShelf class is initialized with two arguments: the hostname of an SMTP server (which it will use for sending notification messages to the server administrator) and the directory where it should store article data.

Once you've created a NewsShelf object, all you have to do is pass it to a news.NNTPFactory object. This factory uses nntp.NNTPServer for a protocol, which is designed to work with any implementation of database.INewsStorage.

Getting Started

Building Simple Clients and Servers

Web Clients

Web Servers

Web Services and RPC

Authentication

Mail Clients

Mail Servers

NNTP Clients and Servers

SSH

Services, Processes, and Logging



Twisted Network Programming Essentials
Twisted Network Programming Essentials
ISBN: 0596100329
EAN: 2147483647
Year: 2004
Pages: 107
Authors: Abe Fettig

Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net