1 \section{\module{nntplib
} ---
4 \declaremodule{standard
}{nntplib
}
5 \modulesynopsis{NNTP protocol client (requires sockets).
}
7 \indexii{NNTP
}{protocol
}
8 \index{Network News Transfer Protocol
}
10 This module defines the class
\class{NNTP
} which implements the client
11 side of the NNTP protocol. It can be used to implement a news reader
12 or poster, or automated news processors. For more information on NNTP
13 (Network News Transfer Protocol), see Internet
\rfc{977}.
15 Here are two small examples of how it can be used. To list some
16 statistics about a newsgroup and print the subjects of the last
10
20 >>> s = NNTP('news.cwi.nl')
21 >>> resp, count, first, last, name = s.group('comp.lang.python')
22 >>> print 'Group', name, 'has', count, 'articles, range', first, 'to', last
23 Group comp.lang.python has
59 articles, range
3742 to
3803
24 >>> resp, subs = s.xhdr('subject', first + '-' + last)
25 >>> for id, sub in subs
[-
10:
]: print id, sub
27 3792 Re: Removing elements from a list while iterating...
28 3793 Re: Who likes Info files?
29 3794 Emacs and doc strings
30 3795 a few questions about the Mac implementation
31 3796 Re: executable python scripts
32 3797 Re: executable python scripts
33 3798 Re: a few questions about the Mac implementation
34 3799 Re: PROPOSAL: A Generic Python Object Interface for Python C Modules
35 3802 Re: executable python scripts
36 3803 Re:
\POSIX{} wait and SIGCHLD
38 '
205 news.cwi.nl closing connection. Goodbye.'
41 To post an article from a file (this assumes that the article has
45 >>> s = NNTP('news.cwi.nl')
46 >>> f = open('/tmp/article')
48 '
240 Article posted successfully.'
50 '
205 news.cwi.nl closing connection. Goodbye.'
53 The module itself defines the following items:
55 \begin{classdesc
}{NNTP
}{host
\optional{, port
56 \optional{, user
\optional{, password
57 \optional{, readermode
}}}}}
58 Return a new instance of the
\class{NNTP
} class, representing a
59 connection to the NNTP server running on host
\var{host
}, listening at
60 port
\var{port
}. The default
\var{port
} is
119. If the optional
61 \var{user
} and
\var{password
} are provided, the
62 \samp{AUTHINFO USER
} and
\samp{AUTHINFO PASS
} commands are used to
63 identify and authenticate the user to the server. If the optional
64 flag
\var{readermode
} is true, then a
\samp{mode reader
} command is
65 sent before authentication is performed. Reader mode is sometimes
66 necessary if you are connecting to an NNTP server on the local machine
67 and intend to call reader-specific commands, such as
\samp{group
}. If
68 you get unexpected
\code{NNTPPermanentError
}s, you might need to set
69 \var{readermode
}.
\var{readermode
} defaults to
\code{None
}.
72 \begin{classdesc
}{NNTPError
}{}
73 Derived from the standard exception
\code{Exception
}, this is the base
74 class for all exceptions raised by the
\code{nntplib
} module.
77 \begin{classdesc
}{NNTPReplyError
}{}
78 Exception raised when an unexpected reply is received from the
79 server. For backwards compatibility, the exception
\code{error_reply
}
80 is equivalent to this class.
83 \begin{classdesc
}{NNTPTemporaryError
}{}
84 Exception raised when an error code in the range
400--
499 is
85 received. For backwards compatibility, the exception
86 \code{error_temp
} is equivalent to this class.
89 \begin{classdesc
}{NNTPPermanentError
}{}
90 Exception raised when an error code in the range
500--
599 is
91 received. For backwards compatibility, the exception
92 \code{error_perm
} is equivalent to this class.
95 \begin{classdesc
}{NNTPProtocolError
}{}
96 Exception raised when a reply is received from the server that does
97 not begin with a digit in the range
1--
5. For backwards
98 compatibility, the exception
\code{error_proto
} is equivalent to this
102 \begin{classdesc
}{NNTPDataError
}{}
103 Exception raised when there is some error in the response data. For
104 backwards compatibility, the exception
\code{error_data
} is
105 equivalent to this class.
109 \subsection{NNTP Objects
\label{nntp-objects
}}
111 NNTP instances have the following methods. The
\var{response
} that is
112 returned as the first item in the return tuple of almost all methods
113 is the server's response: a string beginning with a three-digit code.
114 If the server's response indicates an error, the method raises one of
115 the above exceptions.
118 \begin{methoddesc
}{getwelcome
}{}
119 Return the welcome message sent by the server in reply to the initial
120 connection. (This message sometimes contains disclaimers or help
121 information that may be relevant to the user.)
124 \begin{methoddesc
}{set_debuglevel
}{level
}
125 Set the instance's debugging level. This controls the amount of
126 debugging output printed. The default,
\code{0}, produces no debugging
127 output. A value of
\code{1} produces a moderate amount of debugging
128 output, generally a single line per request or response. A value of
129 \code{2} or higher produces the maximum amount of debugging output,
130 logging each line sent and received on the connection (including
134 \begin{methoddesc
}{newgroups
}{date, time
}
135 Send a
\samp{NEWGROUPS
} command. The
\var{date
} argument should be a
136 string of the form
\code{'
\var{yy
}\var{mm
}\var{dd
}'
} indicating the
137 date, and
\var{time
} should be a string of the form
138 \code{'
\var{hh
}\var{mm
}\var{ss
}'
} indicating the time. Return a pair
139 \code{(
\var{response
},
\var{groups
})
} where
\var{groups
} is a list of
140 group names that are new since the given date and time.
143 \begin{methoddesc
}{newnews
}{group, date, time
}
144 Send a
\samp{NEWNEWS
} command. Here,
\var{group
} is a group name or
145 \code{'*'
}, and
\var{date
} and
\var{time
} have the same meaning as for
146 \method{newgroups()
}. Return a pair
\code{(
\var{response
},
147 \var{articles
})
} where
\var{articles
} is a list of article ids.
150 \begin{methoddesc
}{list
}{}
151 Send a
\samp{LIST
} command. Return a pair
\code{(
\var{response
},
152 \var{list
})
} where
\var{list
} is a list of tuples. Each tuple has the
153 form
\code{(
\var{group
},
\var{last
},
\var{first
},
\var{flag
})
}, where
154 \var{group
} is a group name,
\var{last
} and
\var{first
} are the last
155 and first article numbers (as strings), and
\var{flag
} is
156 \code{'y'
} if posting is allowed,
\code{'n'
} if not, and
\code{'m'
} if
157 the newsgroup is moderated. (Note the ordering:
\var{last
},
161 \begin{methoddesc
}{group
}{name
}
162 Send a
\samp{GROUP
} command, where
\var{name
} is the group name.
163 Return a tuple
\code{(
\var{response
},
\var{count
},
\var{first
},
164 \var{last
},
\var{name
})
} where
\var{count
} is the (estimated) number
165 of articles in the group,
\var{first
} is the first article number in
166 the group,
\var{last
} is the last article number in the group, and
167 \var{name
} is the group name. The numbers are returned as strings.
170 \begin{methoddesc
}{help
}{}
171 Send a
\samp{HELP
} command. Return a pair
\code{(
\var{response
},
172 \var{list
})
} where
\var{list
} is a list of help strings.
175 \begin{methoddesc
}{stat
}{id
}
176 Send a
\samp{STAT
} command, where
\var{id
} is the message id (enclosed
177 in
\character{<
} and
\character{>
}) or an article number (as a string).
178 Return a triple
\code{(
\var{response
},
\var{number
},
\var{id
})
} where
179 \var{number
} is the article number (as a string) and
\var{id
} is the
180 article id (enclosed in
\character{<
} and
\character{>
}).
183 \begin{methoddesc
}{next
}{}
184 Send a
\samp{NEXT
} command. Return as for
\method{stat()
}.
187 \begin{methoddesc
}{last
}{}
188 Send a
\samp{LAST
} command. Return as for
\method{stat()
}.
191 \begin{methoddesc
}{head
}{id
}
192 Send a
\samp{HEAD
} command, where
\var{id
} has the same meaning as for
193 \method{stat()
}. Return a tuple
194 \code{(
\var{response
},
\var{number
},
\var{id
},
\var{list
})
}
195 where the first three are the same as for
\method{stat()
},
196 and
\var{list
} is a list of the article's headers (an uninterpreted
197 list of lines, without trailing newlines).
200 \begin{methoddesc
}{body
}{id
}
201 Send a
\samp{BODY
} command, where
\var{id
} has the same meaning as for
202 \method{stat()
}. Return as for
\method{head()
}.
205 \begin{methoddesc
}{article
}{id
}
206 Send an
\samp{ARTICLE
} command, where
\var{id
} has the same meaning as
207 for
\method{stat()
}. Return as for
\method{head()
}.
210 \begin{methoddesc
}{slave
}{}
211 Send a
\samp{SLAVE
} command. Return the server's
\var{response
}.
214 \begin{methoddesc
}{xhdr
}{header, string
}
215 Send an
\samp{XHDR
} command. This command is not defined in the RFC
216 but is a common extension. The
\var{header
} argument is a header
217 keyword, e.g.
\code{'subject'
}. The
\var{string
} argument should have
218 the form
\code{'
\var{first
}-
\var{last
}'
} where
\var{first
} and
219 \var{last
} are the first and last article numbers to search. Return a
220 pair
\code{(
\var{response
},
\var{list
})
}, where
\var{list
} is a list of
221 pairs
\code{(
\var{id
},
\var{text
})
}, where
\var{id
} is an article id
222 (as a string) and
\var{text
} is the text of the requested header for
226 \begin{methoddesc
}{post
}{file
}
227 Post an article using the
\samp{POST
} command. The
\var{file
}
228 argument is an open file object which is read until EOF using its
229 \method{readline()
} method. It should be a well-formed news article,
230 including the required headers. The
\method{post()
} method
231 automatically escapes lines beginning with
\samp{.
}.
234 \begin{methoddesc
}{ihave
}{id, file
}
235 Send an
\samp{IHAVE
} command. If the response is not an error, treat
236 \var{file
} exactly as for the
\method{post()
} method.
239 \begin{methoddesc
}{date
}{}
240 Return a triple
\code{(
\var{response
},
\var{date
},
\var{time
})
},
241 containing the current date and time in a form suitable for the
242 \method{newnews()
} and
\method{newgroups()
} methods.
243 This is an optional NNTP extension, and may not be supported by all
247 \begin{methoddesc
}{xgtitle
}{name
}
248 Process an
\samp{XGTITLE
} command, returning a pair
\code{(
\var{response
},
249 \var{list
})
}, where
\var{list
} is a list of tuples containing
250 \code{(
\var{name
},
\var{title
})
}.
251 % XXX huh? Should that be name, description?
252 This is an optional NNTP extension, and may not be supported by all
256 \begin{methoddesc
}{xover
}{start, end
}
257 Return a pair
\code{(
\var{resp
},
\var{list
})
}.
\var{list
} is a list
258 of tuples, one for each article in the range delimited by the
\var{start
}
259 and
\var{end
} article numbers. Each tuple is of the form
260 \code{(
\var{article number
},
\var{subject
},
\var{poster
},
\var{date
},
261 \var{id
},
\var{references
},
\var{size
},
\var{lines
})
}.
262 This is an optional NNTP extension, and may not be supported by all
266 \begin{methoddesc
}{xpath
}{id
}
267 Return a pair
\code{(
\var{resp
},
\var{path
})
}, where
\var{path
} is the
268 directory path to the article with message ID
\var{id
}. This is an
269 optional NNTP extension, and may not be supported by all servers.
272 \begin{methoddesc
}{quit
}{}
273 Send a
\samp{QUIT
} command and close the connection. Once this method
274 has been called, no other methods of the NNTP object should be called.