More Pythonic?

Jeremy Hylton jeremy at cnri.reston.va.us
Wed Sep 15 11:36:09 EDT 1999


>>>>> "AM" == Aahz Maruch <aahz at netcom.com> writes:

  AM> Aside from the obvious glitch that you need to use
  AM> urllib2.HTTPError, your code generates a NameError on resp.  I
  AM> think I know how to fix it, but I'd like you to tell me in case
  AM> I miss something obvious.

Sorry -- a bogus example written in a hurry.

The proper way to use the code would be:

import urllib2

try:
    resp = urllib2.urlopen('http://frob.nitz/')
except urllib2.HTTPError, resp:
    print resp.code, resp.msg
    # or do some test on the resp.code and continue only if it is
    # one of the error you expect to deal with

  AM> Overall, your code looks similar to the code I was planning to
  AM> write, but the big feature your code is missing is the stack of
  AM> redirects.

I'm not entirely clear on what the "stack of redirects" feature is.  I 
assume you're concenred about a case with multiple redirects, e.g.
   URL1 -> URL2 -> URL3 -> URL4,
where the last redirect raises an error.  What do you want to do at
this point?  The Python call stack does contain separate request
objects for each redirect, but it isn't obvious how to do anything
with them.

Jeremy





More information about the Python-list mailing list