libnntplib.tex 10.9 KB
Newer Older
Fred Drake's avatar
Fred Drake committed
1
\section{\module{nntplib} ---
Fred Drake's avatar
Fred Drake committed
2
         NNTP protocol client}
3

Fred Drake's avatar
Fred Drake committed
4
\declaremodule{standard}{nntplib}
5 6
\modulesynopsis{NNTP protocol client (requires sockets).}

Fred Drake's avatar
Fred Drake committed
7
\indexii{NNTP}{protocol}
Fred Drake's avatar
Fred Drake committed
8
\index{Network News Transfer Protocol}
9

10
This module defines the class \class{NNTP} which implements the client
Guido van Rossum's avatar
Guido van Rossum committed
11 12
side of the NNTP protocol.  It can be used to implement a news reader
or poster, or automated news processors.  For more information on NNTP
13
(Network News Transfer Protocol), see Internet \rfc{977}.
Guido van Rossum's avatar
Guido van Rossum committed
14

Guido van Rossum's avatar
Guido van Rossum committed
15 16 17
Here are two small examples of how it can be used.  To list some
statistics about a newsgroup and print the subjects of the last 10
articles:
Guido van Rossum's avatar
Guido van Rossum committed
18

19
\begin{verbatim}
Guido van Rossum's avatar
Guido van Rossum committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
>>> s = NNTP('news.cwi.nl')
>>> resp, count, first, last, name = s.group('comp.lang.python')
>>> print 'Group', name, 'has', count, 'articles, range', first, 'to', last
Group comp.lang.python has 59 articles, range 3742 to 3803
>>> resp, subs = s.xhdr('subject', first + '-' + last)
>>> for id, sub in subs[-10:]: print id, sub
... 
3792 Re: Removing elements from a list while iterating...
3793 Re: Who likes Info files?
3794 Emacs and doc strings
3795 a few questions about the Mac implementation
3796 Re: executable python scripts
3797 Re: executable python scripts
3798 Re: a few questions about the Mac implementation 
3799 Re: PROPOSAL: A Generic Python Object Interface for Python C Modules
3802 Re: executable python scripts 
36
3803 Re: \POSIX{} wait and SIGCHLD
Guido van Rossum's avatar
Guido van Rossum committed
37 38
>>> s.quit()
'205 news.cwi.nl closing connection.  Goodbye.'
39
\end{verbatim}
Guido van Rossum's avatar
Guido van Rossum committed
40 41 42 43

To post an article from a file (this assumes that the article has
valid headers):

44
\begin{verbatim}
Guido van Rossum's avatar
Guido van Rossum committed
45 46 47 48 49 50
>>> s = NNTP('news.cwi.nl')
>>> f = open('/tmp/article')
>>> s.post(f)
'240 Article posted successfully.'
>>> s.quit()
'205 news.cwi.nl closing connection.  Goodbye.'
51
\end{verbatim}
Fred Drake's avatar
Fred Drake committed
52

Guido van Rossum's avatar
Guido van Rossum committed
53 54
The module itself defines the following items:

55
\begin{classdesc}{NNTP}{host\optional{, port
56 57
                        \optional{, user\optional{, password
			\optional{, readermode}}}}}
58
Return a new instance of the \class{NNTP} class, representing a
Guido van Rossum's avatar
Guido van Rossum committed
59
connection to the NNTP server running on host \var{host}, listening at
60
port \var{port}.  The default \var{port} is 119.  If the optional
Fred Drake's avatar
Fred Drake committed
61 62
\var{user} and \var{password} are provided, the
\samp{AUTHINFO USER} and \samp{AUTHINFO PASS} commands are used to
63 64 65 66 67 68 69
identify and authenticate the user to the server.  If the optional
flag \var{readermode} is true, then a \samp{mode reader} command is
sent before authentication is performed.  Reader mode is sometimes
necessary if you are connecting to an NNTP server on the local machine
and intend to call reader-specific commands, such as \samp{group}.  If
you get unexpected \code{NNTPPermanentError}s, you might need to set
\var{readermode}.  \var{readermode} defaults to \code{None}.
70
\end{classdesc}
Guido van Rossum's avatar
Guido van Rossum committed
71

72 73 74 75 76 77 78 79 80 81
\begin{classdesc}{NNTPError}{}
Derived from the standard exception \code{Exception}, this is the base
class for all exceptions raised by the \code{nntplib} module.
\end{classdesc}

\begin{classdesc}{NNTPReplyError}{}
Exception raised when an unexpected reply is received from the
server.  For backwards compatibility, the exception \code{error_reply}
is equivalent to this class.
\end{classdesc}
Guido van Rossum's avatar
Guido van Rossum committed
82

83 84 85 86 87
\begin{classdesc}{NNTPTemporaryError}{}
Exception raised when an error code in the range 400--499 is
received.  For backwards compatibility, the exception
\code{error_temp} is equivalent to this class.
\end{classdesc}
Guido van Rossum's avatar
Guido van Rossum committed
88

89 90 91 92 93
\begin{classdesc}{NNTPPermanentError}{}
Exception raised when an error code in the range 500--599 is
received.  For backwards compatibility, the exception
\code{error_perm} is equivalent to this class.
\end{classdesc}
Guido van Rossum's avatar
Guido van Rossum committed
94

95
\begin{classdesc}{NNTPProtocolError}{}
Guido van Rossum's avatar
Guido van Rossum committed
96
Exception raised when a reply is received from the server that does
97 98 99 100 101 102 103 104 105 106
not begin with a digit in the range 1--5.  For backwards
compatibility, the exception \code{error_proto} is equivalent to this
class.
\end{classdesc}

\begin{classdesc}{NNTPDataError}{}
Exception raised when there is some error in the response data.  For
backwards compatibility, the exception \code{error_data} is
equivalent to this class.
\end{classdesc}
Guido van Rossum's avatar
Guido van Rossum committed
107

108

Fred Drake's avatar
Fred Drake committed
109
\subsection{NNTP Objects \label{nntp-objects}}
Guido van Rossum's avatar
Guido van Rossum committed
110 111 112 113 114 115 116 117

NNTP instances have the following methods.  The \var{response} that is
returned as the first item in the return tuple of almost all methods
is the server's response: a string beginning with a three-digit code.
If the server's response indicates an error, the method raises one of
the above exceptions.


118
\begin{methoddesc}{getwelcome}{}
Guido van Rossum's avatar
Guido van Rossum committed
119 120 121
Return the welcome message sent by the server in reply to the initial
connection.  (This message sometimes contains disclaimers or help
information that may be relevant to the user.)
122
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
123

124
\begin{methoddesc}{set_debuglevel}{level}
Guido van Rossum's avatar
Guido van Rossum committed
125
Set the instance's debugging level.  This controls the amount of
126 127 128 129 130 131 132 133 134
debugging output printed.  The default, \code{0}, produces no debugging
output.  A value of \code{1} produces a moderate amount of debugging
output, generally a single line per request or response.  A value of
\code{2} or higher produces the maximum amount of debugging output,
logging each line sent and received on the connection (including
message text).
\end{methoddesc}

\begin{methoddesc}{newgroups}{date, time}
Guido van Rossum's avatar
Guido van Rossum committed
135
Send a \samp{NEWGROUPS} command.  The \var{date} argument should be a
Fred Drake's avatar
Fred Drake committed
136
string of the form \code{'\var{yy}\var{mm}\var{dd}'} indicating the
Guido van Rossum's avatar
Guido van Rossum committed
137
date, and \var{time} should be a string of the form
Fred Drake's avatar
Fred Drake committed
138
\code{'\var{hh}\var{mm}\var{ss}'} indicating the time.  Return a pair
Guido van Rossum's avatar
Guido van Rossum committed
139 140
\code{(\var{response}, \var{groups})} where \var{groups} is a list of
group names that are new since the given date and time.
141
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
142

143
\begin{methoddesc}{newnews}{group, date, time}
Guido van Rossum's avatar
Guido van Rossum committed
144
Send a \samp{NEWNEWS} command.  Here, \var{group} is a group name or
145 146
\code{'*'}, and \var{date} and \var{time} have the same meaning as for
\method{newgroups()}.  Return a pair \code{(\var{response},
Guido van Rossum's avatar
Guido van Rossum committed
147
\var{articles})} where \var{articles} is a list of article ids.
148
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
149

150
\begin{methoddesc}{list}{}
Guido van Rossum's avatar
Guido van Rossum committed
151 152 153 154
Send a \samp{LIST} command.  Return a pair \code{(\var{response},
\var{list})} where \var{list} is a list of tuples.  Each tuple has the
form \code{(\var{group}, \var{last}, \var{first}, \var{flag})}, where
\var{group} is a group name, \var{last} and \var{first} are the last
Fred Drake's avatar
Fred Drake committed
155 156 157 158
and first article numbers (as strings), and \var{flag} is
\code{'y'} if posting is allowed, \code{'n'} if not, and \code{'m'} if
the newsgroup is moderated.  (Note the ordering: \var{last},
\var{first}.)
159
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
160

161
\begin{methoddesc}{group}{name}
Guido van Rossum's avatar
Guido van Rossum committed
162
Send a \samp{GROUP} command, where \var{name} is the group name.
Fred Drake's avatar
Fred Drake committed
163 164 165 166 167
Return a tuple \code{(\var{response}, \var{count}, \var{first},
\var{last}, \var{name})} where \var{count} is the (estimated) number
of articles in the group, \var{first} is the first article number in
the group, \var{last} is the last article number in the group, and
\var{name} is the group name.  The numbers are returned as strings.
168 169 170
\end{methoddesc}

\begin{methoddesc}{help}{}
Guido van Rossum's avatar
Guido van Rossum committed
171 172
Send a \samp{HELP} command.  Return a pair \code{(\var{response},
\var{list})} where \var{list} is a list of help strings.
173
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
174

175
\begin{methoddesc}{stat}{id}
Guido van Rossum's avatar
Guido van Rossum committed
176
Send a \samp{STAT} command, where \var{id} is the message id (enclosed
177
in \character{<} and \character{>}) or an article number (as a string).
178
Return a triple \code{(\var{response}, \var{number}, \var{id})} where
Guido van Rossum's avatar
Guido van Rossum committed
179
\var{number} is the article number (as a string) and \var{id} is the
180 181
article id  (enclosed in \character{<} and \character{>}).
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
182

183 184 185
\begin{methoddesc}{next}{}
Send a \samp{NEXT} command.  Return as for \method{stat()}.
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
186

187 188 189
\begin{methoddesc}{last}{}
Send a \samp{LAST} command.  Return as for \method{stat()}.
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
190

191
\begin{methoddesc}{head}{id}
Guido van Rossum's avatar
Guido van Rossum committed
192
Send a \samp{HEAD} command, where \var{id} has the same meaning as for
193 194 195 196
\method{stat()}.  Return a tuple
\code{(\var{response}, \var{number}, \var{id}, \var{list})}
where the first three are the same as for \method{stat()},
and \var{list} is a list of the article's headers (an uninterpreted
Guido van Rossum's avatar
Guido van Rossum committed
197
list of lines, without trailing newlines).
198
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
199

200
\begin{methoddesc}{body}{id}
Guido van Rossum's avatar
Guido van Rossum committed
201
Send a \samp{BODY} command, where \var{id} has the same meaning as for
202
\method{stat()}.  Return as for \method{head()}.
203
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
204

205
\begin{methoddesc}{article}{id}
Guido van Rossum's avatar
Guido van Rossum committed
206
Send a \samp{ARTICLE} command, where \var{id} has the same meaning as
207
for \method{stat()}.  Return as for \method{head()}.
208
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
209

210
\begin{methoddesc}{slave}{}
Guido van Rossum's avatar
Guido van Rossum committed
211
Send a \samp{SLAVE} command.  Return the server's \var{response}.
212
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
213

214
\begin{methoddesc}{xhdr}{header, string}
Guido van Rossum's avatar
Guido van Rossum committed
215 216
Send an \samp{XHDR} command.  This command is not defined in the RFC
but is a common extension.  The \var{header} argument is a header
217
keyword, e.g. \code{'subject'}.  The \var{string} argument should have
Fred Drake's avatar
Fred Drake committed
218
the form \code{'\var{first}-\var{last}'} where \var{first} and
Guido van Rossum's avatar
Guido van Rossum committed
219 220 221 222 223
\var{last} are the first and last article numbers to search.  Return a
pair \code{(\var{response}, \var{list})}, where \var{list} is a list of
pairs \code{(\var{id}, \var{text})}, where \var{id} is an article id
(as a string) and \var{text} is the text of the requested header for
that article.
224
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
225

226
\begin{methoddesc}{post}{file}
Guido van Rossum's avatar
Guido van Rossum committed
227 228
Post an article using the \samp{POST} command.  The \var{file}
argument is an open file object which is read until EOF using its
229 230
\method{readline()} method.  It should be a well-formed news article,
including the required headers.  The \method{post()} method
Guido van Rossum's avatar
Guido van Rossum committed
231
automatically escapes lines beginning with \samp{.}.
232
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
233

234
\begin{methoddesc}{ihave}{id, file}
Guido van Rossum's avatar
Guido van Rossum committed
235
Send an \samp{IHAVE} command.  If the response is not an error, treat
236 237
\var{file} exactly as for the \method{post()} method.
\end{methoddesc}
Guido van Rossum's avatar
Guido van Rossum committed
238

239
\begin{methoddesc}{date}{}
240 241
Return a triple \code{(\var{response}, \var{date}, \var{time})},
containing the current date and time in a form suitable for the
242
\method{newnews()} and \method{newgroups()} methods.
243 244
This is an optional NNTP extension, and may not be supported by all
servers.
245
\end{methoddesc}
246

247 248
\begin{methoddesc}{xgtitle}{name}
Process an \samp{XGTITLE} command, returning a pair \code{(\var{response},
249
\var{list})}, where \var{list} is a list of tuples containing
250 251 252 253
\code{(\var{name}, \var{title})}.
% XXX huh?  Should that be name, description?
This is an optional NNTP extension, and may not be supported by all
servers.
254
\end{methoddesc}
255

256
\begin{methoddesc}{xover}{start, end}
257 258 259
Return a pair \code{(\var{resp}, \var{list})}.  \var{list} is a list
of tuples, one for each article in the range delimited by the \var{start}
and \var{end} article numbers.  Each tuple is of the form
Fred Drake's avatar
Fred Drake committed
260 261
\code{(\var{article number}, \var{subject}, \var{poster}, \var{date},
\var{id}, \var{references}, \var{size}, \var{lines})}.
262 263
This is an optional NNTP extension, and may not be supported by all
servers.
264
\end{methoddesc}
265

266
\begin{methoddesc}{xpath}{id}
267 268 269
Return a pair \code{(\var{resp}, \var{path})}, where \var{path} is the
directory path to the article with message ID \var{id}.  This is an
optional NNTP extension, and may not be supported by all servers.
270
\end{methoddesc}
271

272
\begin{methoddesc}{quit}{}
Guido van Rossum's avatar
Guido van Rossum committed
273 274
Send a \samp{QUIT} command and close the connection.  Once this method
has been called, no other methods of the NNTP object should be called.
275
\end{methoddesc}