This error pops up randomly, and I’m pretty sure it’s because the infoGotten variable isn’t initialized before the return statement calls it. The part that has me puzzled is how it’s getting to that part of the code to produce that error in the first place. Hopefully someone can explain to me why that is as I haven’t been able to figure it out yet. I’m guessing it’s because of the try/except statement but I did some searching and checked 7.4 in the manual and it doesn’t appear (to me anyways) that I’m doing something incorrect.
breakLoop = 0
def get_item_info(linkParameters):
global breakLoop
nheaders = {'User-Agent' : 'Firefox/15.0.1'}
purl = 'http://example.com/something.php'
pd = linkParameters
nreq = urllib.request.Request(purl, pd, nheaders)
if breakLoop >= 4:
return 'Request timed out {} times'.format(breakLoop)
try:
nresponse = urllib.request.urlopen(nreq)
except urllib.error.URLError:
breakLoop += 1
get_item_info(pd)
except urllib.error.HTTPError:
breakLoop += 1
get_item_info(pd)
else:
infoGotten = nresponse.read()
return infoGotten
Thanks!
You need to return the results of the recursive calls, so it should be
return get_item_info(pd)in theexceptclauses (which I combined below):Recursion seems like a weird way to perform the retries though, why not use a loop? The following seems more clear: