I\'m trying this simple code, but the damn break doesn\'t work... what is wrong?
while True:
for proxy in proxylist:
try:
h = urllib.urlo
You can use a custom exception and then catch it:
exit_condition = False
try:
<some code ...>
if exit_conditon is True:
raise UnboundLocalError('My exit condition was met. Leaving try block')
<some code ...>
except UnboundLocalError, e:
print 'Here I got out of try with message %s' % e.message
pass
except Exception, e:
print 'Here is my initial exception'
finally:
print 'Here I do finally only if I want to'
You just break out of for
loop -- not while
loop:
running = True
while running:
for proxy in proxylist:
try:
h = urllib.urlopen(website, proxies = {'http': proxy}).readlines()
print 'worked %s' % proxy
running = False
except:
print 'error %s' % proxy
print 'done'
You break out of the for
loop only, so you never leave the while loop and restart iterating over the proxylist
over and over again. Just omit the surrounding while loop, I actually don't understand why you enclosed the code in a while True
in the first place.
break is breaking the innermost loop, which is the for
loop in your case. To break from more than one loop you have few options:
return
but in your case you actually don't need the outer while
loop at all. Just remove it.