blob: c3cbd2bc954509dfaf9aa1abeebb4e1a671077a8 [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
13This module defines the class :class:`NNTP` which implements the client side of
14the NNTP protocol. It can be used to implement a news reader or poster, or
15automated news processors. For more information on NNTP (Network News Transfer
16Protocol), see Internet :rfc:`977`.
17
18Here are two small examples of how it can be used. To list some statistics
19about a newsgroup and print the subjects of the last 10 articles::
20
Antoine Pitrou824cf252010-09-07 18:44:52 +000021 >>> s = NNTP('news.gmane.org')
22 >>> resp, count, first, last, name = s.group('gmane.comp.python.committers')
Georg Brandl6911e3c2007-09-04 07:15:32 +000023 >>> print('Group', name, 'has', count, 'articles, range', first, 'to', last)
Antoine Pitrou824cf252010-09-07 18:44:52 +000024 Group gmane.comp.python.committers has 1071 articles, range 1 to 1071
Georg Brandl116aa622007-08-15 14:28:22 +000025 >>> resp, subs = s.xhdr('subject', first + '-' + last)
Georg Brandl6911e3c2007-09-04 07:15:32 +000026 >>> for id, sub in subs[-10:]: print(id, sub)
Georg Brandl48310cd2009-01-03 21:18:54 +000027 ...
Antoine Pitrou824cf252010-09-07 18:44:52 +000028 1062 Re: Mercurial Status?
29 1063 Re: [python-committers] (Windows) buildbots on 3.x
30 1064 Re: Mercurial Status?
31 1065 Re: Mercurial Status?
32 1066 Python 2.6.6 status
33 1067 Commit Privileges for Ask Solem
34 1068 Re: Commit Privileges for Ask Solem
35 1069 Re: Commit Privileges for Ask Solem
36 1070 Re: Commit Privileges for Ask Solem
37 1071 2.6.6 rc 2
Georg Brandl116aa622007-08-15 14:28:22 +000038 >>> s.quit()
Antoine Pitrou824cf252010-09-07 18:44:52 +000039 '205 Bye!'
Georg Brandl116aa622007-08-15 14:28:22 +000040
41To post an article from a file (this assumes that the article has valid
Antoine Pitrou824cf252010-09-07 18:44:52 +000042headers, and that you have right to post on the particular newsgroup)::
Georg Brandl116aa622007-08-15 14:28:22 +000043
Antoine Pitrou824cf252010-09-07 18:44:52 +000044 >>> s = NNTP('news.gmane.org')
Georg Brandl116aa622007-08-15 14:28:22 +000045 >>> f = open('/tmp/article')
46 >>> s.post(f)
47 '240 Article posted successfully.'
48 >>> s.quit()
Antoine Pitrou824cf252010-09-07 18:44:52 +000049 '205 Bye!'
Georg Brandl116aa622007-08-15 14:28:22 +000050
51The module itself defines the following items:
52
53
Georg Brandl55ac8f02007-09-01 13:51:09 +000054.. class:: NNTP(host[, port [, user[, password [, readermode][, usenetrc]]]])
Georg Brandl116aa622007-08-15 14:28:22 +000055
56 Return a new instance of the :class:`NNTP` class, representing a connection
57 to the NNTP server running on host *host*, listening at port *port*. The
58 default *port* is 119. If the optional *user* and *password* are provided,
59 or if suitable credentials are present in :file:`/.netrc` and the optional
60 flag *usenetrc* is true (the default), the ``AUTHINFO USER`` and ``AUTHINFO
61 PASS`` commands are used to identify and authenticate the user to the server.
62 If the optional flag *readermode* is true, then a ``mode reader`` command is
63 sent before authentication is performed. Reader mode is sometimes necessary
64 if you are connecting to an NNTP server on the local machine and intend to
65 call reader-specific commands, such as ``group``. If you get unexpected
66 :exc:`NNTPPermanentError`\ s, you might need to set *readermode*.
67 *readermode* defaults to ``None``. *usenetrc* defaults to ``True``.
68
Georg Brandl116aa622007-08-15 14:28:22 +000069
70.. exception:: NNTPError
71
72 Derived from the standard exception :exc:`Exception`, this is the base class for
73 all exceptions raised by the :mod:`nntplib` module.
74
75
76.. exception:: NNTPReplyError
77
78 Exception raised when an unexpected reply is received from the server. For
79 backwards compatibility, the exception ``error_reply`` is equivalent to this
80 class.
81
82
83.. exception:: NNTPTemporaryError
84
85 Exception raised when an error code in the range 400--499 is received. For
86 backwards compatibility, the exception ``error_temp`` is equivalent to this
87 class.
88
89
90.. exception:: NNTPPermanentError
91
92 Exception raised when an error code in the range 500--599 is received. For
93 backwards compatibility, the exception ``error_perm`` is equivalent to this
94 class.
95
96
97.. exception:: NNTPProtocolError
98
99 Exception raised when a reply is received from the server that does not begin
100 with a digit in the range 1--5. For backwards compatibility, the exception
101 ``error_proto`` is equivalent to this class.
102
103
104.. exception:: NNTPDataError
105
106 Exception raised when there is some error in the response data. For backwards
107 compatibility, the exception ``error_data`` is equivalent to this class.
108
109
110.. _nntp-objects:
111
112NNTP Objects
113------------
114
115NNTP instances have the following methods. The *response* that is returned as
116the first item in the return tuple of almost all methods is the server's
117response: a string beginning with a three-digit code. If the server's response
118indicates an error, the method raises one of the above exceptions.
119
120
121.. method:: NNTP.getwelcome()
122
123 Return the welcome message sent by the server in reply to the initial
124 connection. (This message sometimes contains disclaimers or help information
125 that may be relevant to the user.)
126
127
128.. method:: NNTP.set_debuglevel(level)
129
130 Set the instance's debugging level. This controls the amount of debugging
131 output printed. The default, ``0``, produces no debugging output. A value of
132 ``1`` produces a moderate amount of debugging output, generally a single line
133 per request or response. A value of ``2`` or higher produces the maximum amount
134 of debugging output, logging each line sent and received on the connection
135 (including message text).
136
137
138.. method:: NNTP.newgroups(date, time, [file])
139
140 Send a ``NEWGROUPS`` command. The *date* argument should be a string of the
141 form ``'yymmdd'`` indicating the date, and *time* should be a string of the form
142 ``'hhmmss'`` indicating the time. Return a pair ``(response, groups)`` where
143 *groups* is a list of group names that are new since the given date and time. If
144 the *file* parameter is supplied, then the output of the ``NEWGROUPS`` command
145 is stored in a file. If *file* is a string, then the method will open a file
Antoine Pitrou25d535e2010-09-15 11:25:11 +0000146 object with that name, write to it then close it. If *file* is a :term:`file
147 object`, then it will start calling :meth:`write` on it to store the lines of
148 the command output. If *file* is supplied, then the returned *list* is an empty list.
Georg Brandl116aa622007-08-15 14:28:22 +0000149
150
151.. method:: NNTP.newnews(group, date, time, [file])
152
153 Send a ``NEWNEWS`` command. Here, *group* is a group name or ``'*'``, and
154 *date* and *time* have the same meaning as for :meth:`newgroups`. Return a pair
155 ``(response, articles)`` where *articles* is a list of message ids. If the
156 *file* parameter is supplied, then the output of the ``NEWNEWS`` command is
157 stored in a file. If *file* is a string, then the method will open a file
Antoine Pitrou25d535e2010-09-15 11:25:11 +0000158 object with that name, write to it then close it. If *file* is a :term:`file
159 object`, then it will start calling :meth:`write` on it to store the lines of the
160 command output. If *file* is supplied, then the returned *list* is an empty list.
Georg Brandl116aa622007-08-15 14:28:22 +0000161
162
163.. method:: NNTP.list([file])
164
165 Send a ``LIST`` command. Return a pair ``(response, list)`` where *list* is a
166 list of tuples. Each tuple has the form ``(group, last, first, flag)``, where
167 *group* is a group name, *last* and *first* are the last and first article
168 numbers (as strings), and *flag* is ``'y'`` if posting is allowed, ``'n'`` if
169 not, and ``'m'`` if the newsgroup is moderated. (Note the ordering: *last*,
170 *first*.) If the *file* parameter is supplied, then the output of the ``LIST``
171 command is stored in a file. If *file* is a string, then the method will open
Antoine Pitrou25d535e2010-09-15 11:25:11 +0000172 a file with that name, write to it then close it. If *file* is a :term:`file
173 object`, then it will start calling :meth:`write` on it to store the lines of
174 the command output. If *file* is supplied, then the returned *list* is an empty
Georg Brandl116aa622007-08-15 14:28:22 +0000175 list.
176
177
178.. method:: NNTP.descriptions(grouppattern)
179
180 Send a ``LIST NEWSGROUPS`` command, where *grouppattern* is a wildmat string as
181 specified in RFC2980 (it's essentially the same as DOS or UNIX shell wildcard
182 strings). Return a pair ``(response, list)``, where *list* is a list of tuples
183 containing ``(name, title)``.
184
Georg Brandl116aa622007-08-15 14:28:22 +0000185
186.. method:: NNTP.description(group)
187
188 Get a description for a single group *group*. If more than one group matches
189 (if 'group' is a real wildmat string), return the first match. If no group
190 matches, return an empty string.
191
192 This elides the response code from the server. If the response code is needed,
193 use :meth:`descriptions`.
194
Georg Brandl116aa622007-08-15 14:28:22 +0000195
196.. method:: NNTP.group(name)
197
198 Send a ``GROUP`` command, where *name* is the group name. Return a tuple
199 ``(response, count, first, last, name)`` where *count* is the (estimated) number
200 of articles in the group, *first* is the first article number in the group,
201 *last* is the last article number in the group, and *name* is the group name.
202 The numbers are returned as strings.
203
204
205.. method:: NNTP.help([file])
206
207 Send a ``HELP`` command. Return a pair ``(response, list)`` where *list* is a
208 list of help strings. If the *file* parameter is supplied, then the output of
209 the ``HELP`` command is stored in a file. If *file* is a string, then the
Antoine Pitrou25d535e2010-09-15 11:25:11 +0000210 method will open a file with that name, write to it then close it. If *file*
211 is a :term:`file object`, then it will start calling :meth:`write` on it to store
Georg Brandl116aa622007-08-15 14:28:22 +0000212 the lines of the command output. If *file* is supplied, then the returned *list*
213 is an empty list.
214
215
216.. method:: NNTP.stat(id)
217
218 Send a ``STAT`` command, where *id* is the message id (enclosed in ``'<'`` and
219 ``'>'``) or an article number (as a string). Return a triple ``(response,
220 number, id)`` where *number* is the article number (as a string) and *id* is the
221 message id (enclosed in ``'<'`` and ``'>'``).
222
223
224.. method:: NNTP.next()
225
226 Send a ``NEXT`` command. Return as for :meth:`stat`.
227
228
229.. method:: NNTP.last()
230
231 Send a ``LAST`` command. Return as for :meth:`stat`.
232
233
234.. method:: NNTP.head(id)
235
236 Send a ``HEAD`` command, where *id* has the same meaning as for :meth:`stat`.
237 Return a tuple ``(response, number, id, list)`` where the first three are the
238 same as for :meth:`stat`, and *list* is a list of the article's headers (an
239 uninterpreted list of lines, without trailing newlines).
240
241
242.. method:: NNTP.body(id,[file])
243
244 Send a ``BODY`` command, where *id* has the same meaning as for :meth:`stat`.
245 If the *file* parameter is supplied, then the body is stored in a file. If
Antoine Pitrou25d535e2010-09-15 11:25:11 +0000246 *file* is a string, then the method will open a file with that name, write
247 to it then close it. If *file* is a :term:`file object`, then it will start
Georg Brandl116aa622007-08-15 14:28:22 +0000248 calling :meth:`write` on it to store the lines of the body. Return as for
249 :meth:`head`. If *file* is supplied, then the returned *list* is an empty list.
250
251
252.. method:: NNTP.article(id)
253
254 Send an ``ARTICLE`` command, where *id* has the same meaning as for
255 :meth:`stat`. Return as for :meth:`head`.
256
257
258.. method:: NNTP.slave()
259
260 Send a ``SLAVE`` command. Return the server's *response*.
261
262
263.. method:: NNTP.xhdr(header, string, [file])
264
265 Send an ``XHDR`` command. This command is not defined in the RFC but is a
266 common extension. The *header* argument is a header keyword, e.g.
267 ``'subject'``. The *string* argument should have the form ``'first-last'``
268 where *first* and *last* are the first and last article numbers to search.
269 Return a pair ``(response, list)``, where *list* is a list of pairs ``(id,
270 text)``, where *id* is an article number (as a string) and *text* is the text of
271 the requested header for that article. If the *file* parameter is supplied, then
272 the output of the ``XHDR`` command is stored in a file. If *file* is a string,
Antoine Pitrou25d535e2010-09-15 11:25:11 +0000273 then the method will open a file with that name, write to it then close it.
274 If *file* is a :term:`file object`, then it will start calling :meth:`write` on
275 it to store the lines of the command output. If *file* is supplied, then the
Georg Brandl116aa622007-08-15 14:28:22 +0000276 returned *list* is an empty list.
277
278
279.. method:: NNTP.post(file)
280
281 Post an article using the ``POST`` command. The *file* argument is an open file
282 object which is read until EOF using its :meth:`readline` method. It should be
283 a well-formed news article, including the required headers. The :meth:`post`
284 method automatically escapes lines beginning with ``.``.
285
286
287.. method:: NNTP.ihave(id, file)
288
289 Send an ``IHAVE`` command. *id* is a message id (enclosed in ``'<'`` and
290 ``'>'``). If the response is not an error, treat *file* exactly as for the
291 :meth:`post` method.
292
293
294.. method:: NNTP.date()
295
296 Return a triple ``(response, date, time)``, containing the current date and time
297 in a form suitable for the :meth:`newnews` and :meth:`newgroups` methods. This
298 is an optional NNTP extension, and may not be supported by all servers.
299
300
301.. method:: NNTP.xgtitle(name, [file])
302
303 Process an ``XGTITLE`` command, returning a pair ``(response, list)``, where
304 *list* is a list of tuples containing ``(name, title)``. If the *file* parameter
305 is supplied, then the output of the ``XGTITLE`` command is stored in a file.
Antoine Pitrou25d535e2010-09-15 11:25:11 +0000306 If *file* is a string, then the method will open a file with that name, write
307 to it then close it. If *file* is a :term:`file object`, then it will start
Georg Brandl116aa622007-08-15 14:28:22 +0000308 calling :meth:`write` on it to store the lines of the command output. If *file*
309 is supplied, then the returned *list* is an empty list. This is an optional NNTP
310 extension, and may not be supported by all servers.
311
Georg Brandl116aa622007-08-15 14:28:22 +0000312 RFC2980 says "It is suggested that this extension be deprecated". Use
313 :meth:`descriptions` or :meth:`description` instead.
314
315
316.. method:: NNTP.xover(start, end, [file])
317
318 Return a pair ``(resp, list)``. *list* is a list of tuples, one for each
319 article in the range delimited by the *start* and *end* article numbers. Each
320 tuple is of the form ``(article number, subject, poster, date, id, references,
321 size, lines)``. If the *file* parameter is supplied, then the output of the
322 ``XOVER`` command is stored in a file. If *file* is a string, then the method
Antoine Pitrou25d535e2010-09-15 11:25:11 +0000323 will open a file with that name, write to it then close it. If *file* is a
324 :term:`file object`, then it will start calling :meth:`write` on it to store the
Georg Brandl116aa622007-08-15 14:28:22 +0000325 lines of the command output. If *file* is supplied, then the returned *list* is
326 an empty list. This is an optional NNTP extension, and may not be supported by
327 all servers.
328
329
330.. method:: NNTP.xpath(id)
331
332 Return a pair ``(resp, path)``, where *path* is the directory path to the
333 article with message ID *id*. This is an optional NNTP extension, and may not
334 be supported by all servers.
335
336
337.. method:: NNTP.quit()
338
339 Send a ``QUIT`` command and close the connection. Once this method has been
340 called, no other methods of the NNTP object should be called.
341