How can I determine an HTTPMessage ?

Paul Boddie paul at boddie.org.uk
Wed Jan 11 18:46:18 EST 2006


Peter Hansen wrote:
> Kevin wrote:
> > Can you tell me what to look for in an HTTPMessage that is an error?  I
> > have looked at the header objects and I cannot determine an error
> > message.
>
> I think you're missing most of the context and detail that would help us
> provide a useful answer for you.

Here's some of your own medicine. ;-)

A Google search for "HTTPMessage Python" produced a link to the
surprisingly helpful "urllib2 - The Missing Manual" [1]. This states
that the result of urlopen can be queried using the info method; for
example:

import urllib2
try:
    f = urllib2.urlopen(some_url)
    message = f.info()
except urllib2.HTTPError, exc:
    message = exc.info()

One would guess that the status attribute on the above message object
would yield some kind of descriptive response from the server, noting
that the response code from the server is itself typically found as the
code attribute on either the f or exc objects manipulated in the above
example. However, my limited experiments show that status is typically
set to the empty string, although a perusal of the source code for
httplib reveals that the status attribute is only used to signal
exceptional conditions in the processing of responses, and I would
guess that most HTTP interactions wouldn't result in anything special
being put in that attribute.

I suppose what one could do is to examine f.code or exc.code and then
interpret that value accordingly (see the httplib module for the HTTP
response code constants) possibly generating a suitable message,
although the only place in the standard library that helps here is the
BaseHTTPRequestHandler class:

import BaseHTTPServer
message, description = \
    BaseHTTPServer.BaseHTTPRequestHandler.responses[f.code]

Paul

P.S. It's a shame that the time spent by various other contributors in
making unhelpful remarks in response to the original, albeit imprecise
query wasn't spent in offering just a little helpful advice instead. I
guess such horseplay is how the Python community gets its "arrogant"
label...

[1] http://www.voidspace.org.uk/python/articles/urllib2.shtml




More information about the Python-list mailing list