In article <hilruv$nu...@panix5.panix.com>, Aahz <a...@pythoncraft.com> wrote: >In article <mailman.233.1262197919.28905.python-l...@python.org>, >Philip Semanchuk <phi...@semanchuk.com> wrote: >> >>While I don't fully understand what you're trying to accomplish by >>changing the URL to google.com after 3 iterations, I suspect that some >>of your trouble comes from using "while True". Your code would be >>clearer if the while clause actually stated the exit condition. Here's >>a suggestion (untested): >> >>MAX_ATTEMPTS = 5 >> >>count = 0 >>while count <= MAX_ATTEMPTS: >> count += 1 >> try: >> print 'attempt ' + str(count) >> request = urllib2.Request(url, None, headers) >> response = urllib2.urlopen(request) >> if response: >> print 'True response.' >> except URLError: >> print 'fail ' + str(count) > >Note that you may have good reason for doing it differently: > >MAX_ATTEMPTS = 5 >def retry(url): > count = 0 > while True: > count += 1 > try: > print 'attempt', count > request = urllib2.Request(url, None, headers) > response = urllib2.urlopen(request) > if response: > print 'True response' ^^^^^ Oops, that print should have been a return.
> except URLError: > if count < MAX_ATTEMPTS: > time.sleep(5) > else: > raise > >This structure is required in order for the raise to do a proper >re-raise. -- Aahz (a...@pythoncraft.com) <*> http://www.pythoncraft.com/ "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." --Red Adair -- http://mail.python.org/mailman/listinfo/python-list