blob: b02f142d0d8d7fdb51fcb246e087d965fb913ddd [file] [log] [blame]
Fred Drakefc576191998-04-04 07:15:02 +00001\section{Standard Module \module{nntplib}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-nntplib}
Guido van Rossuma12ef941995-02-27 17:53:25 +00003\stmodindex{nntplib}
Fred Drake6279fcc1998-01-07 13:23:32 +00004\indexii{NNTP}{protocol}
Fred Drake3f6034d1998-07-02 19:33:43 +00005\index{Network News Transfer Protocol}
Guido van Rossum86751151995-02-28 17:14:32 +00006
Guido van Rossum86751151995-02-28 17:14:32 +00007
Fred Drakefc576191998-04-04 07:15:02 +00008This module defines the class \class{NNTP} which implements the client
Guido van Rossumcca8d2b1995-03-22 15:48:46 +00009side of the NNTP protocol. It can be used to implement a news reader
10or poster, or automated news processors. For more information on NNTP
Fred Drakec5891241998-02-09 19:16:20 +000011(Network News Transfer Protocol), see Internet \rfc{977}.
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000012
Guido van Rossum1b91cda1995-03-24 15:56:02 +000013Here are two small examples of how it can be used. To list some
14statistics about a newsgroup and print the subjects of the last 10
15articles:
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000016
Fred Drake19479911998-02-13 06:58:54 +000017\begin{verbatim}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000018>>> s = NNTP('news.cwi.nl')
19>>> resp, count, first, last, name = s.group('comp.lang.python')
20>>> print 'Group', name, 'has', count, 'articles, range', first, 'to', last
21Group comp.lang.python has 59 articles, range 3742 to 3803
22>>> resp, subs = s.xhdr('subject', first + '-' + last)
23>>> for id, sub in subs[-10:]: print id, sub
24...
253792 Re: Removing elements from a list while iterating...
263793 Re: Who likes Info files?
273794 Emacs and doc strings
283795 a few questions about the Mac implementation
293796 Re: executable python scripts
303797 Re: executable python scripts
313798 Re: a few questions about the Mac implementation
323799 Re: PROPOSAL: A Generic Python Object Interface for Python C Modules
333802 Re: executable python scripts
Fred Drake65b32f71998-02-09 20:27:12 +0000343803 Re: \POSIX{} wait and SIGCHLD
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000035>>> s.quit()
36'205 news.cwi.nl closing connection. Goodbye.'
Fred Drake19479911998-02-13 06:58:54 +000037\end{verbatim}
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
Fred Drake19479911998-02-13 06:58:54 +000042\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.'
Fred Drake19479911998-02-13 06:58:54 +000049\end{verbatim}
Guido van Rossume47da0a1997-07-17 16:34:52 +000050%
Guido van Rossum1b91cda1995-03-24 15:56:02 +000051The module itself defines the following items:
52
Fred Drakefc576191998-04-04 07:15:02 +000053\begin{classdesc}{NNTP}{host\optional{, port}}
54Return a new instance of the \class{NNTP} class, representing a
Guido van Rossum1b91cda1995-03-24 15:56:02 +000055connection to the NNTP server running on host \var{host}, listening at
56port \var{port}. The default \var{port} is 119.
Fred Drakefc576191998-04-04 07:15:02 +000057\end{classdesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +000058
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
Fred Drakefc576191998-04-04 07:15:02 +000076
Guido van Rossum1b91cda1995-03-24 15:56:02 +000077\subsection{NNTP Objects}
Fred Drakefc576191998-04-04 07:15:02 +000078\label{nntp-objects}
Guido van Rossum1b91cda1995-03-24 15:56:02 +000079
80NNTP instances have the following methods. The \var{response} that is
81returned as the first item in the return tuple of almost all methods
82is the server's response: a string beginning with a three-digit code.
83If the server's response indicates an error, the method raises one of
84the above exceptions.
85
Guido van Rossum1b91cda1995-03-24 15:56:02 +000086
Fred Drakefc576191998-04-04 07:15:02 +000087\begin{methoddesc}{getwelcome}{}
Guido van Rossum1b91cda1995-03-24 15:56:02 +000088Return 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.)
Fred Drakefc576191998-04-04 07:15:02 +000091\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +000092
Fred Drakefc576191998-04-04 07:15:02 +000093\begin{methoddesc}{set_debuglevel}{level}
Guido van Rossum1b91cda1995-03-24 15:56:02 +000094Set the instance's debugging level. This controls the amount of
Fred Drakefc576191998-04-04 07:15:02 +000095debugging output printed. The default, \code{0}, produces no debugging
96output. A value of \code{1} produces a moderate amount of debugging
97output, generally a single line per request or response. A value of
98\code{2} or higher produces the maximum amount of debugging output,
99logging each line sent and received on the connection (including
100message text).
101\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000102
Fred Drakefc576191998-04-04 07:15:02 +0000103\begin{methoddesc}{newgroups}{date, time}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000104Send a \samp{NEWGROUPS} command. The \var{date} argument should be a
105string of the form \code{"\var{yy}\var{mm}\var{dd}"} indicating the
106date, and \var{time} should be a string of the form
107\code{"\var{hh}\var{mm}\var{ss}"} indicating the time. Return a pair
108\code{(\var{response}, \var{groups})} where \var{groups} is a list of
109group names that are new since the given date and time.
Fred Drakefc576191998-04-04 07:15:02 +0000110\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000111
Fred Drakefc576191998-04-04 07:15:02 +0000112\begin{methoddesc}{newnews}{group, date, time}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000113Send a \samp{NEWNEWS} command. Here, \var{group} is a group name or
Fred Drakefc576191998-04-04 07:15:02 +0000114\code{'*'}, and \var{date} and \var{time} have the same meaning as for
115\method{newgroups()}. Return a pair \code{(\var{response},
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000116\var{articles})} where \var{articles} is a list of article ids.
Fred Drakefc576191998-04-04 07:15:02 +0000117\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000118
Fred Drakefc576191998-04-04 07:15:02 +0000119\begin{methoddesc}{list}{}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000120Send a \samp{LIST} command. Return a pair \code{(\var{response},
121\var{list})} where \var{list} is a list of tuples. Each tuple has the
122form \code{(\var{group}, \var{last}, \var{first}, \var{flag})}, where
123\var{group} is a group name, \var{last} and \var{first} are the last
124and first article numbers (as strings), and \var{flag} is \code{'y'}
125if posting is allowed, \code{'n'} if not, and \code{'m'} if the
126newsgroup is moderated. (Note the ordering: \var{last}, \var{first}.)
Fred Drakefc576191998-04-04 07:15:02 +0000127\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000128
Fred Drakefc576191998-04-04 07:15:02 +0000129\begin{methoddesc}{group}{name}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000130Send a \samp{GROUP} command, where \var{name} is the group name.
Fred Drakefc576191998-04-04 07:15:02 +0000131Return a tuple \code{(}\var{response}\code{,} \var{count}\code{,}
132\var{first}\code{,} \var{last}\code{,} \var{name}\code{)} where
133\var{count} is the (estimated) number of articles in the group,
134\var{first} is the first article number in the group, \var{last} is
135the last article number in the group, and \var{name} is the group
136name. The numbers are returned as strings.
137\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000138
Fred Drakefc576191998-04-04 07:15:02 +0000139\begin{methoddesc}{help}{}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000140Send a \samp{HELP} command. Return a pair \code{(\var{response},
141\var{list})} where \var{list} is a list of help strings.
Fred Drakefc576191998-04-04 07:15:02 +0000142\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000143
Fred Drakefc576191998-04-04 07:15:02 +0000144\begin{methoddesc}{stat}{id}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000145Send a \samp{STAT} command, where \var{id} is the message id (enclosed
Fred Drakefc576191998-04-04 07:15:02 +0000146in \character{<} and \character{>}) or an article number (as a string).
Fred Drake4b3f0311996-12-13 22:04:31 +0000147Return a triple \code{(\var{response}, \var{number}, \var{id})} where
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000148\var{number} is the article number (as a string) and \var{id} is the
Fred Drakefc576191998-04-04 07:15:02 +0000149article id (enclosed in \character{<} and \character{>}).
150\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000151
Fred Drakefc576191998-04-04 07:15:02 +0000152\begin{methoddesc}{next}{}
153Send a \samp{NEXT} command. Return as for \method{stat()}.
154\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000155
Fred Drakefc576191998-04-04 07:15:02 +0000156\begin{methoddesc}{last}{}
157Send a \samp{LAST} command. Return as for \method{stat()}.
158\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000159
Fred Drakefc576191998-04-04 07:15:02 +0000160\begin{methoddesc}{head}{id}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000161Send a \samp{HEAD} command, where \var{id} has the same meaning as for
Guido van Rossumcd905091998-06-30 14:53:41 +0000162\method{stat()}. Return a tuple
163\code{(\var{response}, \var{number}, \var{id}, \var{list})}
164where the first three are the same as for \method{stat()},
165and \var{list} is a list of the article's headers (an uninterpreted
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000166list of lines, without trailing newlines).
Fred Drakefc576191998-04-04 07:15:02 +0000167\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000168
Fred Drakefc576191998-04-04 07:15:02 +0000169\begin{methoddesc}{body}{id}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000170Send a \samp{BODY} command, where \var{id} has the same meaning as for
Guido van Rossumcd905091998-06-30 14:53:41 +0000171\method{stat()}. Return as for \method{head()}.
Fred Drakefc576191998-04-04 07:15:02 +0000172\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000173
Fred Drakefc576191998-04-04 07:15:02 +0000174\begin{methoddesc}{article}{id}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000175Send a \samp{ARTICLE} command, where \var{id} has the same meaning as
Guido van Rossumcd905091998-06-30 14:53:41 +0000176for \method{stat()}. Return as for \method{head()}.
Fred Drakefc576191998-04-04 07:15:02 +0000177\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000178
Fred Drakefc576191998-04-04 07:15:02 +0000179\begin{methoddesc}{slave}{}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000180Send a \samp{SLAVE} command. Return the server's \var{response}.
Fred Drakefc576191998-04-04 07:15:02 +0000181\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000182
Fred Drakefc576191998-04-04 07:15:02 +0000183\begin{methoddesc}{xhdr}{header, string}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000184Send an \samp{XHDR} command. This command is not defined in the RFC
185but is a common extension. The \var{header} argument is a header
Fred Drakefc576191998-04-04 07:15:02 +0000186keyword, e.g. \code{'subject'}. The \var{string} argument should have
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000187the 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.
Fred Drakefc576191998-04-04 07:15:02 +0000193\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000194
Fred Drakefc576191998-04-04 07:15:02 +0000195\begin{methoddesc}{post}{file}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000196Post an article using the \samp{POST} command. The \var{file}
197argument is an open file object which is read until EOF using its
Fred Drakefc576191998-04-04 07:15:02 +0000198\method{readline()} method. It should be a well-formed news article,
199including the required headers. The \method{post()} method
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000200automatically escapes lines beginning with \samp{.}.
Fred Drakefc576191998-04-04 07:15:02 +0000201\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000202
Fred Drakefc576191998-04-04 07:15:02 +0000203\begin{methoddesc}{ihave}{id, file}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000204Send an \samp{IHAVE} command. If the response is not an error, treat
Fred Drakefc576191998-04-04 07:15:02 +0000205\var{file} exactly as for the \method{post()} method.
206\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000207
Fred Drakefc576191998-04-04 07:15:02 +0000208\begin{methoddesc}{date}{}
Guido van Rossum94adab51997-06-02 17:27:50 +0000209Return a triple \code{(\var{response}, \var{date}, \var{time})},
210containing the current date and time in a form suitable for the
Fred Drakefc576191998-04-04 07:15:02 +0000211\method{newnews()} and \method{newgroups()} methods.
Guido van Rossum94adab51997-06-02 17:27:50 +0000212This is an optional NNTP extension, and may not be supported by all
213servers.
Fred Drakefc576191998-04-04 07:15:02 +0000214\end{methoddesc}
Guido van Rossum94adab51997-06-02 17:27:50 +0000215
Fred Drakefc576191998-04-04 07:15:02 +0000216\begin{methoddesc}{xgtitle}{name}
217Process an \samp{XGTITLE} command, returning a pair \code{(\var{response},
Fred Drakefac431e1998-02-16 21:57:37 +0000218\var{list})}, where \var{list} is a list of tuples containing
Guido van Rossum94adab51997-06-02 17:27:50 +0000219\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.
Fred Drakefc576191998-04-04 07:15:02 +0000223\end{methoddesc}
Guido van Rossum94adab51997-06-02 17:27:50 +0000224
Fred Drakefc576191998-04-04 07:15:02 +0000225\begin{methoddesc}{xover}{start, end}
Guido van Rossum94adab51997-06-02 17:27:50 +0000226Return 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
Fred Drakefac431e1998-02-16 21:57:37 +0000229\code{(}\var{article number}\code{,} \var{subject}\code{,}
230\var{poster}\code{,} \var{date}\code{,} \var{id}\code{,}
231\var{references}\code{,} \var{size}\code{,} \var{lines}\code{)}.
Guido van Rossum94adab51997-06-02 17:27:50 +0000232This is an optional NNTP extension, and may not be supported by all
233servers.
Fred Drakefc576191998-04-04 07:15:02 +0000234\end{methoddesc}
Guido van Rossum94adab51997-06-02 17:27:50 +0000235
Fred Drakefc576191998-04-04 07:15:02 +0000236\begin{methoddesc}{xpath}{id}
Guido van Rossum94adab51997-06-02 17:27:50 +0000237Return a pair \code{(\var{resp}, \var{path})}, where \var{path} is the
238directory path to the article with message ID \var{id}. This is an
239optional NNTP extension, and may not be supported by all servers.
Fred Drakefc576191998-04-04 07:15:02 +0000240\end{methoddesc}
Guido van Rossum94adab51997-06-02 17:27:50 +0000241
Fred Drakefc576191998-04-04 07:15:02 +0000242\begin{methoddesc}{quit}{}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000243Send a \samp{QUIT} command and close the connection. Once this method
244has been called, no other methods of the NNTP object should be called.
Fred Drakefc576191998-04-04 07:15:02 +0000245\end{methoddesc}