urllib2 http status

John J. Lee jjl at pobox.com
Tue Jul 29 16:55:12 EDT 2003


rosendo at dbora.com (rosendo) writes:

> I'm writing a http client to test some applications.
> The code below it's the core of the client:
[snip]

You're using *both* urlopen *and* add_cookie_header / extract_cookies.
Don't do that.  urlopen calls those methods itself.

Also, HTTPError is a subclass of URLError, so your second except:
suite will never be reached.  I'm not sure what the purpose of the
empty except: there is, either -- why not just let the exception
propogate to top level?

The unreleased version of ClientCookie can do Referer automatically.
Ask me if you want a copy (the release is waiting for somebody to look
at an RFE for urllib2 that I've posted).

Also, Python doesn't use semicolons <wink>, and the second 'argument'
to except receives an exception, not a exception message (string).


> Before this code we declare c = ClientCookie.Cookies(), etc......
> Then i don't know how to know the status of the response, because the
> property:
> response.info().status seems not to function like i wait, that it's
> with an 200, 301, 400, etc.....
> How can obtein this status?

If you get a returned response, it's always 200.  For non-200
responses, urllib2 always raises an exception.  It's probably a wart,
but too late to change now.

Corrected code (untested, since incomplete):

try:
    start_ts = time.time()
    request = urllib2.Request('http://%s%s' % (req.host, req.path),
                              req.data, req.headers)
    request.add_header("Referer", 'http://%s%s' % (req.host, req.path))
    c.clear()
    #request = urllib2.Request('http://www.google.com')
    try:                    
        response = urllib2.urlopen(request)
        data = response.read()
        headers = response.info().headers
        #status = response.info().status
    except urllib2.HTTPError, e:
        print "URLError: ", e
        # Note: e.msg, e.code are what you want.
        # IIRC, HTTPError works just like a response object, too.
    duration = time.time() - start_ts

    print ('The result had status: 200 duration: %5.2f http://%s%s' %
           (response.info().status, duration, req.host,req.path))
except:
    print sys.exc_type,sys.exc_value
    print 'cagadita....' + str(req)


John




More information about the Python-list mailing list