1

I wanna swap using/notUsing Proxy when request failed, and I wrote the code below. But I found that if enable_proxy is true and the request failed, then the next try using urllib2.urlopen will also use proxy, why?

opener = urllib2.build_opener(proxy)
while read_succ == False
    read_succ = True
    f = None
    try:
        if enable_proxy == True:
            f = opener.open(req, timeout=cur_timeout)
        else:
            f = urllib2.urlopen(url=req, timeout=cur_timeout)
        content = f.read()
    except Exception,e:
        read_succ = False
        enable_proxy = not enable_proxy
    finally:
        if f:
            f.close()
Dodou Wang
  • 29
  • 7

0 Answers0