blob: ef507e1ff9803f56ed3eb94a72fc195cce348e22 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`nntplib` --- NNTP protocol client
3=======================================
4
5.. module:: nntplib
6 :synopsis: NNTP protocol client (requires sockets).
7
8
9.. index::
10 pair: NNTP; protocol
11 single: Network News Transfer Protocol
12
Raymond Hettinger469271d2011-01-27 20:38:46 +000013**Source code:** :source:`Lib/nntplib.py`
14
15--------------
16
Georg Brandl116aa622007-08-15 14:28:22 +000017This module defines the class :class:`NNTP` which implements the client side of
Antoine Pitrou69ab9512010-09-29 15:03:40 +000018the Network News Transfer Protocol. It can be used to implement a news reader
19or poster, or automated news processors. It is compatible with :rfc:`3977`
20as well as the older :rfc:`977` and :rfc:`2980`.
Georg Brandl116aa622007-08-15 14:28:22 +000021
22Here are two small examples of how it can be used. To list some statistics
23about a newsgroup and print the subjects of the last 10 articles::
24
Antoine Pitrou69ab9512010-09-29 15:03:40 +000025 >>> s = nntplib.NNTP('news.gmane.org')
Antoine Pitroue3396512010-09-07 18:44:12 +000026 >>> resp, count, first, last, name = s.group('gmane.comp.python.committers')
Georg Brandl6911e3c2007-09-04 07:15:32 +000027 >>> print('Group', name, 'has', count, 'articles, range', first, 'to', last)
Antoine Pitrou69ab9512010-09-29 15:03:40 +000028 Group gmane.comp.python.committers has 1096 articles, range 1 to 1096
29 >>> resp, overviews = s.over((last - 9, last))
30 >>> for id, over in overviews:
31 ... print(id, nntplib.decode_header(over['subject']))
Georg Brandl48310cd2009-01-03 21:18:54 +000032 ...
Antoine Pitrou69ab9512010-09-29 15:03:40 +000033 1087 Re: Commit privileges for Łukasz Langa
34 1088 Re: 3.2 alpha 2 freeze
35 1089 Re: 3.2 alpha 2 freeze
36 1090 Re: Commit privileges for Łukasz Langa
37 1091 Re: Commit privileges for Łukasz Langa
38 1092 Updated ssh key
39 1093 Re: Updated ssh key
40 1094 Re: Updated ssh key
41 1095 Hello fellow committers!
42 1096 Re: Hello fellow committers!
Georg Brandl116aa622007-08-15 14:28:22 +000043 >>> s.quit()
Antoine Pitroue3396512010-09-07 18:44:12 +000044 '205 Bye!'
Georg Brandl116aa622007-08-15 14:28:22 +000045
Antoine Pitrou69ab9512010-09-29 15:03:40 +000046To post an article from a binary file (this assumes that the article has valid
Antoine Pitroue3396512010-09-07 18:44:12 +000047headers, and that you have right to post on the particular newsgroup)::
Georg Brandl116aa622007-08-15 14:28:22 +000048
Antoine Pitrou69ab9512010-09-29 15:03:40 +000049 >>> s = nntplib.NNTP('news.gmane.org')
50 >>> f = open('/tmp/article.txt', 'rb')
Georg Brandl116aa622007-08-15 14:28:22 +000051 >>> s.post(f)
52 '240 Article posted successfully.'
53 >>> s.quit()
Antoine Pitroue3396512010-09-07 18:44:12 +000054 '205 Bye!'
Georg Brandl116aa622007-08-15 14:28:22 +000055
Antoine Pitrou69ab9512010-09-29 15:03:40 +000056The module itself defines the following classes:
Georg Brandl116aa622007-08-15 14:28:22 +000057
58
Antoine Pitrou859c4ef2010-11-09 18:58:42 +000059.. class:: NNTP(host, port=119, user=None, password=None, readermode=None, usenetrc=False, [timeout])
Georg Brandl116aa622007-08-15 14:28:22 +000060
Antoine Pitroua0781152010-11-05 19:16:37 +000061 Return a new :class:`NNTP` object, representing a connection
Antoine Pitrou69ab9512010-09-29 15:03:40 +000062 to the NNTP server running on host *host*, listening at port *port*.
63 An optional *timeout* can be specified for the socket connection.
64 If the optional *user* and *password* are provided, or if suitable
65 credentials are present in :file:`/.netrc` and the optional flag *usenetrc*
Antoine Pitrou859c4ef2010-11-09 18:58:42 +000066 is true, the ``AUTHINFO USER`` and ``AUTHINFO PASS`` commands are used
67 to identify and authenticate the user to the server. If the optional
Antoine Pitrou69ab9512010-09-29 15:03:40 +000068 flag *readermode* is true, then a ``mode reader`` command is sent before
69 authentication is performed. Reader mode is sometimes necessary if you are
70 connecting to an NNTP server on the local machine and intend to call
71 reader-specific commands, such as ``group``. If you get unexpected
Georg Brandl116aa622007-08-15 14:28:22 +000072 :exc:`NNTPPermanentError`\ s, you might need to set *readermode*.
Antoine Pitrou859c4ef2010-11-09 18:58:42 +000073
74 .. versionchanged:: 3.2
75 *usenetrc* is now False by default.
Georg Brandl116aa622007-08-15 14:28:22 +000076
Georg Brandl116aa622007-08-15 14:28:22 +000077
Antoine Pitrou859c4ef2010-11-09 18:58:42 +000078.. 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 +000079
80 Return a new :class:`NNTP_SSL` object, representing an encrypted
81 connection to the NNTP server running on host *host*, listening at
82 port *port*. :class:`NNTP_SSL` objects have the same methods as
83 :class:`NNTP` objects. If *port* is omitted, port 563 (NNTPS) is used.
84 *ssl_context* is also optional, and is a :class:`~ssl.SSLContext` object.
85 All other parameters behave the same as for :class:`NNTP`.
86
87 Note that SSL-on-563 is discouraged per :rfc:`4642`, in favor of
88 STARTTLS as described below. However, some servers only support the
89 former.
90
91 .. versionadded:: 3.2
92
93
Georg Brandl116aa622007-08-15 14:28:22 +000094.. exception:: NNTPError
95
Antoine Pitrou69ab9512010-09-29 15:03:40 +000096 Derived from the standard exception :exc:`Exception`, this is the base
97 class for all exceptions raised by the :mod:`nntplib` module. Instances
98 of this class have the following attribute:
99
100 .. attribute:: response
101
102 The response of the server if available, as a :class:`str` object.
Georg Brandl116aa622007-08-15 14:28:22 +0000103
104
105.. exception:: NNTPReplyError
106
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000107 Exception raised when an unexpected reply is received from the server.
Georg Brandl116aa622007-08-15 14:28:22 +0000108
109
110.. exception:: NNTPTemporaryError
111
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000112 Exception raised when a response code in the range 400--499 is received.
Georg Brandl116aa622007-08-15 14:28:22 +0000113
114
115.. exception:: NNTPPermanentError
116
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000117 Exception raised when a response code in the range 500--599 is received.
Georg Brandl116aa622007-08-15 14:28:22 +0000118
119
120.. exception:: NNTPProtocolError
121
122 Exception raised when a reply is received from the server that does not begin
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000123 with a digit in the range 1--5.
Georg Brandl116aa622007-08-15 14:28:22 +0000124
125
126.. exception:: NNTPDataError
127
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000128 Exception raised when there is some error in the response data.
Georg Brandl116aa622007-08-15 14:28:22 +0000129
130
131.. _nntp-objects:
132
133NNTP Objects
134------------
135
Antoine Pitrou1cb121e2010-11-09 18:54:37 +0000136When connected, :class:`NNTP` and :class:`NNTP_SSL` objects support the
137following methods and attributes.
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000138
Antoine Pitroua0781152010-11-05 19:16:37 +0000139Attributes
140^^^^^^^^^^
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000141
Antoine Pitroua0781152010-11-05 19:16:37 +0000142.. attribute:: NNTP.nntp_version
143
144 An integer representing the version of the NNTP protocol supported by the
145 server. In practice, this should be ``2`` for servers advertising
146 :rfc:`3977` compliance and ``1`` for others.
147
148 .. versionadded:: 3.2
149
150.. attribute:: NNTP.nntp_implementation
151
152 A string describing the software name and version of the NNTP server,
153 or :const:`None` if not advertised by the server.
154
155 .. versionadded:: 3.2
156
157Methods
158^^^^^^^
159
160The *response* that is returned as the first item in the return tuple of almost
161all methods is the server's response: a string beginning with a three-digit
162code. If the server's response indicates an error, the method raises one of
163the above exceptions.
164
165Many of the following methods take an optional keyword-only argument *file*.
166When the *file* argument is supplied, it must be either a :term:`file object`
167opened for binary writing, or the name of an on-disk file to be written to.
168The method will then write any data returned by the server (except for the
169response line and the terminating dot) to the file; any list of lines,
170tuples or objects that the method normally returns will be empty.
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000171
172.. versionchanged:: 3.2
173 Many of the following methods have been reworked and fixed, which makes
174 them incompatible with their 3.1 counterparts.
175
176
177.. method:: NNTP.quit()
178
179 Send a ``QUIT`` command and close the connection. Once this method has been
180 called, no other methods of the NNTP object should be called.
Georg Brandl116aa622007-08-15 14:28:22 +0000181
182
183.. method:: NNTP.getwelcome()
184
185 Return the welcome message sent by the server in reply to the initial
186 connection. (This message sometimes contains disclaimers or help information
187 that may be relevant to the user.)
188
189
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000190.. method:: NNTP.getcapabilities()
Georg Brandl116aa622007-08-15 14:28:22 +0000191
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000192 Return the :rfc:`3977` capabilities advertised by the server, as a
193 :class:`dict` instance mapping capability names to (possibly empty) lists
194 of values. On legacy servers which don't understand the ``CAPABILITIES``
195 command, an empty dictionary is returned instead.
196
197 >>> s = NNTP('news.gmane.org')
198 >>> 'POST' in s.getcapabilities()
199 True
200
201 .. versionadded:: 3.2
Georg Brandl116aa622007-08-15 14:28:22 +0000202
203
Antoine Pitrou1cb121e2010-11-09 18:54:37 +0000204.. method:: NNTP.login(user=None, password=None, usenetrc=True)
205
206 Send ``AUTHINFO`` commands with the user name and password. If *user*
207 and *password* are None and *usenetrc* is True, credentials from
208 ``~/.netrc`` will be used if possible.
209
210 Unless intentionally delayed, login is normally performed during the
211 :class:`NNTP` object initialization and separately calling this function
212 is unnecessary. To force authentication to be delayed, you must not set
213 *user* or *password* when creating the object, and must set *usenetrc* to
214 False.
215
216 .. versionadded:: 3.2
217
218
219.. method:: NNTP.starttls(ssl_context=None)
220
221 Send a ``STARTTLS`` command. The *ssl_context* argument is optional
222 and should be a :class:`ssl.SSLContext` object. This will enable
223 encryption on the NNTP connection.
224
225 Note that this may not be done after authentication information has
226 been transmitted, and authentication occurs by default if possible during a
227 :class:`NNTP` object initialization. See :meth:`NNTP.login` for information
228 on suppressing this behavior.
229
230 .. versionadded:: 3.2
231
232
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000233.. method:: NNTP.newgroups(date, *, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000234
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000235 Send a ``NEWGROUPS`` command. The *date* argument should be a
236 :class:`datetime.date` or :class:`datetime.datetime` object.
237 Return a pair ``(response, groups)`` where *groups* is a list representing
238 the groups that are new since the given *date*. If *file* is supplied,
239 though, then *groups* will be empty.
240
241 >>> from datetime import date, timedelta
242 >>> resp, groups = s.newgroups(date.today() - timedelta(days=3))
243 >>> len(groups)
244 85
245 >>> groups[0]
246 GroupInfo(group='gmane.network.tor.devel', last='4', first='1', flag='m')
Georg Brandl116aa622007-08-15 14:28:22 +0000247
248
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000249.. method:: NNTP.newnews(group, date, *, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000250
251 Send a ``NEWNEWS`` command. Here, *group* is a group name or ``'*'``, and
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000252 *date* has the same meaning as for :meth:`newgroups`. Return a pair
253 ``(response, articles)`` where *articles* is a list of message ids.
254
255 This command is frequently disabled by NNTP server administrators.
Georg Brandl116aa622007-08-15 14:28:22 +0000256
257
Antoine Pitrou08eeada2010-11-04 21:36:15 +0000258.. method:: NNTP.list(group_pattern=None, *, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000259
Antoine Pitrou08eeada2010-11-04 21:36:15 +0000260 Send a ``LIST`` or ``LIST ACTIVE`` command. Return a pair
261 ``(response, list)`` where *list* is a list of tuples representing all
262 the groups available from this NNTP server, optionally matching the
263 pattern string *group_pattern*. Each tuple has the form
264 ``(group, last, first, flag)``, where *group* is a group name, *last*
265 and *first* are the last and first article numbers, and *flag* usually
266 takes one of these values:
Antoine Pitrou7dd1af02010-11-03 18:32:54 +0000267
268 * ``y``: Local postings and articles from peers are allowed.
269 * ``m``: The group is moderated and all postings must be approved.
270 * ``n``: No local postings are allowed, only articles from peers.
271 * ``j``: Articles from peers are filed in the junk group instead.
272 * ``x``: No local postings, and articles from peers are ignored.
273 * ``=foo.bar``: Articles are filed in the ``foo.bar`` group instead.
274
275 If *flag* has another value, then the status of the newsgroup should be
276 considered unknown.
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000277
Antoine Pitrou08eeada2010-11-04 21:36:15 +0000278 This command can return very large results, especially if *group_pattern*
279 is not specified. It is best to cache the results offline unless you
280 really need to refresh them.
281
282 .. versionchanged:: 3.2
283 *group_pattern* was added.
Georg Brandl116aa622007-08-15 14:28:22 +0000284
285
286.. method:: NNTP.descriptions(grouppattern)
287
288 Send a ``LIST NEWSGROUPS`` command, where *grouppattern* is a wildmat string as
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000289 specified in :rfc:`3977` (it's essentially the same as DOS or UNIX shell wildcard
290 strings). Return a pair ``(response, descriptions)``, where *descriptions*
291 is a dictionary mapping group names to textual descriptions.
292
293 >>> resp, descs = s.descriptions('gmane.comp.python.*')
294 >>> len(descs)
295 295
296 >>> descs.popitem()
297 ('gmane.comp.python.bio.general', 'BioPython discussion list (Moderated)')
Georg Brandl116aa622007-08-15 14:28:22 +0000298
Georg Brandl116aa622007-08-15 14:28:22 +0000299
300.. method:: NNTP.description(group)
301
302 Get a description for a single group *group*. If more than one group matches
303 (if 'group' is a real wildmat string), return the first match. If no group
304 matches, return an empty string.
305
306 This elides the response code from the server. If the response code is needed,
307 use :meth:`descriptions`.
308
Georg Brandl116aa622007-08-15 14:28:22 +0000309
310.. method:: NNTP.group(name)
311
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000312 Send a ``GROUP`` command, where *name* is the group name. The group is
313 selected as the current group, if it exists. Return a tuple
314 ``(response, count, first, last, name)`` where *count* is the (estimated)
315 number of articles in the group, *first* is the first article number in
316 the group, *last* is the last article number in the group, and *name*
317 is the group name.
Georg Brandl116aa622007-08-15 14:28:22 +0000318
319
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000320.. method:: NNTP.over(message_spec, *, file=None)
321
322 Send a ``OVER`` command, or a ``XOVER`` command on legacy servers.
323 *message_spec* can be either a string representing a message id, or
324 a ``(first, last)`` tuple of numbers indicating a range of articles in
325 the current group, or a ``(first, None)`` tuple indicating a range of
326 articles starting from *first* to the last article in the current group,
327 or :const:`None` to select the current article in the current group.
328
329 Return a pair ``(response, overviews)``. *overviews* is a list of
330 ``(article_number, overview)`` tuples, one for each article selected
331 by *message_spec*. Each *overview* is a dictionary with the same number
332 of items, but this number depends on the server. These items are either
333 message headers (the key is then the lower-cased header name) or metadata
334 items (the key is then the metadata name prepended with ``":"``). The
335 following items are guaranteed to be present by the NNTP specification:
336
337 * the ``subject``, ``from``, ``date``, ``message-id`` and ``references``
338 headers
339 * the ``:bytes`` metadata: the number of bytes in the entire raw article
340 (including headers and body)
341 * the ``:lines`` metadata: the number of lines in the article body
342
Antoine Pitrou4103bc02010-11-03 18:18:43 +0000343 The value of each item is either a string, or :const:`None` if not present.
344
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000345 It is advisable to use the :func:`decode_header` function on header
346 values when they may contain non-ASCII characters::
347
348 >>> _, _, first, last, _ = s.group('gmane.comp.python.devel')
349 >>> resp, overviews = s.over((last, last))
350 >>> art_num, over = overviews[0]
351 >>> art_num
352 117216
353 >>> list(over.keys())
354 ['xref', 'from', ':lines', ':bytes', 'references', 'date', 'message-id', 'subject']
355 >>> over['from']
356 '=?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= <martin@v.loewis.de>'
357 >>> nntplib.decode_header(over['from'])
358 '"Martin v. Löwis" <martin@v.loewis.de>'
359
360 .. versionadded:: 3.2
361
362
363.. method:: NNTP.help(*, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000364
365 Send a ``HELP`` command. Return a pair ``(response, list)`` where *list* is a
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000366 list of help strings.
Georg Brandl116aa622007-08-15 14:28:22 +0000367
368
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000369.. method:: NNTP.stat(message_spec=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000370
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000371 Send a ``STAT`` command, where *message_spec* is either a message id
372 (enclosed in ``'<'`` and ``'>'``) or an article number in the current group.
373 If *message_spec* is omitted or :const:`None`, the current article in the
374 current group is considered. Return a triple ``(response, number, id)``
375 where *number* is the article number and *id* is the message id.
376
377 >>> _, _, first, last, _ = s.group('gmane.comp.python.devel')
378 >>> resp, number, message_id = s.stat(first)
379 >>> number, message_id
380 (9099, '<20030112190404.GE29873@epoch.metaslash.com>')
Georg Brandl116aa622007-08-15 14:28:22 +0000381
382
383.. method:: NNTP.next()
384
385 Send a ``NEXT`` command. Return as for :meth:`stat`.
386
387
388.. method:: NNTP.last()
389
390 Send a ``LAST`` command. Return as for :meth:`stat`.
391
392
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000393.. method:: NNTP.article(message_spec=None, *, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000394
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000395 Send an ``ARTICLE`` command, where *message_spec* has the same meaning as
396 for :meth:`stat`. Return a tuple ``(response, info)`` where *info*
Senthil Kumarana6bac952011-07-04 11:28:30 -0700397 is a :class:`~collections.namedtuple` with three attributes *number*,
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000398 *message_id* and *lines* (in that order). *number* is the article number
399 in the group (or 0 if the information is not available), *message_id* the
400 message id as a string, and *lines* a list of lines (without terminating
401 newlines) comprising the raw message including headers and body.
402
403 >>> resp, info = s.article('<20030112190404.GE29873@epoch.metaslash.com>')
404 >>> info.number
405 0
406 >>> info.message_id
407 '<20030112190404.GE29873@epoch.metaslash.com>'
408 >>> len(info.lines)
409 65
410 >>> info.lines[0]
411 b'Path: main.gmane.org!not-for-mail'
412 >>> info.lines[1]
413 b'From: Neal Norwitz <neal@metaslash.com>'
414 >>> info.lines[-3:]
415 [b'There is a patch for 2.3 as well as 2.2.', b'', b'Neal']
Georg Brandl116aa622007-08-15 14:28:22 +0000416
417
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000418.. method:: NNTP.head(message_spec=None, *, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000419
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000420 Same as :meth:`article()`, but sends a ``HEAD`` command. The *lines*
421 returned (or written to *file*) will only contain the message headers, not
422 the body.
Georg Brandl116aa622007-08-15 14:28:22 +0000423
424
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000425.. method:: NNTP.body(message_spec=None, *, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000426
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000427 Same as :meth:`article()`, but sends a ``BODY`` command. The *lines*
428 returned (or written to *file*) will only contain the message body, not the
429 headers.
430
431
432.. method:: NNTP.post(data)
433
434 Post an article using the ``POST`` command. The *data* argument is either
435 a :term:`file object` opened for binary reading, or any iterable of bytes
436 objects (representing raw lines of the article to be posted). It should
437 represent a well-formed news article, including the required headers. The
438 :meth:`post` method automatically escapes lines beginning with ``.`` and
439 appends the termination line.
440
441 If the method succeeds, the server's response is returned. If the server
442 refuses posting, a :class:`NNTPReplyError` is raised.
443
444
445.. method:: NNTP.ihave(message_id, data)
446
447 Send an ``IHAVE`` command. *message_id* is the id of the message to send
448 to the server (enclosed in ``'<'`` and ``'>'``). The *data* parameter
449 and the return value are the same as for :meth:`post()`.
450
451
452.. method:: NNTP.date()
453
454 Return a pair ``(response, date)``. *date* is a :class:`~datetime.datetime`
455 object containing the current date and time of the server.
Georg Brandl116aa622007-08-15 14:28:22 +0000456
457
458.. method:: NNTP.slave()
459
460 Send a ``SLAVE`` command. Return the server's *response*.
461
462
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000463.. method:: NNTP.set_debuglevel(level)
Georg Brandl116aa622007-08-15 14:28:22 +0000464
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000465 Set the instance's debugging level. This controls the amount of debugging
466 output printed. The default, ``0``, produces no debugging output. A value of
467 ``1`` produces a moderate amount of debugging output, generally a single line
468 per request or response. A value of ``2`` or higher produces the maximum amount
469 of debugging output, logging each line sent and received on the connection
470 (including message text).
471
472
473The following are optional NNTP extensions defined in :rfc:`2980`. Some of
474them have been superseded by newer commands in :rfc:`3977`.
475
476
477.. method:: NNTP.xhdr(header, string, *, file=None)
478
479 Send an ``XHDR`` command. The *header* argument is a header keyword, e.g.
Georg Brandl116aa622007-08-15 14:28:22 +0000480 ``'subject'``. The *string* argument should have the form ``'first-last'``
481 where *first* and *last* are the first and last article numbers to search.
482 Return a pair ``(response, list)``, where *list* is a list of pairs ``(id,
483 text)``, where *id* is an article number (as a string) and *text* is the text of
484 the requested header for that article. If the *file* parameter is supplied, then
485 the output of the ``XHDR`` command is stored in a file. If *file* is a string,
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000486 then the method will open a file with that name, write to it then close it.
487 If *file* is a :term:`file object`, then it will start calling :meth:`write` on
488 it to store the lines of the command output. If *file* is supplied, then the
Georg Brandl116aa622007-08-15 14:28:22 +0000489 returned *list* is an empty list.
490
491
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000492.. method:: NNTP.xover(start, end, *, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000493
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000494 Send an ``XOVER`` command. *start* and *end* are article numbers
495 delimiting the range of articles to select. The return value is the
496 same of for :meth:`over()`. It is recommended to use :meth:`over()`
497 instead, since it will automatically use the newer ``OVER`` command
498 if available.
Georg Brandl116aa622007-08-15 14:28:22 +0000499
500
501.. method:: NNTP.xpath(id)
502
503 Return a pair ``(resp, path)``, where *path* is the directory path to the
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000504 article with message ID *id*. Most of the time, this extension is not
505 enabled by NNTP server administrators.
Georg Brandl116aa622007-08-15 14:28:22 +0000506
507
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000508.. XXX deprecated:
Georg Brandl116aa622007-08-15 14:28:22 +0000509
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000510 .. method:: NNTP.xgtitle(name, *, file=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000511
Antoine Pitrou69ab9512010-09-29 15:03:40 +0000512 Process an ``XGTITLE`` command, returning a pair ``(response, list)``, where
513 *list* is a list of tuples containing ``(name, title)``. If the *file* parameter
514 is supplied, then the output of the ``XGTITLE`` command is stored in a file.
515 If *file* is a string, then the method will open a file with that name, write
516 to it then close it. If *file* is a :term:`file object`, then it will start
517 calling :meth:`write` on it to store the lines of the command output. If *file*
518 is supplied, then the returned *list* is an empty list. This is an optional NNTP
519 extension, and may not be supported by all servers.
520
521 RFC2980 says "It is suggested that this extension be deprecated". Use
522 :meth:`descriptions` or :meth:`description` instead.
523
524
525Utility functions
526-----------------
527
528The module also defines the following utility function:
529
530
531.. function:: decode_header(header_str)
532
533 Decode a header value, un-escaping any escaped non-ASCII characters.
534 *header_str* must be a :class:`str` object. The unescaped value is
535 returned. Using this function is recommended to display some headers
536 in a human readable form::
537
538 >>> decode_header("Some subject")
539 'Some subject'
540 >>> decode_header("=?ISO-8859-15?Q?D=E9buter_en_Python?=")
541 'Débuter en Python'
542 >>> decode_header("Re: =?UTF-8?B?cHJvYmzDqG1lIGRlIG1hdHJpY2U=?=")
543 'Re: problème de matrice'