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