From 19ef6a530aaf5b35b0f51037b3980ac1e06e23c5 Mon Sep 17 00:00:00 2001 From: binary-husky Date: Sun, 30 Jul 2023 13:23:35 +0800 Subject: [PATCH] add additonal source for checking proxy ip --- check_proxy.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/check_proxy.py b/check_proxy.py index fd5e276..474988c 100644 --- a/check_proxy.py +++ b/check_proxy.py @@ -3,15 +3,18 @@ def check_proxy(proxies): import requests proxies_https = proxies['https'] if proxies is not None else '无' try: - response = requests.get("https://ipapi.co/json/", - proxies=proxies, timeout=4) + response = requests.get("https://ipapi.co/json/", proxies=proxies, timeout=4) data = response.json() print(f'查询代理的地理位置,返回的结果是{data}') if 'country_name' in data: country = data['country_name'] result = f"代理配置 {proxies_https}, 代理所在地:{country}" elif 'error' in data: - result = f"代理配置 {proxies_https}, 代理所在地:未知,IP查询频率受限" + alternative = _check_with_backup_source(proxies) + if alternative is None: + result = f"代理配置 {proxies_https}, 代理所在地:未知,IP查询频率受限" + else: + result = f"代理配置 {proxies_https}, 代理所在地:{alternative}" else: result = f"代理配置 {proxies_https}, 代理数据解析失败:{data}" print(result) @@ -21,6 +24,11 @@ def check_proxy(proxies): print(result) return result +def _check_with_backup_source(proxies): + import random, string, requests + random_string = ''.join(random.choices(string.ascii_letters + string.digits, k=32)) + try: return requests.get(f"http://{random_string}.edns.ip-api.com/json", proxies=proxies, timeout=4).json()['dns']['geo'] + except: return None def backup_and_download(current_version, remote_version): """