Re: urllib2 http status

Discussion in 'Python' started by John J. Lee, Jul 29, 2003.

  1. John J. Lee

    John J. Lee Guest

    (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
     
    John J. Lee, Jul 29, 2003
    #1
    1. Advertising

  2. John J. Lee

    rosendo Guest

    Thanks again John.
    The code goes fine.
    Rosendo.


    (John J. Lee) wrote in message news:<>...
    > (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
     
    rosendo, Jul 31, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. saha
    Replies:
    0
    Views:
    4,069
  2. Josef Cihal
    Replies:
    0
    Views:
    761
    Josef Cihal
    Sep 5, 2005
  3. Shug
    Replies:
    13
    Views:
    902
    Philipp
    Dec 15, 2006
  4. Shug
    Replies:
    13
    Views:
    957
    Philipp
    Dec 15, 2006
  5. harryos
    Replies:
    0
    Views:
    388
    harryos
    Oct 13, 2010
Loading...

Share This Page