Writing a class which essentially spiders a site and saves the files locally. On a URLError exception, it sleeps for a second and tries again (on 404 it just moves on). The relevant bit of code, including the offending method:<br>
<br>class Handler(threading.Thread):<br> def __init__(self, url):<br> threading.Thread.__init__(self)<br> self.url = url<br><br> def save(self, uri, location):<br> try:<br>
handler = urllib2.urlopen(uri)<br> except urllib2.HTTPError, e:<br> if e.code == 404:<br> return<br> else:<br>
print "retrying %s (HTTPError)" % uri<br> time.sleep(1)<br> self.save(uri, location)<br> except urllib2.URLError, e:<br>
print "retrying %s" % uri<br> time.sleep(1)<br> self.save(uri, location)<br><br> if not os.path.exists(os.path.dirname(location)):<br>
os.makedirs(os.path.dirname(location))<br><br> file = open(location, "w")<br> file.write(handler.read())<br> file.close()<br><br>...<br><br>But what I am seeing is that after a retry (on catching a URLError exception), I see bunches of "UnboundLocalError: local variable 'handler' referenced before assignment" errors on line 38, which is the "file.write(handler.read())" line..<br>
<br>What gives?<br clear="all"><br>-- <br>Wells Oliver<br><a href="mailto:wells@submute.net">wells@submute.net</a><br>