1

I have a script that runs and submits urls in a text file via GET to an API and saves the response to a text file. However, the for loop quits if I get a failure in the first section and does not continue passing the others. How can I still grab the failure and continue on with the others without the script exiting before it finishes?

sys.stdout=open("mylog.txt","w")
for row in range(0, len(exampleData)):  
    url = exampleData[row][0]
    print (url)
    response = requests.get(url, auth=(user, pwd))
    if response.status_code != 200:
        print('Failure Message {}' .format(response.text))
        work = 'failed'
        continue
    data = json.loads(response.text)    
    print(data)
    work = 'succeeded'
sys.stdout.close()

3 Answers3

2

Use continue instead of exit()

Hui-Yu Lee
  • 909
  • 8
  • 20
  • Tried that, but my log file still only showed the first failure not all with continue. It did seem to process longer however. – user3691635 Jun 10 '16 at 15:41
0

Use an exception to catch the failure and continue on.

Lucas
  • 5,071
  • 1
  • 18
  • 17
0

Now that your loop control is corrected, it must be working correctly. It will print out a failure message every time it gets an error response (not 200). If you're only seeing one error message, you're only getting one non-200 response from the other side. If that's not what you expect, the problem is on the server side. (Or in the contents of exampleData.)

You need to debug your own server-client system. Simplify this loop so that the only thing it does is print diagnostic information about the response (e.g., print status_code), and find out what's really going on.

alexis
  • 48,685
  • 16
  • 101
  • 161