blob: 3943f2c249dd18fb8840d927913b7e6b6a0443cf [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`nntplib` --- NNTP protocol client
2=======================================
3
4.. module:: nntplib
5 :synopsis: NNTP protocol client (requires sockets).
6
7
8.. index::
9 pair: NNTP; protocol
10 single: Network News Transfer Protocol
11
Raymond Hettinger469271d2011-01-27 20:38:46 +000012**Source code:** :source:`Lib/nntplib.py`
13
14--------------
15
Georg Brandl116aa622007-08-15 14:28:22 +000016This module defines the class :class:`NNTP` which implements the client side of
Antoine Pitrou69ab9512010-09-29 15:03:40 +000017the Network News Transfer Protocol. It can be used to implement a news reader
18or poster, or automated news processors. It is compatible with :rfc:`3977`
19as well as the older :rfc:`977` and :rfc:`2980`.
Georg Brandl116aa622007-08-15 14:28:22 +000020
21Here are two small examples of how it can be used. To list some statistics
22about a newsgroup and print the subjects of the last 10 articles::
23
Antoine Pitrou69ab9512010-09-29 15:03:40 +000024 >>> s = nntplib.NNTP('news.gmane.org')
Antoine Pitroue3396512010-09-07 18:44:12 +000025 >>> resp, count, first, last, name = s.group('gmane.comp.python.committers')
Georg Brandl6911e3c2007-09-04 07:15:32 +000026 >>> print('Group', name, 'has', count, 'articles, range', first, 'to', last)
Antoine Pitrou69ab9512010-09-29 15:03:40 +000027 Group gmane.comp.python.committers has 1096 articles, range 1 to 1096
28 >>> resp, overviews = s.over((last - 9, last))
29 >>> for id, over in overviews:
30 ... print(id, nntplib.decode_header(over['subject']))
Georg Brandl48310cd2009-01-03 21:18:54 +000031 ...
Antoine Pitrou69ab9512010-09-29 15:03:40 +000032 1087 Re: Commit privileges for Łukasz Langa
33 1088 Re: 3.2 alpha 2 freeze
34 1089 Re: 3.2 alpha 2 freeze
35 1090 Re: Commit privileges for Łukasz Langa
36 1091 Re: Commit privileges for Łukasz Langa
37 1092 Updated ssh key
38 1093 Re: Updated ssh key
39 1094 Re: Updated ssh key
40 1095 Hello fellow committers!
41 1096 Re: Hello fellow committers!
Georg Brandl116aa622007-08-15 14:28:22 +000042 >>> s.quit()
Antoine Pitroue3396512010-09-07 18:44:12 +000043 '205 Bye!'
Georg Brandl116aa622007-08-15 14:28:22 +000044
Antoine Pitrou69ab9512010-09-29 15:03:40 +000045To post an article from a binary file (this assumes that the article has valid
Antoine Pitroue3396512010-09-07 18:44:12 +000046headers, and that you have right to post on the particular newsgroup)::
Georg Brandl116aa622007-08-15 14:28:22 +000047
Antoine Pitrou69ab9512010-09-29 15:03:40 +000048 >>> s = nntplib.NNTP('news.gmane.org')
Petri Lehtinen9f74c6c2013-02-23 19:26:56 +010049 >>> f = open('article.txt', 'rb')
Georg Brandl116aa622007-08-15 14:28:22 +000050 >>> s.post(f)
51 '240 Article posted successfully.'
52 >>> s.quit()
Antoine Pitroue3396512010-09-07 18:44:12 +000053 '205 Bye!'
Georg Brandl116aa622007-08-15 14:28:22 +000054
Antoine Pitrou69ab9512010-09-29 15:03:40 +000055The module itself defines the following classes:
Georg Brandl116aa622007-08-15 14:28:22 +000056
57
Antoine Pitrou859c4ef2010-11-09 18:58:42 +000058.. class:: NNTP(host, port=119, user=None, password=None, readermode=None, usenetrc=False, [timeout])
Georg Brandl116aa622007-08-15 14:28:22 +000059
Antoine Pitroua0781152010-11-05 19:16:37 +000060 Return a new :class:`NNTP` object, representing a connection
Antoine Pitrou69ab9512010-09-29 15:03:40 +000061 to the NNTP server running on host *host*, listening at port *port*.
62 An optional *timeout* can be specified for the socket connection.
63 If the optional *user* and *password* are provided, or if suitable
64 credentials are present in :file:`/.netrc` and the optional flag *usenetrc*
Antoine Pitrou859c4ef2010-11-09 18:58:42 +000065 is true, the ``AUTHINFO USER`` and ``AUTHINFO PASS`` commands are used
66 to identify and authenticate the user to the server. If the optional
Antoine Pitrou69ab9512010-09-29 15:03:40 +000067 flag *readermode* is true, then a ``mode reader`` command is sent before
68 authentication is performed. Reader mode is sometimes necessary if you are
69 connecting to an NNTP server on the local machine and intend to call
70 reader-specific commands, such as ``group``. If you get unexpected
Georg Brandl116aa622007-08-15 14:28:22 +000071 :exc:`NNTPPermanentError`\ s, you might need to set *readermode*.
Giampaolo Rodolà424298a2011-03-03 18:34:06 +000072 :class:`NNTP` class supports the :keyword:`with` statement to
Andrew Svetlov0832af62012-12-18 23:10:48 +020073 unconditionally consume :exc:`OSError` exceptions and to close the NNTP
Giampaolo Rodolà424298a2011-03-03 18:34:06 +000074 connection when done. Here is a sample on how using it:
75
76 >>> from nntplib import NNTP
Ezio Melotti04f648c2011-07-26 09:37:46 +030077 >>> with NNTP('news.gmane.org') as n:
Giampaolo Rodolà424298a2011-03-03 18:34:06 +000078 ... n.group('gmane.comp.python.committers')
79 ...
Ezio Melotti04f648c2011-07-26 09:37:46 +030080 ('211 1755 1 1755 gmane.comp.python.committers', 1755, 1, 1755, 'gmane.comp.python.committers')
Giampaolo Rodolà424298a2011-03-03 18:34:06 +000081 >>>
82
Antoine Pitrou859c4ef2010-11-09 18:58:42 +000083
84 .. versionchanged:: 3.2
Serhiy Storchakafbc1c262013-11-29 12:17:13 +020085 *usenetrc* is now ``False`` by default.
Georg Brandl116aa622007-08-15 14:28:22 +000086
Giampaolo Rodolà424298a2011-03-03 18:34:06 +000087 .. versionchanged:: 3.3
88 Support for the :keyword:`with` statement was added.
Georg Brandl116aa622007-08-15 14:28:22 +000089
Antoine Pitrou859c4ef2010-11-09 18:58:42 +000090.. class:: NNTP_SSL(host, port=563, user=None, password=None, ssl_context=None, readermode=None, usenetrc=False, [timeout])
Antoine Pitrou1cb121e2010-11-09 18:54:37 +000091
92 Return a new :class:`NNTP_SSL` object, representing an encrypted
93 connection to the NNTP server running on host *host*, listening at
94 port *port*. :class:`NNTP_SSL` objects have the same methods as
95 :class:`NNTP` objects. If *port* is omitted, port 563 (NNTPS) is used.
96 *ssl_context* is also optional, and is a :class:`~ssl.SSLContext` object.
Antoine Pitrouc5e075f2014-03-22 18:19:11 +010097 Please read :ref:`ssl-security` for best practices.
Antoine Pitrou1cb121e2010-11-09 18:54:37 +000098 All other parameters behave the same as for :class:`NNTP`.
99
100 Note that SSL-on-563 is discouraged per :rfc:`4642`, in favor of
101 STARTTLS as described below. However, some servers only support the
102 former.
103
104 .. versionadded:: 3.2
105
Christian Heimes216d4632013-12-02 20:20:11 +0100106 .. versionchanged:: 3.4
107 The class now supports hostname check with
Antoine Pitrouc5e075f2014-03-22 18:19:11 +0100108 :attr:`ssl.SSLContext.check_hostname` and *Server Name Indication* (see
109 :data:`ssl.HAS_SNI`).
Antoine Pitrou1cb121e2010-11-09 18:54:37 +0000110
Georg Brandl116aa622007-08-15 14:28:22 +0000111.. exception:: NNTPError
112
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000113 Derived from the standard exception :exc:`Exception`, this is the base
114 class for all exceptions raised by the :mod:`nntplib` module. Instances
115 of this class have the following attribute:
116
117 .. attribute:: response
118
119 The response of the server if available, as a :class:`str` object.
Georg Brandl116aa622007-08-15 14:28:22 +0000120
121
122.. exception:: NNTPReplyError
123
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000124 Exception raised when an unexpected reply is received from the server.
Georg Brandl116aa622007-08-15 14:28:22 +0000125
126
127.. exception:: NNTPTemporaryError
128
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000129 Exception raised when a response code in the range 400--499 is received.
Georg Brandl116aa622007-08-15 14:28:22 +0000130
131
132.. exception:: NNTPPermanentError
133
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000134 Exception raised when a response code in the range 500--599 is received.
Georg Brandl116aa622007-08-15 14:28:22 +0000135
136
137.. exception:: NNTPProtocolError
138
139 Exception raised when a reply is received from the server that does not begin
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000140 with a digit in the range 1--5.
Georg Brandl116aa622007-08-15 14:28:22 +0000141
142
143.. exception:: NNTPDataError
144
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000145 Exception raised when there is some error in the response data.
Georg Brandl116aa622007-08-15 14:28:22 +0000146
147
148.. _nntp-objects:
149
150NNTP Objects
151------------
152
Antoine Pitrou1cb121e2010-11-09 18:54:37 +0000153When connected, :class:`NNTP` and :class:`NNTP_SSL` objects support the
154following methods and attributes.
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000155
Antoine Pitroua0781152010-11-05 19:16:37 +0000156Attributes
157^^^^^^^^^^
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000158
Antoine Pitroua0781152010-11-05 19:16:37 +0000159.. attribute:: NNTP.nntp_version
160
161 An integer representing the version of the NNTP protocol supported by the
162 server. In practice, this should be ``2`` for servers advertising
163 :rfc:`3977` compliance and ``1`` for others.
164
165 .. versionadded:: 3.2
166
167.. attribute:: NNTP.nntp_implementation
168
169 A string describing the software name and version of the NNTP server,
170 or :const:`None` if not advertised by the server.
171
172 .. versionadded:: 3.2
173
174Methods
175^^^^^^^
176
177The *response* that is returned as the first item in the return tuple of almost
178all methods is the server's response: a string beginning with a three-digit
179code. If the server's response indicates an error, the method raises one of
180the above exceptions.
181
182Many of the following methods take an optional keyword-only argument *file*.
183When the *file* argument is supplied, it must be either a :term:`file object`
184opened for binary writing, or the name of an on-disk file to be written to.
185The method will then write any data returned by the server (except for the
186response line and the terminating dot) to the file; any list of lines,
187tuples or objects that the method normally returns will be empty.
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000188
189.. versionchanged:: 3.2
190 Many of the following methods have been reworked and fixed, which makes
191 them incompatible with their 3.1 counterparts.
192
193
194.. method:: NNTP.quit()
195
196 Send a ``QUIT`` command and close the connection. Once this method has been
197 called, no other methods of the NNTP object should be called.
Georg Brandl116aa622007-08-15 14:28:22 +0000198
199
200.. method:: NNTP.getwelcome()
201
202 Return the welcome message sent by the server in reply to the initial
203 connection. (This message sometimes contains disclaimers or help information
204 that may be relevant to the user.)
205
206
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000207.. method:: NNTP.getcapabilities()
Georg Brandl116aa622007-08-15 14:28:22 +0000208
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000209 Return the :rfc:`3977` capabilities advertised by the server, as a
210 :class:`dict` instance mapping capability names to (possibly empty) lists
211 of values. On legacy servers which don't understand the ``CAPABILITIES``
212 command, an empty dictionary is returned instead.
213
214 >>> s = NNTP('news.gmane.org')
215 >>> 'POST' in s.getcapabilities()
216 True
217
218 .. versionadded:: 3.2
Georg Brandl116aa622007-08-15 14:28:22 +0000219
220
Antoine Pitrou1cb121e2010-11-09 18:54:37 +0000221.. method:: NNTP.login(user=None, password=None, usenetrc=True)
222
223 Send ``AUTHINFO`` commands with the user name and password. If *user*
Serhiy Storchakafbc1c262013-11-29 12:17:13 +0200224 and *password* are None and *usenetrc* is true, credentials from
Antoine Pitrou1cb121e2010-11-09 18:54:37 +0000225 ``~/.netrc`` will be used if possible.
226
227 Unless intentionally delayed, login is normally performed during the
228 :class:`NNTP` object initialization and separately calling this function
229 is unnecessary. To force authentication to be delayed, you must not set
230 *user* or *password* when creating the object, and must set *usenetrc* to
231 False.
232
233 .. versionadded:: 3.2
234
235
236.. method:: NNTP.starttls(ssl_context=None)
237
Antoine Pitrouc5e075f2014-03-22 18:19:11 +0100238 Send a ``STARTTLS`` command. This will enable encryption on the NNTP
239 connection. The *ssl_context* argument is optional and should be a
240 :class:`ssl.SSLContext` object. Please read :ref:`ssl-security` for best
241 practices.
Antoine Pitrou1cb121e2010-11-09 18:54:37 +0000242
243 Note that this may not be done after authentication information has
244 been transmitted, and authentication occurs by default if possible during a
245 :class:`NNTP` object initialization. See :meth:`NNTP.login` for information
246 on suppressing this behavior.
247
248 .. versionadded:: 3.2
249
Christian Heimes216d4632013-12-02 20:20:11 +0100250 .. versionchanged:: 3.4
251 The method now supports hostname check with
Antoine Pitrouc5e075f2014-03-22 18:19:11 +0100252 :attr:`ssl.SSLContext.check_hostname` and *Server Name Indication* (see
253 :data:`ssl.HAS_SNI`).
Antoine Pitrou1cb121e2010-11-09 18:54:37 +0000254
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000255.. method:: NNTP.newgroups(date, *, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000256
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000257 Send a ``NEWGROUPS`` command. The *date* argument should be a
258 :class:`datetime.date` or :class:`datetime.datetime` object.
259 Return a pair ``(response, groups)`` where *groups* is a list representing
260 the groups that are new since the given *date*. If *file* is supplied,
261 though, then *groups* will be empty.
262
263 >>> from datetime import date, timedelta
264 >>> resp, groups = s.newgroups(date.today() - timedelta(days=3))
265 >>> len(groups)
266 85
267 >>> groups[0]
268 GroupInfo(group='gmane.network.tor.devel', last='4', first='1', flag='m')
Georg Brandl116aa622007-08-15 14:28:22 +0000269
270
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000271.. method:: NNTP.newnews(group, date, *, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000272
273 Send a ``NEWNEWS`` command. Here, *group* is a group name or ``'*'``, and
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000274 *date* has the same meaning as for :meth:`newgroups`. Return a pair
275 ``(response, articles)`` where *articles* is a list of message ids.
276
277 This command is frequently disabled by NNTP server administrators.
Georg Brandl116aa622007-08-15 14:28:22 +0000278
279
Antoine Pitrou08eeada2010-11-04 21:36:15 +0000280.. method:: NNTP.list(group_pattern=None, *, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000281
Antoine Pitrou08eeada2010-11-04 21:36:15 +0000282 Send a ``LIST`` or ``LIST ACTIVE`` command. Return a pair
283 ``(response, list)`` where *list* is a list of tuples representing all
284 the groups available from this NNTP server, optionally matching the
285 pattern string *group_pattern*. Each tuple has the form
286 ``(group, last, first, flag)``, where *group* is a group name, *last*
287 and *first* are the last and first article numbers, and *flag* usually
288 takes one of these values:
Antoine Pitrou7dd1af02010-11-03 18:32:54 +0000289
290 * ``y``: Local postings and articles from peers are allowed.
291 * ``m``: The group is moderated and all postings must be approved.
292 * ``n``: No local postings are allowed, only articles from peers.
293 * ``j``: Articles from peers are filed in the junk group instead.
294 * ``x``: No local postings, and articles from peers are ignored.
295 * ``=foo.bar``: Articles are filed in the ``foo.bar`` group instead.
296
297 If *flag* has another value, then the status of the newsgroup should be
298 considered unknown.
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000299
Antoine Pitrou08eeada2010-11-04 21:36:15 +0000300 This command can return very large results, especially if *group_pattern*
301 is not specified. It is best to cache the results offline unless you
302 really need to refresh them.
303
304 .. versionchanged:: 3.2
305 *group_pattern* was added.
Georg Brandl116aa622007-08-15 14:28:22 +0000306
307
308.. method:: NNTP.descriptions(grouppattern)
309
310 Send a ``LIST NEWSGROUPS`` command, where *grouppattern* is a wildmat string as
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000311 specified in :rfc:`3977` (it's essentially the same as DOS or UNIX shell wildcard
312 strings). Return a pair ``(response, descriptions)``, where *descriptions*
313 is a dictionary mapping group names to textual descriptions.
314
315 >>> resp, descs = s.descriptions('gmane.comp.python.*')
316 >>> len(descs)
317 295
318 >>> descs.popitem()
319 ('gmane.comp.python.bio.general', 'BioPython discussion list (Moderated)')
Georg Brandl116aa622007-08-15 14:28:22 +0000320
Georg Brandl116aa622007-08-15 14:28:22 +0000321
322.. method:: NNTP.description(group)
323
324 Get a description for a single group *group*. If more than one group matches
325 (if 'group' is a real wildmat string), return the first match. If no group
326 matches, return an empty string.
327
328 This elides the response code from the server. If the response code is needed,
329 use :meth:`descriptions`.
330
Georg Brandl116aa622007-08-15 14:28:22 +0000331
332.. method:: NNTP.group(name)
333
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000334 Send a ``GROUP`` command, where *name* is the group name. The group is
335 selected as the current group, if it exists. Return a tuple
336 ``(response, count, first, last, name)`` where *count* is the (estimated)
337 number of articles in the group, *first* is the first article number in
338 the group, *last* is the last article number in the group, and *name*
339 is the group name.
Georg Brandl116aa622007-08-15 14:28:22 +0000340
341
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000342.. method:: NNTP.over(message_spec, *, file=None)
343
344 Send a ``OVER`` command, or a ``XOVER`` command on legacy servers.
345 *message_spec* can be either a string representing a message id, or
346 a ``(first, last)`` tuple of numbers indicating a range of articles in
347 the current group, or a ``(first, None)`` tuple indicating a range of
348 articles starting from *first* to the last article in the current group,
349 or :const:`None` to select the current article in the current group.
350
351 Return a pair ``(response, overviews)``. *overviews* is a list of
352 ``(article_number, overview)`` tuples, one for each article selected
353 by *message_spec*. Each *overview* is a dictionary with the same number
354 of items, but this number depends on the server. These items are either
355 message headers (the key is then the lower-cased header name) or metadata
356 items (the key is then the metadata name prepended with ``":"``). The
357 following items are guaranteed to be present by the NNTP specification:
358
359 * the ``subject``, ``from``, ``date``, ``message-id`` and ``references``
360 headers
361 * the ``:bytes`` metadata: the number of bytes in the entire raw article
362 (including headers and body)
363 * the ``:lines`` metadata: the number of lines in the article body
364
Antoine Pitrou4103bc02010-11-03 18:18:43 +0000365 The value of each item is either a string, or :const:`None` if not present.
366
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000367 It is advisable to use the :func:`decode_header` function on header
368 values when they may contain non-ASCII characters::
369
370 >>> _, _, first, last, _ = s.group('gmane.comp.python.devel')
371 >>> resp, overviews = s.over((last, last))
372 >>> art_num, over = overviews[0]
373 >>> art_num
374 117216
375 >>> list(over.keys())
376 ['xref', 'from', ':lines', ':bytes', 'references', 'date', 'message-id', 'subject']
377 >>> over['from']
378 '=?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= <martin@v.loewis.de>'
379 >>> nntplib.decode_header(over['from'])
380 '"Martin v. Löwis" <martin@v.loewis.de>'
381
382 .. versionadded:: 3.2
383
384
385.. method:: NNTP.help(*, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000386
387 Send a ``HELP`` command. Return a pair ``(response, list)`` where *list* is a
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000388 list of help strings.
Georg Brandl116aa622007-08-15 14:28:22 +0000389
390
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000391.. method:: NNTP.stat(message_spec=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000392
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000393 Send a ``STAT`` command, where *message_spec* is either a message id
394 (enclosed in ``'<'`` and ``'>'``) or an article number in the current group.
395 If *message_spec* is omitted or :const:`None`, the current article in the
396 current group is considered. Return a triple ``(response, number, id)``
397 where *number* is the article number and *id* is the message id.
398
399 >>> _, _, first, last, _ = s.group('gmane.comp.python.devel')
400 >>> resp, number, message_id = s.stat(first)
401 >>> number, message_id
402 (9099, '<20030112190404.GE29873@epoch.metaslash.com>')
Georg Brandl116aa622007-08-15 14:28:22 +0000403
404
405.. method:: NNTP.next()
406
Georg Brandl21527bf2013-10-29 08:14:51 +0100407 Send a ``NEXT`` command. Return as for :meth:`.stat`.
Georg Brandl116aa622007-08-15 14:28:22 +0000408
409
410.. method:: NNTP.last()
411
Georg Brandl21527bf2013-10-29 08:14:51 +0100412 Send a ``LAST`` command. Return as for :meth:`.stat`.
Georg Brandl116aa622007-08-15 14:28:22 +0000413
414
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000415.. method:: NNTP.article(message_spec=None, *, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000416
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000417 Send an ``ARTICLE`` command, where *message_spec* has the same meaning as
Georg Brandl21527bf2013-10-29 08:14:51 +0100418 for :meth:`.stat`. Return a tuple ``(response, info)`` where *info*
Senthil Kumarana6bac952011-07-04 11:28:30 -0700419 is a :class:`~collections.namedtuple` with three attributes *number*,
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000420 *message_id* and *lines* (in that order). *number* is the article number
421 in the group (or 0 if the information is not available), *message_id* the
422 message id as a string, and *lines* a list of lines (without terminating
423 newlines) comprising the raw message including headers and body.
424
425 >>> resp, info = s.article('<20030112190404.GE29873@epoch.metaslash.com>')
426 >>> info.number
427 0
428 >>> info.message_id
429 '<20030112190404.GE29873@epoch.metaslash.com>'
430 >>> len(info.lines)
431 65
432 >>> info.lines[0]
433 b'Path: main.gmane.org!not-for-mail'
434 >>> info.lines[1]
435 b'From: Neal Norwitz <neal@metaslash.com>'
436 >>> info.lines[-3:]
437 [b'There is a patch for 2.3 as well as 2.2.', b'', b'Neal']
Georg Brandl116aa622007-08-15 14:28:22 +0000438
439
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000440.. method:: NNTP.head(message_spec=None, *, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000441
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000442 Same as :meth:`article()`, but sends a ``HEAD`` command. The *lines*
443 returned (or written to *file*) will only contain the message headers, not
444 the body.
Georg Brandl116aa622007-08-15 14:28:22 +0000445
446
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000447.. method:: NNTP.body(message_spec=None, *, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000448
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000449 Same as :meth:`article()`, but sends a ``BODY`` command. The *lines*
450 returned (or written to *file*) will only contain the message body, not the
451 headers.
452
453
454.. method:: NNTP.post(data)
455
456 Post an article using the ``POST`` command. The *data* argument is either
457 a :term:`file object` opened for binary reading, or any iterable of bytes
458 objects (representing raw lines of the article to be posted). It should
459 represent a well-formed news article, including the required headers. The
460 :meth:`post` method automatically escapes lines beginning with ``.`` and
461 appends the termination line.
462
463 If the method succeeds, the server's response is returned. If the server
464 refuses posting, a :class:`NNTPReplyError` is raised.
465
466
467.. method:: NNTP.ihave(message_id, data)
468
469 Send an ``IHAVE`` command. *message_id* is the id of the message to send
470 to the server (enclosed in ``'<'`` and ``'>'``). The *data* parameter
471 and the return value are the same as for :meth:`post()`.
472
473
474.. method:: NNTP.date()
475
476 Return a pair ``(response, date)``. *date* is a :class:`~datetime.datetime`
477 object containing the current date and time of the server.
Georg Brandl116aa622007-08-15 14:28:22 +0000478
479
480.. method:: NNTP.slave()
481
482 Send a ``SLAVE`` command. Return the server's *response*.
483
484
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000485.. method:: NNTP.set_debuglevel(level)
Georg Brandl116aa622007-08-15 14:28:22 +0000486
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000487 Set the instance's debugging level. This controls the amount of debugging
488 output printed. The default, ``0``, produces no debugging output. A value of
489 ``1`` produces a moderate amount of debugging output, generally a single line
490 per request or response. A value of ``2`` or higher produces the maximum amount
491 of debugging output, logging each line sent and received on the connection
492 (including message text).
493
494
495The following are optional NNTP extensions defined in :rfc:`2980`. Some of
496them have been superseded by newer commands in :rfc:`3977`.
497
498
Ezio Melottie927e252012-09-08 20:46:01 +0300499.. method:: NNTP.xhdr(hdr, str, *, file=None)
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000500
Ezio Melottie927e252012-09-08 20:46:01 +0300501 Send an ``XHDR`` command. The *hdr* argument is a header keyword, e.g.
502 ``'subject'``. The *str* argument should have the form ``'first-last'``
Georg Brandl116aa622007-08-15 14:28:22 +0000503 where *first* and *last* are the first and last article numbers to search.
504 Return a pair ``(response, list)``, where *list* is a list of pairs ``(id,
505 text)``, where *id* is an article number (as a string) and *text* is the text of
506 the requested header for that article. If the *file* parameter is supplied, then
507 the output of the ``XHDR`` command is stored in a file. If *file* is a string,
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000508 then the method will open a file with that name, write to it then close it.
509 If *file* is a :term:`file object`, then it will start calling :meth:`write` on
510 it to store the lines of the command output. If *file* is supplied, then the
Georg Brandl116aa622007-08-15 14:28:22 +0000511 returned *list* is an empty list.
512
513
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000514.. method:: NNTP.xover(start, end, *, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000515
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000516 Send an ``XOVER`` command. *start* and *end* are article numbers
517 delimiting the range of articles to select. The return value is the
518 same of for :meth:`over()`. It is recommended to use :meth:`over()`
519 instead, since it will automatically use the newer ``OVER`` command
520 if available.
Georg Brandl116aa622007-08-15 14:28:22 +0000521
522
523.. method:: NNTP.xpath(id)
524
525 Return a pair ``(resp, path)``, where *path* is the directory path to the
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000526 article with message ID *id*. Most of the time, this extension is not
527 enabled by NNTP server administrators.
Georg Brandl116aa622007-08-15 14:28:22 +0000528
Florent Xicluna67317752011-12-10 11:07:42 +0100529 .. deprecated:: 3.3
530 The XPATH extension is not actively used.
531
Georg Brandl116aa622007-08-15 14:28:22 +0000532
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000533.. XXX deprecated:
Georg Brandl116aa622007-08-15 14:28:22 +0000534
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000535 .. method:: NNTP.xgtitle(name, *, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000536
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000537 Process an ``XGTITLE`` command, returning a pair ``(response, list)``, where
538 *list* is a list of tuples containing ``(name, title)``. If the *file* parameter
539 is supplied, then the output of the ``XGTITLE`` command is stored in a file.
540 If *file* is a string, then the method will open a file with that name, write
541 to it then close it. If *file* is a :term:`file object`, then it will start
542 calling :meth:`write` on it to store the lines of the command output. If *file*
543 is supplied, then the returned *list* is an empty list. This is an optional NNTP
544 extension, and may not be supported by all servers.
545
546 RFC2980 says "It is suggested that this extension be deprecated". Use
547 :meth:`descriptions` or :meth:`description` instead.
548
549
550Utility functions
551-----------------
552
553The module also defines the following utility function:
554
555
556.. function:: decode_header(header_str)
557
558 Decode a header value, un-escaping any escaped non-ASCII characters.
559 *header_str* must be a :class:`str` object. The unescaped value is
560 returned. Using this function is recommended to display some headers
561 in a human readable form::
562
563 >>> decode_header("Some subject")
564 'Some subject'
565 >>> decode_header("=?ISO-8859-15?Q?D=E9buter_en_Python?=")
566 'Débuter en Python'
567 >>> decode_header("Re: =?UTF-8?B?cHJvYmzDqG1lIGRlIG1hdHJpY2U=?=")
568 'Re: problème de matrice'