blob: f953072c084d10d5eb3620b233ed52da97d86615 [file] [log] [blame]
Guido van Rossum470be141995-03-17 16:07:09 +00001\section{Standard Module \sectcode{nntplib}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-nntplib}
Guido van Rossuma12ef941995-02-27 17:53:25 +00003\stmodindex{nntplib}
Guido van Rossum86751151995-02-28 17:14:32 +00004
5\renewcommand{\indexsubitem}{(in module nntplib)}
6
Guido van Rossumcca8d2b1995-03-22 15:48:46 +00007This module defines the class \code{NNTP} which implements the client
8side of the NNTP protocol. It can be used to implement a news reader
9or poster, or automated news processors. For more information on NNTP
10(Network News Transfer Protocol), see Internet RFC 977.
11
Guido van Rossum1b91cda1995-03-24 15:56:02 +000012Here are two small examples of how it can be used. To list some
13statistics about a newsgroup and print the subjects of the last 10
14articles:
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000015
Guido van Rossume47da0a1997-07-17 16:34:52 +000016\bcode\begin{verbatim}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000017>>> s = NNTP('news.cwi.nl')
18>>> resp, count, first, last, name = s.group('comp.lang.python')
19>>> print 'Group', name, 'has', count, 'articles, range', first, 'to', last
20Group comp.lang.python has 59 articles, range 3742 to 3803
21>>> resp, subs = s.xhdr('subject', first + '-' + last)
22>>> for id, sub in subs[-10:]: print id, sub
23...
243792 Re: Removing elements from a list while iterating...
253793 Re: Who likes Info files?
263794 Emacs and doc strings
273795 a few questions about the Mac implementation
283796 Re: executable python scripts
293797 Re: executable python scripts
303798 Re: a few questions about the Mac implementation
313799 Re: PROPOSAL: A Generic Python Object Interface for Python C Modules
323802 Re: executable python scripts
333803 Re: POSIX wait and SIGCHLD
34>>> s.quit()
35'205 news.cwi.nl closing connection. Goodbye.'
36>>>
Guido van Rossume47da0a1997-07-17 16:34:52 +000037\end{verbatim}\ecode
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000038
39To post an article from a file (this assumes that the article has
40valid headers):
41
Guido van Rossume47da0a1997-07-17 16:34:52 +000042\bcode\begin{verbatim}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000043>>> s = NNTP('news.cwi.nl')
44>>> f = open('/tmp/article')
45>>> s.post(f)
46'240 Article posted successfully.'
47>>> s.quit()
48'205 news.cwi.nl closing connection. Goodbye.'
49>>>
Guido van Rossume47da0a1997-07-17 16:34:52 +000050\end{verbatim}\ecode
51%
Guido van Rossum1b91cda1995-03-24 15:56:02 +000052The module itself defines the following items:
53
54\begin{funcdesc}{NNTP}{host\optional{\, port}}
55Return a new instance of the \code{NNTP} class, representing a
56connection to the NNTP server running on host \var{host}, listening at
57port \var{port}. The default \var{port} is 119.
58\end{funcdesc}
59
60\begin{excdesc}{error_reply}
61Exception raised when an unexpected reply is received from the server.
62\end{excdesc}
63
64\begin{excdesc}{error_temp}
65Exception raised when an error code in the range 400--499 is received.
66\end{excdesc}
67
68\begin{excdesc}{error_perm}
69Exception raised when an error code in the range 500--599 is received.
70\end{excdesc}
71
72\begin{excdesc}{error_proto}
73Exception raised when a reply is received from the server that does
74not begin with a digit in the range 1--5.
75\end{excdesc}
76
77\subsection{NNTP Objects}
78
79NNTP instances have the following methods. The \var{response} that is
80returned as the first item in the return tuple of almost all methods
81is the server's response: a string beginning with a three-digit code.
82If the server's response indicates an error, the method raises one of
83the above exceptions.
84
85\renewcommand{\indexsubitem}{(NNTP object method)}
86
87\begin{funcdesc}{getwelcome}{}
88Return the welcome message sent by the server in reply to the initial
89connection. (This message sometimes contains disclaimers or help
90information that may be relevant to the user.)
91\end{funcdesc}
92
93\begin{funcdesc}{set_debuglevel}{level}
94Set the instance's debugging level. This controls the amount of
95debugging output printed. The default, 0, produces no debugging
96output. A value of 1 produces a moderate amount of debugging output,
97generally a single line per request or response. A value of 2 or
98higher produces the maximum amount of debugging output, logging each
99line sent and received on the connection (including message text).
100\end{funcdesc}
101
102\begin{funcdesc}{newgroups}{date\, time}
103Send a \samp{NEWGROUPS} command. The \var{date} argument should be a
104string of the form \code{"\var{yy}\var{mm}\var{dd}"} indicating the
105date, and \var{time} should be a string of the form
106\code{"\var{hh}\var{mm}\var{ss}"} indicating the time. Return a pair
107\code{(\var{response}, \var{groups})} where \var{groups} is a list of
108group names that are new since the given date and time.
109\end{funcdesc}
110
111\begin{funcdesc}{newnews}{group\, date\, time}
112Send a \samp{NEWNEWS} command. Here, \var{group} is a group name or
113\code{"*"}, and \var{date} and \var{time} have the same meaning as for
114\code{newgroups()}. Return a pair \code{(\var{response},
115\var{articles})} where \var{articles} is a list of article ids.
116\end{funcdesc}
117
118\begin{funcdesc}{list}{}
119Send a \samp{LIST} command. Return a pair \code{(\var{response},
120\var{list})} where \var{list} is a list of tuples. Each tuple has the
121form \code{(\var{group}, \var{last}, \var{first}, \var{flag})}, where
122\var{group} is a group name, \var{last} and \var{first} are the last
123and first article numbers (as strings), and \var{flag} is \code{'y'}
124if posting is allowed, \code{'n'} if not, and \code{'m'} if the
125newsgroup is moderated. (Note the ordering: \var{last}, \var{first}.)
126\end{funcdesc}
127
128\begin{funcdesc}{group}{name}
129Send a \samp{GROUP} command, where \var{name} is the group name.
130Return a tuple \code{(\var{response}, \var{count}, \var{first},
131\var{last}, \var{name})} where \var{count} is the (estimated) number
132of articles in the group, \var{first} is the first article number in
133the group, \var{last} is the last article number in the group, and
134\var{name} is the group name. The numbers are returned as strings.
135\end{funcdesc}
136
137\begin{funcdesc}{help}{}
138Send a \samp{HELP} command. Return a pair \code{(\var{response},
139\var{list})} where \var{list} is a list of help strings.
140\end{funcdesc}
141
142\begin{funcdesc}{stat}{id}
143Send a \samp{STAT} command, where \var{id} is the message id (enclosed
144in \samp{<} and \samp{>}) or an article number (as a string).
Fred Drake4b3f0311996-12-13 22:04:31 +0000145Return a triple \code{(\var{response}, \var{number}, \var{id})} where
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000146\var{number} is the article number (as a string) and \var{id} is the
147article id (enclosed in \samp{<} and \samp{>}).
148\end{funcdesc}
149
150\begin{funcdesc}{next}{}
151Send a \samp{NEXT} command. Return as for \code{stat()}.
152\end{funcdesc}
153
154\begin{funcdesc}{last}{}
155Send a \samp{LAST} command. Return as for \code{stat()}.
156\end{funcdesc}
157
158\begin{funcdesc}{head}{id}
159Send a \samp{HEAD} command, where \var{id} has the same meaning as for
160\code{stat()}. Return a pair \code{(\var{response}, \var{list})}
161where \var{list} is a list of the article's headers (an uninterpreted
162list of lines, without trailing newlines).
163\end{funcdesc}
164
165\begin{funcdesc}{body}{id}
166Send a \samp{BODY} command, where \var{id} has the same meaning as for
167\code{stat()}. Return a pair \code{(\var{response}, \var{list})}
168where \var{list} is a list of the article's body text (an
169uninterpreted list of lines, without trailing newlines).
170\end{funcdesc}
171
172\begin{funcdesc}{article}{id}
173Send a \samp{ARTICLE} command, where \var{id} has the same meaning as
174for \code{stat()}. Return a pair \code{(\var{response}, \var{list})}
175where \var{list} is a list of the article's header and body text (an
176uninterpreted list of lines, without trailing newlines).
177\end{funcdesc}
178
179\begin{funcdesc}{slave}{}
180Send a \samp{SLAVE} command. Return the server's \var{response}.
181\end{funcdesc}
182
183\begin{funcdesc}{xhdr}{header\, string}
184Send an \samp{XHDR} command. This command is not defined in the RFC
185but is a common extension. The \var{header} argument is a header
186keyword, e.g. \code{"subject"}. The \var{string} argument should have
187the form \code{"\var{first}-\var{last}"} where \var{first} and
188\var{last} are the first and last article numbers to search. Return a
189pair \code{(\var{response}, \var{list})}, where \var{list} is a list of
190pairs \code{(\var{id}, \var{text})}, where \var{id} is an article id
191(as a string) and \var{text} is the text of the requested header for
192that article.
193\end{funcdesc}
194
195\begin{funcdesc}{post}{file}
196Post an article using the \samp{POST} command. The \var{file}
197argument is an open file object which is read until EOF using its
198\code{readline()} method. It should be a well-formed news article,
199including the required headers. The \code{post()} method
200automatically escapes lines beginning with \samp{.}.
201\end{funcdesc}
202
203\begin{funcdesc}{ihave}{id\, file}
204Send an \samp{IHAVE} command. If the response is not an error, treat
205\var{file} exactly as for the \code{post()} method.
206\end{funcdesc}
207
Guido van Rossum94adab51997-06-02 17:27:50 +0000208\begin{funcdesc}{date}{}
209Return a triple \code{(\var{response}, \var{date}, \var{time})},
210containing the current date and time in a form suitable for the
211\code{newnews} and \code{newgroups} methods.
212This is an optional NNTP extension, and may not be supported by all
213servers.
214\end{funcdesc}
215
216\begin{funcdesc}{xgtitle}{name}
217Process an XGTITLE command, returning a pair \code{(\var{response},
218\var{list}}, where \var{list} is a list of tuples containing
219\code{(\var{name}, \var{title})}.
220% XXX huh? Should that be name, description?
221This is an optional NNTP extension, and may not be supported by all
222servers.
223\end{funcdesc}
224
225\begin{funcdesc}{xover}{start\, end}
226Return a pair \code{(\var{resp}, \var{list})}. \var{list} is a list
227of tuples, one for each article in the range delimited by the \var{start}
228and \var{end} article numbers. Each tuple is of the form
229\code{(\var{article number}, \var{subject}, \var{poster}, \var{date}, \var{id}, \var{references}, \var{size}, \var{lines})}.
230This is an optional NNTP extension, and may not be supported by all
231servers.
232\end{funcdesc}
233
234\begin{funcdesc}{xpath}{id}
235Return a pair \code{(\var{resp}, \var{path})}, where \var{path} is the
236directory path to the article with message ID \var{id}. This is an
237optional NNTP extension, and may not be supported by all servers.
238\end{funcdesc}
239
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000240\begin{funcdesc}{quit}{}
241Send a \samp{QUIT} command and close the connection. Once this method
242has been called, no other methods of the NNTP object should be called.
243\end{funcdesc}