blob: ba1d788ed37c8e7a289d34856b9f01340abd53ac [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}
Guido van Rossum86751151995-02-28 17:14:32 +00005
Guido van Rossum86751151995-02-28 17:14:32 +00006
Fred Drakefc576191998-04-04 07:15:02 +00007This module defines the class \class{NNTP} which implements the client
Guido van Rossumcca8d2b1995-03-22 15:48:46 +00008side of the NNTP protocol. It can be used to implement a news reader
9or poster, or automated news processors. For more information on NNTP
Fred Drakec5891241998-02-09 19:16:20 +000010(Network News Transfer Protocol), see Internet \rfc{977}.
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000011
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
Fred Drake19479911998-02-13 06:58:54 +000016\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
Fred Drake65b32f71998-02-09 20:27:12 +0000333803 Re: \POSIX{} wait and SIGCHLD
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000034>>> s.quit()
35'205 news.cwi.nl closing connection. Goodbye.'
Fred Drake19479911998-02-13 06:58:54 +000036\end{verbatim}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000037
38To post an article from a file (this assumes that the article has
39valid headers):
40
Fred Drake19479911998-02-13 06:58:54 +000041\begin{verbatim}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000042>>> 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.'
Fred Drake19479911998-02-13 06:58:54 +000048\end{verbatim}
Guido van Rossume47da0a1997-07-17 16:34:52 +000049%
Guido van Rossum1b91cda1995-03-24 15:56:02 +000050The module itself defines the following items:
51
Fred Drakefc576191998-04-04 07:15:02 +000052\begin{classdesc}{NNTP}{host\optional{, port}}
53Return a new instance of the \class{NNTP} class, representing a
Guido van Rossum1b91cda1995-03-24 15:56:02 +000054connection to the NNTP server running on host \var{host}, listening at
55port \var{port}. The default \var{port} is 119.
Fred Drakefc576191998-04-04 07:15:02 +000056\end{classdesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +000057
58\begin{excdesc}{error_reply}
59Exception raised when an unexpected reply is received from the server.
60\end{excdesc}
61
62\begin{excdesc}{error_temp}
63Exception raised when an error code in the range 400--499 is received.
64\end{excdesc}
65
66\begin{excdesc}{error_perm}
67Exception raised when an error code in the range 500--599 is received.
68\end{excdesc}
69
70\begin{excdesc}{error_proto}
71Exception raised when a reply is received from the server that does
72not begin with a digit in the range 1--5.
73\end{excdesc}
74
Fred Drakefc576191998-04-04 07:15:02 +000075
Guido van Rossum1b91cda1995-03-24 15:56:02 +000076\subsection{NNTP Objects}
Fred Drakefc576191998-04-04 07:15:02 +000077\label{nntp-objects}
Guido van Rossum1b91cda1995-03-24 15:56:02 +000078
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
Guido van Rossum1b91cda1995-03-24 15:56:02 +000085
Fred Drakefc576191998-04-04 07:15:02 +000086\begin{methoddesc}{getwelcome}{}
Guido van Rossum1b91cda1995-03-24 15:56:02 +000087Return 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.)
Fred Drakefc576191998-04-04 07:15:02 +000090\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +000091
Fred Drakefc576191998-04-04 07:15:02 +000092\begin{methoddesc}{set_debuglevel}{level}
Guido van Rossum1b91cda1995-03-24 15:56:02 +000093Set the instance's debugging level. This controls the amount of
Fred Drakefc576191998-04-04 07:15:02 +000094debugging output printed. The default, \code{0}, produces no debugging
95output. A value of \code{1} produces a moderate amount of debugging
96output, generally a single line per request or response. A value of
97\code{2} or higher produces the maximum amount of debugging output,
98logging each line sent and received on the connection (including
99message text).
100\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000101
Fred Drakefc576191998-04-04 07:15:02 +0000102\begin{methoddesc}{newgroups}{date, time}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000103Send 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.
Fred Drakefc576191998-04-04 07:15:02 +0000109\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000110
Fred Drakefc576191998-04-04 07:15:02 +0000111\begin{methoddesc}{newnews}{group, date, time}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000112Send a \samp{NEWNEWS} command. Here, \var{group} is a group name or
Fred Drakefc576191998-04-04 07:15:02 +0000113\code{'*'}, and \var{date} and \var{time} have the same meaning as for
114\method{newgroups()}. Return a pair \code{(\var{response},
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000115\var{articles})} where \var{articles} is a list of article ids.
Fred Drakefc576191998-04-04 07:15:02 +0000116\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000117
Fred Drakefc576191998-04-04 07:15:02 +0000118\begin{methoddesc}{list}{}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000119Send 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}.)
Fred Drakefc576191998-04-04 07:15:02 +0000126\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000127
Fred Drakefc576191998-04-04 07:15:02 +0000128\begin{methoddesc}{group}{name}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000129Send a \samp{GROUP} command, where \var{name} is the group name.
Fred Drakefc576191998-04-04 07:15:02 +0000130Return a tuple \code{(}\var{response}\code{,} \var{count}\code{,}
131\var{first}\code{,} \var{last}\code{,} \var{name}\code{)} where
132\var{count} is the (estimated) number of articles in the group,
133\var{first} is the first article number in the group, \var{last} is
134the last article number in the group, and \var{name} is the group
135name. The numbers are returned as strings.
136\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000137
Fred Drakefc576191998-04-04 07:15:02 +0000138\begin{methoddesc}{help}{}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000139Send a \samp{HELP} command. Return a pair \code{(\var{response},
140\var{list})} where \var{list} is a list of help strings.
Fred Drakefc576191998-04-04 07:15:02 +0000141\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000142
Fred Drakefc576191998-04-04 07:15:02 +0000143\begin{methoddesc}{stat}{id}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000144Send a \samp{STAT} command, where \var{id} is the message id (enclosed
Fred Drakefc576191998-04-04 07:15:02 +0000145in \character{<} and \character{>}) or an article number (as a string).
Fred Drake4b3f0311996-12-13 22:04:31 +0000146Return a triple \code{(\var{response}, \var{number}, \var{id})} where
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000147\var{number} is the article number (as a string) and \var{id} is the
Fred Drakefc576191998-04-04 07:15:02 +0000148article id (enclosed in \character{<} and \character{>}).
149\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000150
Fred Drakefc576191998-04-04 07:15:02 +0000151\begin{methoddesc}{next}{}
152Send a \samp{NEXT} command. Return as for \method{stat()}.
153\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000154
Fred Drakefc576191998-04-04 07:15:02 +0000155\begin{methoddesc}{last}{}
156Send a \samp{LAST} command. Return as for \method{stat()}.
157\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000158
Fred Drakefc576191998-04-04 07:15:02 +0000159\begin{methoddesc}{head}{id}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000160Send a \samp{HEAD} command, where \var{id} has the same meaning as for
Fred Drakefc576191998-04-04 07:15:02 +0000161\method{stat()}. Return a pair \code{(\var{response}, \var{list})}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000162where \var{list} is a list of the article's headers (an uninterpreted
163list of lines, without trailing newlines).
Fred Drakefc576191998-04-04 07:15:02 +0000164\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000165
Fred Drakefc576191998-04-04 07:15:02 +0000166\begin{methoddesc}{body}{id}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000167Send a \samp{BODY} command, where \var{id} has the same meaning as for
Fred Drakefc576191998-04-04 07:15:02 +0000168\method{stat()}. Return a pair \code{(\var{response}, \var{list})}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000169where \var{list} is a list of the article's body text (an
170uninterpreted list of lines, without trailing newlines).
Fred Drakefc576191998-04-04 07:15:02 +0000171\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000172
Fred Drakefc576191998-04-04 07:15:02 +0000173\begin{methoddesc}{article}{id}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000174Send a \samp{ARTICLE} command, where \var{id} has the same meaning as
Fred Drakefc576191998-04-04 07:15:02 +0000175for \method{stat()}. Return a pair \code{(\var{response}, \var{list})}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000176where \var{list} is a list of the article's header and body text (an
177uninterpreted list of lines, without trailing newlines).
Fred Drakefc576191998-04-04 07:15:02 +0000178\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000179
Fred Drakefc576191998-04-04 07:15:02 +0000180\begin{methoddesc}{slave}{}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000181Send a \samp{SLAVE} command. Return the server's \var{response}.
Fred Drakefc576191998-04-04 07:15:02 +0000182\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000183
Fred Drakefc576191998-04-04 07:15:02 +0000184\begin{methoddesc}{xhdr}{header, string}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000185Send an \samp{XHDR} command. This command is not defined in the RFC
186but is a common extension. The \var{header} argument is a header
Fred Drakefc576191998-04-04 07:15:02 +0000187keyword, e.g. \code{'subject'}. The \var{string} argument should have
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000188the form \code{"\var{first}-\var{last}"} where \var{first} and
189\var{last} are the first and last article numbers to search. Return a
190pair \code{(\var{response}, \var{list})}, where \var{list} is a list of
191pairs \code{(\var{id}, \var{text})}, where \var{id} is an article id
192(as a string) and \var{text} is the text of the requested header for
193that article.
Fred Drakefc576191998-04-04 07:15:02 +0000194\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000195
Fred Drakefc576191998-04-04 07:15:02 +0000196\begin{methoddesc}{post}{file}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000197Post an article using the \samp{POST} command. The \var{file}
198argument is an open file object which is read until EOF using its
Fred Drakefc576191998-04-04 07:15:02 +0000199\method{readline()} method. It should be a well-formed news article,
200including the required headers. The \method{post()} method
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000201automatically escapes lines beginning with \samp{.}.
Fred Drakefc576191998-04-04 07:15:02 +0000202\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000203
Fred Drakefc576191998-04-04 07:15:02 +0000204\begin{methoddesc}{ihave}{id, file}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000205Send an \samp{IHAVE} command. If the response is not an error, treat
Fred Drakefc576191998-04-04 07:15:02 +0000206\var{file} exactly as for the \method{post()} method.
207\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000208
Fred Drakefc576191998-04-04 07:15:02 +0000209\begin{methoddesc}{date}{}
Guido van Rossum94adab51997-06-02 17:27:50 +0000210Return a triple \code{(\var{response}, \var{date}, \var{time})},
211containing the current date and time in a form suitable for the
Fred Drakefc576191998-04-04 07:15:02 +0000212\method{newnews()} and \method{newgroups()} methods.
Guido van Rossum94adab51997-06-02 17:27:50 +0000213This is an optional NNTP extension, and may not be supported by all
214servers.
Fred Drakefc576191998-04-04 07:15:02 +0000215\end{methoddesc}
Guido van Rossum94adab51997-06-02 17:27:50 +0000216
Fred Drakefc576191998-04-04 07:15:02 +0000217\begin{methoddesc}{xgtitle}{name}
218Process an \samp{XGTITLE} command, returning a pair \code{(\var{response},
Fred Drakefac431e1998-02-16 21:57:37 +0000219\var{list})}, where \var{list} is a list of tuples containing
Guido van Rossum94adab51997-06-02 17:27:50 +0000220\code{(\var{name}, \var{title})}.
221% XXX huh? Should that be name, description?
222This is an optional NNTP extension, and may not be supported by all
223servers.
Fred Drakefc576191998-04-04 07:15:02 +0000224\end{methoddesc}
Guido van Rossum94adab51997-06-02 17:27:50 +0000225
Fred Drakefc576191998-04-04 07:15:02 +0000226\begin{methoddesc}{xover}{start, end}
Guido van Rossum94adab51997-06-02 17:27:50 +0000227Return a pair \code{(\var{resp}, \var{list})}. \var{list} is a list
228of tuples, one for each article in the range delimited by the \var{start}
229and \var{end} article numbers. Each tuple is of the form
Fred Drakefac431e1998-02-16 21:57:37 +0000230\code{(}\var{article number}\code{,} \var{subject}\code{,}
231\var{poster}\code{,} \var{date}\code{,} \var{id}\code{,}
232\var{references}\code{,} \var{size}\code{,} \var{lines}\code{)}.
Guido van Rossum94adab51997-06-02 17:27:50 +0000233This is an optional NNTP extension, and may not be supported by all
234servers.
Fred Drakefc576191998-04-04 07:15:02 +0000235\end{methoddesc}
Guido van Rossum94adab51997-06-02 17:27:50 +0000236
Fred Drakefc576191998-04-04 07:15:02 +0000237\begin{methoddesc}{xpath}{id}
Guido van Rossum94adab51997-06-02 17:27:50 +0000238Return a pair \code{(\var{resp}, \var{path})}, where \var{path} is the
239directory path to the article with message ID \var{id}. This is an
240optional NNTP extension, and may not be supported by all servers.
Fred Drakefc576191998-04-04 07:15:02 +0000241\end{methoddesc}
Guido van Rossum94adab51997-06-02 17:27:50 +0000242
Fred Drakefc576191998-04-04 07:15:02 +0000243\begin{methoddesc}{quit}{}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000244Send a \samp{QUIT} command and close the connection. Once this method
245has been called, no other methods of the NNTP object should be called.
Fred Drakefc576191998-04-04 07:15:02 +0000246\end{methoddesc}