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