:mod:`nntplib` --- NNTP protocol client
Source code: :source:`Lib/nntplib.py`
This module defines the class :class:`NNTP` which implements the client side of the Network News Transfer Protocol. It can be used to implement a news reader or poster, or automated news processors. It is compatible with RFC 3977 as well as the older RFC 977 and RFC 2980.
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 = nntplib.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 1096 articles, range 1 to 1096 >>> resp, overviews = s.over((last - 9, last)) >>> for id, over in overviews: ... print(id, nntplib.decode_header(over['subject'])) ... 1087 Re: Commit privileges for Łukasz Langa 1088 Re: 3.2 alpha 2 freeze 1089 Re: 3.2 alpha 2 freeze 1090 Re: Commit privileges for Łukasz Langa 1091 Re: Commit privileges for Łukasz Langa 1092 Updated ssh key 1093 Re: Updated ssh key 1094 Re: Updated ssh key 1095 Hello fellow committers! 1096 Re: Hello fellow committers! >>> s.quit() '205 Bye!'
To post an article from a binary file (this assumes that the article has valid headers, and that you have right to post on the particular newsgroup):
>>> s = nntplib.NNTP('news.gmane.org') >>> f = open('/tmp/article.txt', 'rb') >>> s.post(f) '240 Article posted successfully.' >>> s.quit() '205 Bye!'
The module itself defines the following classes:
Return a new :class:`NNTP` object, representing a connection to the NNTP server running on host host, listening at port port. An optional timeout can be specified for the socket connection. If the optional user and password are provided, or if suitable credentials are present in :file:`/.netrc` and the optional flag usenetrc is true, the AUTHINFO USER and AUTHINFO PASS commands are used to identify and authenticate the user to the server. If the optional flag readermode is true, then a mode reader command is sent before authentication is performed. Reader mode is sometimes necessary if you are connecting to an NNTP server on the local machine and intend to call reader-specific commands, such as group. If you get unexpected :exc:`NNTPPermanentError`s, you might need to set readermode. :class:`NNTP` class supports the :keyword:`with` statement to unconditionally consume :exc:`OSError` exceptions and to close the NNTP connection when done. Here is a sample on how using it:
>>> from nntplib import NNTP >>> with NNTP('news.gmane.org') as n: ... n.group('gmane.comp.python.committers') ... ('211 1755 1 1755 gmane.comp.python.committers', 1755, 1, 1755, 'gmane.comp.python.committers') >>>
Return a new :class:`NNTP_SSL` object, representing an encrypted connection to the NNTP server running on host host, listening at port port. :class:`NNTP_SSL` objects have the same methods as :class:`NNTP` objects. If port is omitted, port 563 (NNTPS) is used. ssl_context is also optional, and is a :class:`~ssl.SSLContext` object. All other parameters behave the same as for :class:`NNTP`.
Note that SSL-on-563 is discouraged per RFC 4642, in favor of STARTTLS as described below. However, some servers only support the former.
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.
Many of the following methods take an optional keyword-only argument file. When the file argument is supplied, it must be either a :term:`file object` opened for binary writing, or the name of an on-disk file to be written to. The method will then write any data returned by the server (except for the response line and the terminating dot) to the file; any list of lines, tuples or objects that the method normally returns will be empty.
The module also defines the following utility function: