This module defines the class NNTP which implements the client side of the NNTP protocol. It can be used to implement a news reader or poster, or automated news processors. For more information on NNTP (Network News Transfer Protocol), see Internet RFC 977.
Here are two small examples of how it can be used. To list some statistics about a newsgroup and print the subjects of the last 10 articles:
>>> s = NNTP('news.gmane.org') >>> resp, count, first, last, name = s.group('gmane.comp.python.committers') >>> print('Group', name, 'has', count, 'articles, range', first, 'to', last) Group gmane.comp.python.committers has 1071 articles, range 1 to 1071 >>> resp, subs = s.xhdr('subject', first + '-' + last) >>> for id, sub in subs[-10:]: print(id, sub) ... 1062 Re: Mercurial Status? 1063 Re: [python-committers] (Windows) buildbots on 3.x 1064 Re: Mercurial Status? 1065 Re: Mercurial Status? 1066 Python 2.6.6 status 1067 Commit Privileges for Ask Solem 1068 Re: Commit Privileges for Ask Solem 1069 Re: Commit Privileges for Ask Solem 1070 Re: Commit Privileges for Ask Solem 1071 2.6.6 rc 2 >>> s.quit() '205 Bye!'
To post an article from a file (this assumes that the article has valid headers, and that you have right to post on the particular newsgroup):
>>> s = NNTP('news.gmane.org') >>> f = open('/tmp/article') >>> s.post(f) '240 Article posted successfully.' >>> s.quit() '205 Bye!'
The module itself defines the following items:
NNTP instances have the following methods. The response that is returned as the first item in the return tuple of almost all methods is the server’s response: a string beginning with a three-digit code. If the server’s response indicates an error, the method raises one of the above exceptions.
Get a description for a single group group. If more than one group matches (if ‘group’ is a real wildmat string), return the first match. If no group matches, return an empty string.
This elides the response code from the server. If the response code is needed, use descriptions().
Process an XGTITLE command, returning a pair (response, list), where list is a list of tuples containing (name, title). If the file parameter is supplied, then the output of the XGTITLE command is stored in a file. If file is a string, then the method will open a file with that name, write to it then close it. If file is a file object, then it will start calling write() on it to store the lines of the command output. If file is supplied, then the returned list is an empty list. This is an optional NNTP extension, and may not be supported by all servers.
RFC2980 says “It is suggested that this extension be deprecated”. Use descriptions() or description() instead.
20.13. imaplib — IMAP4 protocol client
20.15. smtplib — SMTP protocol client
Enter search terms or a module, class or function name.