1 | \section{\module{nntplib} ---
|
---|
2 | NNTP protocol client}
|
---|
3 |
|
---|
4 | \declaremodule{standard}{nntplib}
|
---|
5 | \modulesynopsis{NNTP protocol client (requires sockets).}
|
---|
6 |
|
---|
7 | \indexii{NNTP}{protocol}
|
---|
8 | \index{Network News Transfer Protocol}
|
---|
9 |
|
---|
10 | This module defines the class \class{NNTP} which implements the client
|
---|
11 | side of the NNTP protocol. It can be used to implement a news reader
|
---|
12 | or poster, or automated news processors. For more information on NNTP
|
---|
13 | (Network News Transfer Protocol), see Internet \rfc{977}.
|
---|
14 |
|
---|
15 | Here are two small examples of how it can be used. To list some
|
---|
16 | statistics about a newsgroup and print the subjects of the last 10
|
---|
17 | articles:
|
---|
18 |
|
---|
19 | \begin{verbatim}
|
---|
20 | >>> s = NNTP('news.cwi.nl')
|
---|
21 | >>> resp, count, first, last, name = s.group('comp.lang.python')
|
---|
22 | >>> print 'Group', name, 'has', count, 'articles, range', first, 'to', last
|
---|
23 | Group comp.lang.python has 59 articles, range 3742 to 3803
|
---|
24 | >>> resp, subs = s.xhdr('subject', first + '-' + last)
|
---|
25 | >>> for id, sub in subs[-10:]: print id, sub
|
---|
26 | ...
|
---|
27 | 3792 Re: Removing elements from a list while iterating...
|
---|
28 | 3793 Re: Who likes Info files?
|
---|
29 | 3794 Emacs and doc strings
|
---|
30 | 3795 a few questions about the Mac implementation
|
---|
31 | 3796 Re: executable python scripts
|
---|
32 | 3797 Re: executable python scripts
|
---|
33 | 3798 Re: a few questions about the Mac implementation
|
---|
34 | 3799 Re: PROPOSAL: A Generic Python Object Interface for Python C Modules
|
---|
35 | 3802 Re: executable python scripts
|
---|
36 | 3803 Re: \POSIX{} wait and SIGCHLD
|
---|
37 | >>> s.quit()
|
---|
38 | '205 news.cwi.nl closing connection. Goodbye.'
|
---|
39 | \end{verbatim}
|
---|
40 |
|
---|
41 | To post an article from a file (this assumes that the article has
|
---|
42 | valid headers):
|
---|
43 |
|
---|
44 | \begin{verbatim}
|
---|
45 | >>> s = NNTP('news.cwi.nl')
|
---|
46 | >>> f = open('/tmp/article')
|
---|
47 | >>> s.post(f)
|
---|
48 | '240 Article posted successfully.'
|
---|
49 | >>> s.quit()
|
---|
50 | '205 news.cwi.nl closing connection. Goodbye.'
|
---|
51 | \end{verbatim}
|
---|
52 |
|
---|
53 | The module itself defines the following items:
|
---|
54 |
|
---|
55 | \begin{classdesc}{NNTP}{host\optional{, port
|
---|
56 | \optional{, user\optional{, password
|
---|
57 | \optional{, readermode}
|
---|
58 | \optional{, usenetrc}}}}}
|
---|
59 | Return a new instance of the \class{NNTP} class, representing a
|
---|
60 | connection to the NNTP server running on host \var{host}, listening at
|
---|
61 | port \var{port}. The default \var{port} is 119. If the optional
|
---|
62 | \var{user} and \var{password} are provided,
|
---|
63 | or if suitable credentials are present in \file{~/.netrc} and the
|
---|
64 | optional flag \var{usenetrc} is true (the default),
|
---|
65 | the \samp{AUTHINFO USER} and \samp{AUTHINFO PASS} commands are used to
|
---|
66 | identify and authenticate the user to the server. If the optional
|
---|
67 | flag \var{readermode} is true, then a \samp{mode reader} command is
|
---|
68 | sent before authentication is performed. Reader mode is sometimes
|
---|
69 | necessary if you are connecting to an NNTP server on the local machine
|
---|
70 | and intend to call reader-specific commands, such as \samp{group}. If
|
---|
71 | you get unexpected \exception{NNTPPermanentError}s, you might need to set
|
---|
72 | \var{readermode}. \var{readermode} defaults to \code{None}.
|
---|
73 | \var{usenetrc} defaults to \code{True}.
|
---|
74 |
|
---|
75 | \versionchanged[\var{usenetrc} argument added]{2.4}
|
---|
76 | \end{classdesc}
|
---|
77 |
|
---|
78 | \begin{excdesc}{NNTPError}
|
---|
79 | Derived from the standard exception \exception{Exception}, this is the
|
---|
80 | base class for all exceptions raised by the \module{nntplib} module.
|
---|
81 | \end{excdesc}
|
---|
82 |
|
---|
83 | \begin{excdesc}{NNTPReplyError}
|
---|
84 | Exception raised when an unexpected reply is received from the
|
---|
85 | server. For backwards compatibility, the exception \code{error_reply}
|
---|
86 | is equivalent to this class.
|
---|
87 | \end{excdesc}
|
---|
88 |
|
---|
89 | \begin{excdesc}{NNTPTemporaryError}
|
---|
90 | Exception raised when an error code in the range 400--499 is
|
---|
91 | received. For backwards compatibility, the exception
|
---|
92 | \code{error_temp} is equivalent to this class.
|
---|
93 | \end{excdesc}
|
---|
94 |
|
---|
95 | \begin{excdesc}{NNTPPermanentError}
|
---|
96 | Exception raised when an error code in the range 500--599 is
|
---|
97 | received. For backwards compatibility, the exception
|
---|
98 | \code{error_perm} is equivalent to this class.
|
---|
99 | \end{excdesc}
|
---|
100 |
|
---|
101 | \begin{excdesc}{NNTPProtocolError}
|
---|
102 | Exception raised when a reply is received from the server that does
|
---|
103 | not begin with a digit in the range 1--5. For backwards
|
---|
104 | compatibility, the exception \code{error_proto} is equivalent to this
|
---|
105 | class.
|
---|
106 | \end{excdesc}
|
---|
107 |
|
---|
108 | \begin{excdesc}{NNTPDataError}
|
---|
109 | Exception raised when there is some error in the response data. For
|
---|
110 | backwards compatibility, the exception \code{error_data} is
|
---|
111 | equivalent to this class.
|
---|
112 | \end{excdesc}
|
---|
113 |
|
---|
114 |
|
---|
115 | \subsection{NNTP Objects \label{nntp-objects}}
|
---|
116 |
|
---|
117 | NNTP instances have the following methods. The \var{response} that is
|
---|
118 | returned as the first item in the return tuple of almost all methods
|
---|
119 | is the server's response: a string beginning with a three-digit code.
|
---|
120 | If the server's response indicates an error, the method raises one of
|
---|
121 | the above exceptions.
|
---|
122 |
|
---|
123 |
|
---|
124 | \begin{methoddesc}{getwelcome}{}
|
---|
125 | Return the welcome message sent by the server in reply to the initial
|
---|
126 | connection. (This message sometimes contains disclaimers or help
|
---|
127 | information that may be relevant to the user.)
|
---|
128 | \end{methoddesc}
|
---|
129 |
|
---|
130 | \begin{methoddesc}{set_debuglevel}{level}
|
---|
131 | Set the instance's debugging level. This controls the amount of
|
---|
132 | debugging output printed. The default, \code{0}, produces no debugging
|
---|
133 | output. A value of \code{1} produces a moderate amount of debugging
|
---|
134 | output, generally a single line per request or response. A value of
|
---|
135 | \code{2} or higher produces the maximum amount of debugging output,
|
---|
136 | logging each line sent and received on the connection (including
|
---|
137 | message text).
|
---|
138 | \end{methoddesc}
|
---|
139 |
|
---|
140 | \begin{methoddesc}{newgroups}{date, time, \optional{file}}
|
---|
141 | Send a \samp{NEWGROUPS} command. The \var{date} argument should be a
|
---|
142 | string of the form \code{'\var{yy}\var{mm}\var{dd}'} indicating the
|
---|
143 | date, and \var{time} should be a string of the form
|
---|
144 | \code{'\var{hh}\var{mm}\var{ss}'} indicating the time. Return a pair
|
---|
145 | \code{(\var{response}, \var{groups})} where \var{groups} is a list of
|
---|
146 | group names that are new since the given date and time.
|
---|
147 | If the \var{file} parameter is supplied, then the output of the
|
---|
148 | \samp{NEWGROUPS} command is stored in a file. If \var{file} is a string,
|
---|
149 | then the method will open a file object with that name, write to it
|
---|
150 | then close it. If \var{file} is a file object, then it will start
|
---|
151 | calling \method{write()} on it to store the lines of the command output.
|
---|
152 | If \var{file} is supplied, then the returned \var{list} is an empty list.
|
---|
153 | \end{methoddesc}
|
---|
154 |
|
---|
155 | \begin{methoddesc}{newnews}{group, date, time, \optional{file}}
|
---|
156 | Send a \samp{NEWNEWS} command. Here, \var{group} is a group name or
|
---|
157 | \code{'*'}, and \var{date} and \var{time} have the same meaning as for
|
---|
158 | \method{newgroups()}. Return a pair \code{(\var{response},
|
---|
159 | \var{articles})} where \var{articles} is a list of message ids.
|
---|
160 | If the \var{file} parameter is supplied, then the output of the
|
---|
161 | \samp{NEWNEWS} command is stored in a file. If \var{file} is a string,
|
---|
162 | then the method will open a file object with that name, write to it
|
---|
163 | then close it. If \var{file} is a file object, then it will start
|
---|
164 | calling \method{write()} on it to store the lines of the command output.
|
---|
165 | If \var{file} is supplied, then the returned \var{list} is an empty list.
|
---|
166 | \end{methoddesc}
|
---|
167 |
|
---|
168 | \begin{methoddesc}{list}{\optional{file}}
|
---|
169 | Send a \samp{LIST} command. Return a pair \code{(\var{response},
|
---|
170 | \var{list})} where \var{list} is a list of tuples. Each tuple has the
|
---|
171 | form \code{(\var{group}, \var{last}, \var{first}, \var{flag})}, where
|
---|
172 | \var{group} is a group name, \var{last} and \var{first} are the last
|
---|
173 | and first article numbers (as strings), and \var{flag} is
|
---|
174 | \code{'y'} if posting is allowed, \code{'n'} if not, and \code{'m'} if
|
---|
175 | the newsgroup is moderated. (Note the ordering: \var{last},
|
---|
176 | \var{first}.)
|
---|
177 | If the \var{file} parameter is supplied, then the output of the
|
---|
178 | \samp{LIST} command is stored in a file. If \var{file} is a string,
|
---|
179 | then the method will open a file object with that name, write to it
|
---|
180 | then close it. If \var{file} is a file object, then it will start
|
---|
181 | calling \method{write()} on it to store the lines of the command output.
|
---|
182 | If \var{file} is supplied, then the returned \var{list} is an empty list.
|
---|
183 | \end{methoddesc}
|
---|
184 |
|
---|
185 | \begin{methoddesc}{descriptions}{grouppattern}
|
---|
186 | Send a \samp{LIST NEWSGROUPS} command, where \var{grouppattern} is a wildmat
|
---|
187 | string as specified in RFC2980 (it's essentially the same as DOS or UNIX
|
---|
188 | shell wildcard strings). Return a pair \code{(\var{response},
|
---|
189 | \var{list})}, where \var{list} is a list of tuples containing
|
---|
190 | \code{(\var{name}, \var{title})}.
|
---|
191 |
|
---|
192 | \versionadded{2.4}
|
---|
193 | \end{methoddesc}
|
---|
194 |
|
---|
195 | \begin{methoddesc}{description}{group}
|
---|
196 | Get a description for a single group \var{group}. If more than one group
|
---|
197 | matches (if 'group' is a real wildmat string), return the first match.
|
---|
198 | If no group matches, return an empty string.
|
---|
199 |
|
---|
200 | This elides the response code from the server. If the response code is
|
---|
201 | needed, use \method{descriptions()}.
|
---|
202 |
|
---|
203 | \versionadded{2.4}
|
---|
204 | \end{methoddesc}
|
---|
205 |
|
---|
206 | \begin{methoddesc}{group}{name}
|
---|
207 | Send a \samp{GROUP} command, where \var{name} is the group name.
|
---|
208 | Return a tuple \code{(\var{response}, \var{count}, \var{first},
|
---|
209 | \var{last}, \var{name})} where \var{count} is the (estimated) number
|
---|
210 | of articles in the group, \var{first} is the first article number in
|
---|
211 | the group, \var{last} is the last article number in the group, and
|
---|
212 | \var{name} is the group name. The numbers are returned as strings.
|
---|
213 | \end{methoddesc}
|
---|
214 |
|
---|
215 | \begin{methoddesc}{help}{\optional{file}}
|
---|
216 | Send a \samp{HELP} command. Return a pair \code{(\var{response},
|
---|
217 | \var{list})} where \var{list} is a list of help strings.
|
---|
218 | If the \var{file} parameter is supplied, then the output of the
|
---|
219 | \samp{HELP} command is stored in a file. If \var{file} is a string,
|
---|
220 | then the method will open a file object with that name, write to it
|
---|
221 | then close it. If \var{file} is a file object, then it will start
|
---|
222 | calling \method{write()} on it to store the lines of the command output.
|
---|
223 | If \var{file} is supplied, then the returned \var{list} is an empty list.
|
---|
224 | \end{methoddesc}
|
---|
225 |
|
---|
226 | \begin{methoddesc}{stat}{id}
|
---|
227 | Send a \samp{STAT} command, where \var{id} is the message id (enclosed
|
---|
228 | in \character{<} and \character{>}) or an article number (as a string).
|
---|
229 | Return a triple \code{(\var{response}, \var{number}, \var{id})} where
|
---|
230 | \var{number} is the article number (as a string) and \var{id} is the
|
---|
231 | message id (enclosed in \character{<} and \character{>}).
|
---|
232 | \end{methoddesc}
|
---|
233 |
|
---|
234 | \begin{methoddesc}{next}{}
|
---|
235 | Send a \samp{NEXT} command. Return as for \method{stat()}.
|
---|
236 | \end{methoddesc}
|
---|
237 |
|
---|
238 | \begin{methoddesc}{last}{}
|
---|
239 | Send a \samp{LAST} command. Return as for \method{stat()}.
|
---|
240 | \end{methoddesc}
|
---|
241 |
|
---|
242 | \begin{methoddesc}{head}{id}
|
---|
243 | Send a \samp{HEAD} command, where \var{id} has the same meaning as for
|
---|
244 | \method{stat()}. Return a tuple
|
---|
245 | \code{(\var{response}, \var{number}, \var{id}, \var{list})}
|
---|
246 | where the first three are the same as for \method{stat()},
|
---|
247 | and \var{list} is a list of the article's headers (an uninterpreted
|
---|
248 | list of lines, without trailing newlines).
|
---|
249 | \end{methoddesc}
|
---|
250 |
|
---|
251 | \begin{methoddesc}{body}{id,\optional{file}}
|
---|
252 | Send a \samp{BODY} command, where \var{id} has the same meaning as for
|
---|
253 | \method{stat()}. If the \var{file} parameter is supplied, then
|
---|
254 | the body is stored in a file. If \var{file} is a string, then
|
---|
255 | the method will open a file object with that name, write to it then close it.
|
---|
256 | If \var{file} is a file object, then it will start calling
|
---|
257 | \method{write()} on it to store the lines of the body.
|
---|
258 | Return as for \method{head()}. If \var{file} is supplied, then
|
---|
259 | the returned \var{list} is an empty list.
|
---|
260 | \end{methoddesc}
|
---|
261 |
|
---|
262 | \begin{methoddesc}{article}{id}
|
---|
263 | Send an \samp{ARTICLE} command, where \var{id} has the same meaning as
|
---|
264 | for \method{stat()}. Return as for \method{head()}.
|
---|
265 | \end{methoddesc}
|
---|
266 |
|
---|
267 | \begin{methoddesc}{slave}{}
|
---|
268 | Send a \samp{SLAVE} command. Return the server's \var{response}.
|
---|
269 | \end{methoddesc}
|
---|
270 |
|
---|
271 | \begin{methoddesc}{xhdr}{header, string, \optional{file}}
|
---|
272 | Send an \samp{XHDR} command. This command is not defined in the RFC
|
---|
273 | but is a common extension. The \var{header} argument is a header
|
---|
274 | keyword, e.g. \code{'subject'}. The \var{string} argument should have
|
---|
275 | the form \code{'\var{first}-\var{last}'} where \var{first} and
|
---|
276 | \var{last} are the first and last article numbers to search. Return a
|
---|
277 | pair \code{(\var{response}, \var{list})}, where \var{list} is a list of
|
---|
278 | pairs \code{(\var{id}, \var{text})}, where \var{id} is an article number
|
---|
279 | (as a string) and \var{text} is the text of the requested header for
|
---|
280 | that article.
|
---|
281 | If the \var{file} parameter is supplied, then the output of the
|
---|
282 | \samp{XHDR} command is stored in a file. If \var{file} is a string,
|
---|
283 | then the method will open a file object with that name, write to it
|
---|
284 | then close it. If \var{file} is a file object, then it will start
|
---|
285 | calling \method{write()} on it to store the lines of the command output.
|
---|
286 | If \var{file} is supplied, then the returned \var{list} is an empty list.
|
---|
287 | \end{methoddesc}
|
---|
288 |
|
---|
289 | \begin{methoddesc}{post}{file}
|
---|
290 | Post an article using the \samp{POST} command. The \var{file}
|
---|
291 | argument is an open file object which is read until EOF using its
|
---|
292 | \method{readline()} method. It should be a well-formed news article,
|
---|
293 | including the required headers. The \method{post()} method
|
---|
294 | automatically escapes lines beginning with \samp{.}.
|
---|
295 | \end{methoddesc}
|
---|
296 |
|
---|
297 | \begin{methoddesc}{ihave}{id, file}
|
---|
298 | Send an \samp{IHAVE} command. \var{id} is a message id (enclosed in
|
---|
299 | \character{<} and \character{>}).
|
---|
300 | If the response is not an error, treat
|
---|
301 | \var{file} exactly as for the \method{post()} method.
|
---|
302 | \end{methoddesc}
|
---|
303 |
|
---|
304 | \begin{methoddesc}{date}{}
|
---|
305 | Return a triple \code{(\var{response}, \var{date}, \var{time})},
|
---|
306 | containing the current date and time in a form suitable for the
|
---|
307 | \method{newnews()} and \method{newgroups()} methods.
|
---|
308 | This is an optional NNTP extension, and may not be supported by all
|
---|
309 | servers.
|
---|
310 | \end{methoddesc}
|
---|
311 |
|
---|
312 | \begin{methoddesc}{xgtitle}{name, \optional{file}}
|
---|
313 | Process an \samp{XGTITLE} command, returning a pair \code{(\var{response},
|
---|
314 | \var{list})}, where \var{list} is a list of tuples containing
|
---|
315 | \code{(\var{name}, \var{title})}.
|
---|
316 | % XXX huh? Should that be name, description?
|
---|
317 | If the \var{file} parameter is supplied, then the output of the
|
---|
318 | \samp{XGTITLE} command is stored in a file. If \var{file} is a string,
|
---|
319 | then the method will open a file object with that name, write to it
|
---|
320 | then close it. If \var{file} is a file object, then it will start
|
---|
321 | calling \method{write()} on it to store the lines of the command output.
|
---|
322 | If \var{file} is supplied, then the returned \var{list} is an empty list.
|
---|
323 | This is an optional NNTP extension, and may not be supported by all
|
---|
324 | servers.
|
---|
325 |
|
---|
326 | RFC2980 says ``It is suggested that this extension be deprecated''. Use
|
---|
327 | \method{descriptions()} or \method{description()} instead.
|
---|
328 | \end{methoddesc}
|
---|
329 |
|
---|
330 | \begin{methoddesc}{xover}{start, end, \optional{file}}
|
---|
331 | Return a pair \code{(\var{resp}, \var{list})}. \var{list} is a list
|
---|
332 | of tuples, one for each article in the range delimited by the \var{start}
|
---|
333 | and \var{end} article numbers. Each tuple is of the form
|
---|
334 | \code{(\var{article number}, \var{subject}, \var{poster}, \var{date},
|
---|
335 | \var{id}, \var{references}, \var{size}, \var{lines})}.
|
---|
336 | If the \var{file} parameter is supplied, then the output of the
|
---|
337 | \samp{XOVER} command is stored in a file. If \var{file} is a string,
|
---|
338 | then the method will open a file object with that name, write to it
|
---|
339 | then close it. If \var{file} is a file object, then it will start
|
---|
340 | calling \method{write()} on it to store the lines of the command output.
|
---|
341 | If \var{file} is supplied, then the returned \var{list} is an empty list.
|
---|
342 | This is an optional NNTP extension, and may not be supported by all
|
---|
343 | servers.
|
---|
344 | \end{methoddesc}
|
---|
345 |
|
---|
346 | \begin{methoddesc}{xpath}{id}
|
---|
347 | Return a pair \code{(\var{resp}, \var{path})}, where \var{path} is the
|
---|
348 | directory path to the article with message ID \var{id}. This is an
|
---|
349 | optional NNTP extension, and may not be supported by all servers.
|
---|
350 | \end{methoddesc}
|
---|
351 |
|
---|
352 | \begin{methoddesc}{quit}{}
|
---|
353 | Send a \samp{QUIT} command and close the connection. Once this method
|
---|
354 | has been called, no other methods of the NNTP object should be called.
|
---|
355 | \end{methoddesc}
|
---|