Currently I have a program that has proxies and makes a request to get my ip address with that proxy and returns it back in json.
An example request back is this:
Got Back: {'ip': '91.67.240.45', 'country': 'Germany', 'cc': 'DE'}
I want my program to try and make a request to the url and if it does not get the request because the proxy is down I want to try again 5 times before moving onto the next ip address.
I thought this except block would work but it is not breaking out of the loop when the 5 iterations are over and I am not sure why.
My program does however work when the proxy is up for the first try as it breaks after the first attempt and then moves onto the next ip address.
Here is what I currently have:
import requests import time proxies = [ "95.87.220.19:15600", "91.67.240.45:3128", "85.175.216.32:53281", "91.236.251.131:8118", "91.236.251.131:8118", "88.99.10.249:1080", ] def sol(ip): max_tries = 5 for i in range(1, max_tries+1): try: print(f"Using Proxy: {ip}") r = requests.get('https://api.myip.com', proxies={"https": ip}) print(f"Got Back: {r.json()}") break except OSError: time.sleep(5) print(f"Retrying...: {i}") break for i in proxies: sol(i) How can I make it s my loop has 5 tries before moving onto the next ip address.