blob: 8b59fed9d3024c7a57ed529aa40938a99339e0a6 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{nntplib} ---
2 NNTP protocol client.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{standard}{nntplib}
4
5\modulesynopsis{NNTP protocol client (requires sockets).}
6
Fred Drake6279fcc1998-01-07 13:23:32 +00007\indexii{NNTP}{protocol}
Fred Drake3f6034d1998-07-02 19:33:43 +00008\index{Network News Transfer Protocol}
Guido van Rossum86751151995-02-28 17:14:32 +00009
Guido van Rossum86751151995-02-28 17:14:32 +000010
Fred Drakefc576191998-04-04 07:15:02 +000011This module defines the class \class{NNTP} which implements the client
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000012side of the NNTP protocol. It can be used to implement a news reader
13or poster, or automated news processors. For more information on NNTP
Fred Drakec5891241998-02-09 19:16:20 +000014(Network News Transfer Protocol), see Internet \rfc{977}.
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000015
Guido van Rossum1b91cda1995-03-24 15:56:02 +000016Here are two small examples of how it can be used. To list some
17statistics about a newsgroup and print the subjects of the last 10
18articles:
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000019
Fred Drake19479911998-02-13 06:58:54 +000020\begin{verbatim}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000021>>> s = NNTP('news.cwi.nl')
22>>> resp, count, first, last, name = s.group('comp.lang.python')
23>>> print 'Group', name, 'has', count, 'articles, range', first, 'to', last
24Group comp.lang.python has 59 articles, range 3742 to 3803
25>>> resp, subs = s.xhdr('subject', first + '-' + last)
26>>> for id, sub in subs[-10:]: print id, sub
27...
283792 Re: Removing elements from a list while iterating...
293793 Re: Who likes Info files?
303794 Emacs and doc strings
313795 a few questions about the Mac implementation
323796 Re: executable python scripts
333797 Re: executable python scripts
343798 Re: a few questions about the Mac implementation
353799 Re: PROPOSAL: A Generic Python Object Interface for Python C Modules
363802 Re: executable python scripts
Fred Drake65b32f71998-02-09 20:27:12 +0000373803 Re: \POSIX{} wait and SIGCHLD
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000038>>> s.quit()
39'205 news.cwi.nl closing connection. Goodbye.'
Fred Drake19479911998-02-13 06:58:54 +000040\end{verbatim}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000041
42To post an article from a file (this assumes that the article has
43valid headers):
44
Fred Drake19479911998-02-13 06:58:54 +000045\begin{verbatim}
Guido van Rossumcca8d2b1995-03-22 15:48:46 +000046>>> s = NNTP('news.cwi.nl')
47>>> f = open('/tmp/article')
48>>> s.post(f)
49'240 Article posted successfully.'
50>>> s.quit()
51'205 news.cwi.nl closing connection. Goodbye.'
Fred Drake19479911998-02-13 06:58:54 +000052\end{verbatim}
Guido van Rossume47da0a1997-07-17 16:34:52 +000053%
Guido van Rossum1b91cda1995-03-24 15:56:02 +000054The module itself defines the following items:
55
Fred Drakefc576191998-04-04 07:15:02 +000056\begin{classdesc}{NNTP}{host\optional{, port}}
57Return a new instance of the \class{NNTP} class, representing a
Guido van Rossum1b91cda1995-03-24 15:56:02 +000058connection to the NNTP server running on host \var{host}, listening at
59port \var{port}. The default \var{port} is 119.
Fred Drakefc576191998-04-04 07:15:02 +000060\end{classdesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +000061
62\begin{excdesc}{error_reply}
63Exception raised when an unexpected reply is received from the server.
64\end{excdesc}
65
66\begin{excdesc}{error_temp}
67Exception raised when an error code in the range 400--499 is received.
68\end{excdesc}
69
70\begin{excdesc}{error_perm}
71Exception raised when an error code in the range 500--599 is received.
72\end{excdesc}
73
74\begin{excdesc}{error_proto}
75Exception raised when a reply is received from the server that does
76not begin with a digit in the range 1--5.
77\end{excdesc}
78
Fred Drakefc576191998-04-04 07:15:02 +000079
Guido van Rossum1b91cda1995-03-24 15:56:02 +000080\subsection{NNTP Objects}
Fred Drakefc576191998-04-04 07:15:02 +000081\label{nntp-objects}
Guido van Rossum1b91cda1995-03-24 15:56:02 +000082
83NNTP instances have the following methods. The \var{response} that is
84returned as the first item in the return tuple of almost all methods
85is the server's response: a string beginning with a three-digit code.
86If the server's response indicates an error, the method raises one of
87the above exceptions.
88
Guido van Rossum1b91cda1995-03-24 15:56:02 +000089
Fred Drakefc576191998-04-04 07:15:02 +000090\begin{methoddesc}{getwelcome}{}
Guido van Rossum1b91cda1995-03-24 15:56:02 +000091Return the welcome message sent by the server in reply to the initial
92connection. (This message sometimes contains disclaimers or help
93information that may be relevant to the user.)
Fred Drakefc576191998-04-04 07:15:02 +000094\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +000095
Fred Drakefc576191998-04-04 07:15:02 +000096\begin{methoddesc}{set_debuglevel}{level}
Guido van Rossum1b91cda1995-03-24 15:56:02 +000097Set the instance's debugging level. This controls the amount of
Fred Drakefc576191998-04-04 07:15:02 +000098debugging output printed. The default, \code{0}, produces no debugging
99output. A value of \code{1} produces a moderate amount of debugging
100output, generally a single line per request or response. A value of
101\code{2} or higher produces the maximum amount of debugging output,
102logging each line sent and received on the connection (including
103message text).
104\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000105
Fred Drakefc576191998-04-04 07:15:02 +0000106\begin{methoddesc}{newgroups}{date, time}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000107Send a \samp{NEWGROUPS} command. The \var{date} argument should be a
108string of the form \code{"\var{yy}\var{mm}\var{dd}"} indicating the
109date, and \var{time} should be a string of the form
110\code{"\var{hh}\var{mm}\var{ss}"} indicating the time. Return a pair
111\code{(\var{response}, \var{groups})} where \var{groups} is a list of
112group names that are new since the given date and time.
Fred Drakefc576191998-04-04 07:15:02 +0000113\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000114
Fred Drakefc576191998-04-04 07:15:02 +0000115\begin{methoddesc}{newnews}{group, date, time}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000116Send a \samp{NEWNEWS} command. Here, \var{group} is a group name or
Fred Drakefc576191998-04-04 07:15:02 +0000117\code{'*'}, and \var{date} and \var{time} have the same meaning as for
118\method{newgroups()}. Return a pair \code{(\var{response},
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000119\var{articles})} where \var{articles} is a list of article ids.
Fred Drakefc576191998-04-04 07:15:02 +0000120\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000121
Fred Drakefc576191998-04-04 07:15:02 +0000122\begin{methoddesc}{list}{}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000123Send a \samp{LIST} command. Return a pair \code{(\var{response},
124\var{list})} where \var{list} is a list of tuples. Each tuple has the
125form \code{(\var{group}, \var{last}, \var{first}, \var{flag})}, where
126\var{group} is a group name, \var{last} and \var{first} are the last
127and first article numbers (as strings), and \var{flag} is \code{'y'}
128if posting is allowed, \code{'n'} if not, and \code{'m'} if the
129newsgroup is moderated. (Note the ordering: \var{last}, \var{first}.)
Fred Drakefc576191998-04-04 07:15:02 +0000130\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000131
Fred Drakefc576191998-04-04 07:15:02 +0000132\begin{methoddesc}{group}{name}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000133Send a \samp{GROUP} command, where \var{name} is the group name.
Fred Drakefc576191998-04-04 07:15:02 +0000134Return a tuple \code{(}\var{response}\code{,} \var{count}\code{,}
135\var{first}\code{,} \var{last}\code{,} \var{name}\code{)} where
136\var{count} is the (estimated) number of articles in the group,
137\var{first} is the first article number in the group, \var{last} is
138the last article number in the group, and \var{name} is the group
139name. The numbers are returned as strings.
140\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000141
Fred Drakefc576191998-04-04 07:15:02 +0000142\begin{methoddesc}{help}{}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000143Send a \samp{HELP} command. Return a pair \code{(\var{response},
144\var{list})} where \var{list} is a list of help strings.
Fred Drakefc576191998-04-04 07:15:02 +0000145\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000146
Fred Drakefc576191998-04-04 07:15:02 +0000147\begin{methoddesc}{stat}{id}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000148Send a \samp{STAT} command, where \var{id} is the message id (enclosed
Fred Drakefc576191998-04-04 07:15:02 +0000149in \character{<} and \character{>}) or an article number (as a string).
Fred Drake4b3f0311996-12-13 22:04:31 +0000150Return a triple \code{(\var{response}, \var{number}, \var{id})} where
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000151\var{number} is the article number (as a string) and \var{id} is the
Fred Drakefc576191998-04-04 07:15:02 +0000152article id (enclosed in \character{<} and \character{>}).
153\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000154
Fred Drakefc576191998-04-04 07:15:02 +0000155\begin{methoddesc}{next}{}
156Send a \samp{NEXT} 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}{last}{}
160Send a \samp{LAST} command. Return as for \method{stat()}.
161\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000162
Fred Drakefc576191998-04-04 07:15:02 +0000163\begin{methoddesc}{head}{id}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000164Send a \samp{HEAD} command, where \var{id} has the same meaning as for
Guido van Rossumcd905091998-06-30 14:53:41 +0000165\method{stat()}. Return a tuple
166\code{(\var{response}, \var{number}, \var{id}, \var{list})}
167where the first three are the same as for \method{stat()},
168and \var{list} is a list of the article's headers (an uninterpreted
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000169list of lines, without trailing newlines).
Fred Drakefc576191998-04-04 07:15:02 +0000170\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000171
Fred Drakefc576191998-04-04 07:15:02 +0000172\begin{methoddesc}{body}{id}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000173Send a \samp{BODY} command, where \var{id} has the same meaning as for
Guido van Rossumcd905091998-06-30 14:53:41 +0000174\method{stat()}. Return as for \method{head()}.
Fred Drakefc576191998-04-04 07:15:02 +0000175\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000176
Fred Drakefc576191998-04-04 07:15:02 +0000177\begin{methoddesc}{article}{id}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000178Send a \samp{ARTICLE} command, where \var{id} has the same meaning as
Guido van Rossumcd905091998-06-30 14:53:41 +0000179for \method{stat()}. Return as for \method{head()}.
Fred Drakefc576191998-04-04 07:15:02 +0000180\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000181
Fred Drakefc576191998-04-04 07:15:02 +0000182\begin{methoddesc}{slave}{}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000183Send a \samp{SLAVE} command. Return the server's \var{response}.
Fred Drakefc576191998-04-04 07:15:02 +0000184\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000185
Fred Drakefc576191998-04-04 07:15:02 +0000186\begin{methoddesc}{xhdr}{header, string}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000187Send an \samp{XHDR} command. This command is not defined in the RFC
188but is a common extension. The \var{header} argument is a header
Fred Drakefc576191998-04-04 07:15:02 +0000189keyword, e.g. \code{'subject'}. The \var{string} argument should have
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000190the form \code{"\var{first}-\var{last}"} where \var{first} and
191\var{last} are the first and last article numbers to search. Return a
192pair \code{(\var{response}, \var{list})}, where \var{list} is a list of
193pairs \code{(\var{id}, \var{text})}, where \var{id} is an article id
194(as a string) and \var{text} is the text of the requested header for
195that article.
Fred Drakefc576191998-04-04 07:15:02 +0000196\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000197
Fred Drakefc576191998-04-04 07:15:02 +0000198\begin{methoddesc}{post}{file}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000199Post an article using the \samp{POST} command. The \var{file}
200argument is an open file object which is read until EOF using its
Fred Drakefc576191998-04-04 07:15:02 +0000201\method{readline()} method. It should be a well-formed news article,
202including the required headers. The \method{post()} method
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000203automatically escapes lines beginning with \samp{.}.
Fred Drakefc576191998-04-04 07:15:02 +0000204\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000205
Fred Drakefc576191998-04-04 07:15:02 +0000206\begin{methoddesc}{ihave}{id, file}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000207Send an \samp{IHAVE} command. If the response is not an error, treat
Fred Drakefc576191998-04-04 07:15:02 +0000208\var{file} exactly as for the \method{post()} method.
209\end{methoddesc}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000210
Fred Drakefc576191998-04-04 07:15:02 +0000211\begin{methoddesc}{date}{}
Guido van Rossum94adab51997-06-02 17:27:50 +0000212Return a triple \code{(\var{response}, \var{date}, \var{time})},
213containing the current date and time in a form suitable for the
Fred Drakefc576191998-04-04 07:15:02 +0000214\method{newnews()} and \method{newgroups()} methods.
Guido van Rossum94adab51997-06-02 17:27:50 +0000215This is an optional NNTP extension, and may not be supported by all
216servers.
Fred Drakefc576191998-04-04 07:15:02 +0000217\end{methoddesc}
Guido van Rossum94adab51997-06-02 17:27:50 +0000218
Fred Drakefc576191998-04-04 07:15:02 +0000219\begin{methoddesc}{xgtitle}{name}
220Process an \samp{XGTITLE} command, returning a pair \code{(\var{response},
Fred Drakefac431e1998-02-16 21:57:37 +0000221\var{list})}, where \var{list} is a list of tuples containing
Guido van Rossum94adab51997-06-02 17:27:50 +0000222\code{(\var{name}, \var{title})}.
223% XXX huh? Should that be name, description?
224This is an optional NNTP extension, and may not be supported by all
225servers.
Fred Drakefc576191998-04-04 07:15:02 +0000226\end{methoddesc}
Guido van Rossum94adab51997-06-02 17:27:50 +0000227
Fred Drakefc576191998-04-04 07:15:02 +0000228\begin{methoddesc}{xover}{start, end}
Guido van Rossum94adab51997-06-02 17:27:50 +0000229Return a pair \code{(\var{resp}, \var{list})}. \var{list} is a list
230of tuples, one for each article in the range delimited by the \var{start}
231and \var{end} article numbers. Each tuple is of the form
Fred Drakefac431e1998-02-16 21:57:37 +0000232\code{(}\var{article number}\code{,} \var{subject}\code{,}
233\var{poster}\code{,} \var{date}\code{,} \var{id}\code{,}
234\var{references}\code{,} \var{size}\code{,} \var{lines}\code{)}.
Guido van Rossum94adab51997-06-02 17:27:50 +0000235This is an optional NNTP extension, and may not be supported by all
236servers.
Fred Drakefc576191998-04-04 07:15:02 +0000237\end{methoddesc}
Guido van Rossum94adab51997-06-02 17:27:50 +0000238
Fred Drakefc576191998-04-04 07:15:02 +0000239\begin{methoddesc}{xpath}{id}
Guido van Rossum94adab51997-06-02 17:27:50 +0000240Return a pair \code{(\var{resp}, \var{path})}, where \var{path} is the
241directory path to the article with message ID \var{id}. This is an
242optional NNTP extension, and may not be supported by all servers.
Fred Drakefc576191998-04-04 07:15:02 +0000243\end{methoddesc}
Guido van Rossum94adab51997-06-02 17:27:50 +0000244
Fred Drakefc576191998-04-04 07:15:02 +0000245\begin{methoddesc}{quit}{}
Guido van Rossum1b91cda1995-03-24 15:56:02 +0000246Send a \samp{QUIT} command and close the connection. Once this method
247has been called, no other methods of the NNTP object should be called.
Fred Drakefc576191998-04-04 07:15:02 +0000248\end{methoddesc}