views:

108

answers:

1

Does httplib.HTTPException have error codes? If so how do I get at them from the exception instance? Any help is appreciated.

A: 

The httplib module doesn't use exceptions to convey HTTP responses, just genuine errors (invalid HTTP responses, broken headers, invalid status codes, prematurely broken connections, etc.) Most of the httplib.HTTPException subclasses just have an associated message string (stored in the args attribute), if even that. httplib.HTTPException itself may have an "errno" value as the first entry in args (when raised through httplib.FakeSocket) but it's not a HTTP error code.

The HTTP response codes are conveyed through the httplib.HTTPConnection object, though; the getresponse method will (usually) return a HTTPResponse instance with a status attribute set to the HTTP response code, and a reason attribute set to the text version of it. This includes error codes like 404 and 500. I say "usually" because you (or a library you use) can override httplib.HTTPConnection.response_class to return something else.

Thomas Wouters