python - Continuously looping, unable to work out what's going wrong -


my code below isn't working how want to, i've tried comment little make easier.

basically, want try maximum of 3 times access website , if successful exit loop , continue, if fails 3 times exit out of function.

import random import urllib2 import httplib import urllib import mechanize  def test():      ## 3 attempts...     in range(0, 3):         ## while in 3 attempts...         while true:             ## try...             try:                 print "trying"                 ## proxy list                 proxy_list = {"no proxy": "none"}                  ## randomly chosen proxy                 proxy_number = random.choice(proxy_list.keys())                  ## url post in order data.                 post_url = ""                  browser = mechanize.browser()                 browser.set_handle_robots(false)                 browser.addheaders = [('user-agent', 'firefox')]                 parameters = {""}                 data = urllib.urlencode(parameters)                  ## if proxy_number = no proxy then...                 if proxy_number == "no proxy":                     ## not setup proxy details                     proxy_details = none                 ## if proxy_number real proxy then...                 else:                     ## proxy details                     proxy_details = proxy_list[proxy_number]                     ## setup proxy                     browser.set_proxies({"http": proxy_number})                  ## contact webpage                 trans_array = browser.open(post_url).read().decode('utf-8')                 print trans_array                 ## if exit loop                 break             ## on exceptions             except:                 ## if unsuccessful continue , retry                 continue             ## end current loop         break             ## if unsuccessful after 3 attempts return false         return      print trans_array  test() 

can explain i'm doing wrong?

edit: falsetru

def test():     in range(3):         try:             ...             break     except:         counter = counter  + 1         print counter          continue      if counter  == 3:         return false 

thanks in advance - hyflex

if error occur, continue executed; while loop never end if error continuously raised.

def test():     in range(0, 3):         while true:             try:                 ...                 break             except:                 continue # <---         break 

imho, while loop unnecessary:

def test():     in range(3):         try:             ...             break         except:             continue     else:         return # reach here if loop not ended break     print trans_array 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -