1

For various reasons I need to put a Requests call inside a try/except/retry loop, rather mounting a retry condition to a requests session. Expected behaviour is that if a request has been successful, the loop breaks and the code stops. Actual behaviour though is that it repeats the loop from start to finish, with the break statement seemingly having no effect:

import traceback
import requests
import time

for i in range(0, 15):
    while True:
        try:
            headers ={
            'authority': 'www.wikipedia.org',
            'method': 'GET',
            'path': '/wikipedia.org',
            'scheme': 'https',
            'accept': '*/*',
            'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8',
            'referer': 'https://google.com',
            'sec-fetch-dest': 'empty',
            'sec-fetch-mode': 'cors',
            'sec-fetch-site': 'same-origin',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36'
            }
            r = requests.get(url='https://wikipedia.org', headers=headers)
            print(i, r.status_code)
        except Exception as exc:
            time.sleep(60)
            print(traceback.format_exc())
            print('continue')
            continue
    
        print('break')
        break
    
    print('Finished')

What do I need to change to get the desired behaviour?

3
  • Which loop should be broken when the request is successful? while or for? Commented Apr 7, 2021 at 12:28
  • Is "break" being printed? What about "finished"? Either you don't expect the for loop to initiate another loop of the while loop, or there is an error being caught every time you make a request. Commented Apr 7, 2021 at 12:29
  • the loop should have 15 attempts at running successfully - at which ever iteration of the loop it is successful at, it should end. Commented Apr 7, 2021 at 12:30

3 Answers 3

1

Remove while cycle. while work under for. When python come to break it "break" while cycle.

It's work for me:

import traceback
import requests
import time

for i in range(0, 15):
    try:
        headers ={
        'authority': 'www.wikipedia.org',
        'method': 'GET',
        'path': '/wikipedia.org',
        'scheme': 'https',
        'accept': '*/*',
        'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8',
        'referer': 'https://google.com',
        'sec-fetch-dest': 'empty',
        'sec-fetch-mode': 'cors',
        'sec-fetch-site': 'same-origin',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36'
        }
        r = requests.get(url='https://wikipedia.org', headers=headers)
        print(i, r.status_code)
    except Exception as exc:
        time.sleep(60)
        print(traceback.format_exc())
        print('continue')
        continue

    print('break')
    break

print('Finished')

Sign up to request clarification or add additional context in comments.

Comments

0

try this:

import requests
from requests.exceptions import HTTPError


def get_response(url):

    for i in range(15):
        try:
            response = requests.get(url)
            response.raise_for_status()
            return response.content
        except HTTPError as http_err:
            print(f"HTTP error occurred: {http_err}")
        except Exception as err:
            print(f"Other error occurred: {err}")


url = 'http://google.com'
response = get_response(url)
print(response)
url = 'http://no_valid_address.com'
response = get_response(url)
print(response)

Comments

0

Expected behaviour is that if a request has been successful, the loop breaks and the code stops

the loop should have 15 attempts at running successfully - at which ever iteration of the loop it is successful at, it should end.

You don't need the while block. If you want to have 15 attempts or until first successful request, just do it.

import random


def random_error():
    if random.random() > 0.2:
        raise Exception


for _ in range(15):
    print('-' * 20)
    try:
        random_error()
    except Exception as exc:
        print('exception raised')
    else:
        print('exception not raised')
        break

print('Finished')


Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.