HOW AND WHY NNTP IS USED?
nntplib uses NNTP class which implements the client side of NNTP (Network News Transfer Protocol). Generally NNTP protocol is used to transfer news articles, posters, etc.. among usenet servers which is a distributed discussion system for sharing articles and posts. NNTP closely resembles SMTP (Simple Mail Transfer Protocol) which is used by the mail service providers for sending and receiving mails. The only thing that differentiates from NNTP from SMTP is NNTP is designed for transferring news articles and SMTP is used for transferring mails.
The useful functionality of the NNTP is that it allows both server-to-server communication and client-to-server communication. That in NNTP servers, A server can react to other servers in all means that it can communicate with that server and even the clients connected to it can make queries in it with certain commands that are provided to them in the client-server model.
The following are the commands which can be used in the NNTP:
1. ARTICLE - This command is used to get an article from the currently used usenet server.
2. GROUP - This command is used to select a specific neews group from the Usenet server.
3. IHAVE - This server is used by the client, which tells the server that the client had the particular article that the server is searching for.
4. LIST - This lists all the news groups which are present in the current usenet server.
5. NEWSGROUPS - This command is used same as the LIST command but takes few parameters so that it can recieve a list from a particular date to another date.
6. NEXT - This is used to go to the next message in the current newsgroup.
7. POST - This is used to post a new message in the newsgroup.
This commands are read and understand by the newreaders software and used on the Usenet Servers.
Here is a simple program to demonstrate how we use nntplib in python.
Here in the program, We first importing the NNTP class from the nntplib module. later in the second line we can see a with statement, it is used to make custom names for the classes, objects and functions. Here we are using it for the NNTP('news.gmane.io') we customized with the name as a simple 'n'. NNTP class constructor takes 7 parameters. They are host, port, user, password, readermode, usenetrc, and timeout which is optional. Here we are only passing host name to the class constructor and it returns a NNTP object back. Even the parameters are having certain default values which are passed to the constructor if they are not mentioned at the time of calling. They are port with value 119, user, readermode and password with values 'none', usenetrc with value false. In the next line we are printing the returned tuple which is from the group method. It takes a simple parameter name which is the group name of the the certain group we want to visit. The returned tuple from the group name contains 5 values in it. They are response, count, first, last, name. Where response is the reaction we got from the server, count is the estimated count of the articles in the group, first is the first article number in the group, last is the last article number in the group, name is same as the name of the group.
The output of the above program is.
As we can see the response from the server is '211 7150 1 7151 gmane.comp.python.committers', the total number of articles in the group are 7150 which is count, the first article number is 1 and last article number is 7151, and the complete name of the group is printed 'gmane.comp.python.committers'.