ScientryAPI / proxy.py
raannakasturi's picture
Add proxy handling to NLP functions and update .gitignore to exclude JSON files
67b6792
from swiftshadow.classes import Proxy
from requests import get, exceptions
def get_proxy():
proxies = {}
max_retries = 10 # Maximum number of attempts to find a valid proxy
attempts = 0
while attempts < max_retries:
try:
proxy = Proxy(
autoRotate=True,
cachePeriod=3,
).proxy()
proxies = {
'http': proxy[0],
'https': proxy[0]
}
resp = get('https://checkip.amazonws.com', proxies=proxies, timeout=10)
ip_address = resp.text.strip()
if ip_address == proxy[0].split(':')[0]:
print(f"Valid proxy found: {proxy[0]}")
return proxies
else:
print(f"Proxy {proxy[0]} did not match the response IP ({ip_address}). Retrying...")
except exceptions.RequestException as e:
print(f"Error testing proxy: {e}. Retrying...")
attempts += 1
raise Exception("Failed to find a valid proxy after multiple attempts.")