nntplib: how to access NNTP response codes on exceptions?

carroll at tjc.com carroll at tjc.com
Thu Apr 3 03:33:39 EST 2003


The documentation for nntplib lists a number of exceptions, usually
raised when the NNTP server responds with an error message of some
kind.  When this happens, the NNTP server supplies a numeric response
code indicating the nature of the error.

For instance, if you issue a HEAD 123 for a nonexistent article, the
NNTP server will respond:

   423 No Such Article In Group

where "423" is the response code that means "no such article."

Using nntplib, if I use

 (resp, article_number, msg_id, headers) = s.head('123')

I get the exception nntplib.NNTPTemporaryError as documented in
http://www.python.org/doc/current/lib/module-nntplib.html .  But
NNTPTemporaryError is raised for any response code in the range
400-499.  Is there a way of determining the particular response code?
Ordinarily, it would be the first three characters in the first
element ("resp", in my example) returned from the nntplib method
(whether head, stat, etc.).  But because of the exception, the
assignment to the tuple never occurs, so I can't check it.

I want to distinguish between 423 errors, which are expected and which
I can ignore, and anything else that comes up.




More information about the Python-list mailing list