async def check_proxy(proxy_url: str, timeout=None) -> tuple: results = [] for checker in settings.PROXY_CHECKERS: result = await checker.check(proxy_url, timeout=timeout) if result[0] and settings.PROXY_CHECKING_CONDITION == 'or': return result results.append(result) additional_information = CheckerResult() if settings.PROXY_CHECKING_CONDITION == 'and': for result in results: if not result[0]: return False, additional_information additional_information.update_from_other(result[1]) return True, additional_information return False, additional_information
async def check_proxy(proxy_url: str, timeout=None) -> tuple: if not settings.PROXY_CHECKERS: raise Exception('add at least one checker') checkers = copy.copy(settings.PROXY_CHECKERS) random.shuffle(checkers) results = [] for checker, _ in zip(checkers, range( settings.MINIMUM_NUMBER_OF_CHECKERS_PER_PROXY)): checker() result = await checker().check(proxy_url, timeout=timeout) if not result[0]: return False, None results.append(result) additional_information = CheckerResult() for result in results: additional_information.update_from_other(result[1]) return True, additional_information
async def _check(self, response: aiohttp.ClientResponse, checker_result: CheckerResult): if response.status != 200: return False try: json_result = await response.json() checker_result.ipv4 = json_result['ip'] checker_result.city = json_result['city'] checker_result.region = json_result['region'] checker_result.country_code = json_result['country'] checker_result.location_coordinates = tuple( float(x) for x in json_result['loc'].split(',')) checker_result.organization_name = json_result['org'] return True # TODO: change to actual exception except BaseException as ex: traceback.print_exc() raise ex return False
async def validate(self, response: aiohttp.ClientResponse, checker_result: CheckerResult) -> bool: if response.status != 200: return False json_result = await response.json() if "ip" in json_result: checker_result.ipv4 = json_result["ip"] if "city" in json_result: checker_result.city = json_result["city"] if "region" in json_result: checker_result.region = json_result["region"] if "country" in json_result: checker_result.country_code = json_result["country"] if "loc" in json_result: checker_result.location_coordinates = tuple( float(x) for x in json_result["loc"].split(",")) if "org" in json_result: checker_result.organization_name = json_result["org"] return True
async def validate(self, response: aiohttp.ClientResponse, checker_result: CheckerResult) -> bool: if response.status != 200: return False json_result = await response.json() if 'ip' in json_result: checker_result.ipv4 = json_result['ip'] if 'city' in json_result: checker_result.city = json_result['city'] if 'region' in json_result: checker_result.region = json_result['region'] if 'country' in json_result: checker_result.country_code = json_result['country'] if 'loc' in json_result: checker_result.location_coordinates = tuple( float(x) for x in json_result['loc'].split(',')) if 'org' in json_result: checker_result.organization_name = json_result['org'] return True