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