FancyURLopener: no HTTP status codes?

Erik Max Francis max at alcyone.com
Sun Jul 7 16:15:08 EDT 2002


Carsten Gaebler wrote:

> is there any possibility of getting the HTTP status code from a
> urllib.FancyURLopener object?

If you just want the code, you want to use httplib directly. 
urllib.FancyURLopener is an abstraction that works independent of
protocol, so it doesn't really know anything about HTTP status codes.

You can use something as simple as:

        try:
            connection = httplib.HTTPConnection(host)
            connection.request(REQUEST_TYPE, file)
            response = connection.getresponse()
            self.code = response.status
            connection.close()
        except socket.error: # socket error
            self.code = 900
        except httplib.HTTPException: # httplib error
            self.code = 901
        except IOError: # TCP/IP IO error
            self.code = 902
        except ValueError: # int(port) failed (bad URL)
            self.code = 903

The 9xx codes, of course, are inventions here to indicate metaerrors.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ See the son in your bad day / Smell the flowers in the valley
\__/ Chante Moore
    Bosskey.net: Aliens vs. Predator 2 / http://www.bosskey.net/avp2/
 A personal guide to Aliens vs. Predator 2.



More information about the Python-list mailing list