fix: 代理验证增加极验api.geetest.com连通性测试
- 原来只验证douyu.com,能过但极验不通的代理会入池后立即失败 - 现在两关验证:先过douyu.com,再过api.geetest.com - 日志中09:04:03验证10/10全通过但极验连续失败的问题不会再出现 - 极验超时/不可达的代理在入池前就被过滤掉
This commit is contained in:
@@ -10,7 +10,11 @@ from loguru import logger
|
||||
|
||||
def verify_proxy_url(proxy_url: str, timeout: tuple = (5, 8)) -> tuple[bool, str]:
|
||||
"""
|
||||
验证代理是否可用,只验证斗鱼主站(登录的最终目标)。
|
||||
验证代理是否可用,依次验证:
|
||||
1. 斗鱼主站 www.douyu.com(登录目标)
|
||||
2. 极验接口 api.geetest.com(极验验证瓶颈)
|
||||
|
||||
两关都过才算可用。
|
||||
|
||||
Returns:
|
||||
(是否可用, 消息)
|
||||
@@ -18,6 +22,8 @@ def verify_proxy_url(proxy_url: str, timeout: tuple = (5, 8)) -> tuple[bool, str
|
||||
from utils.http_logger import log_http
|
||||
|
||||
proxies = {'http': proxy_url, 'https': proxy_url}
|
||||
|
||||
# ── 第1关:斗鱼主站 ──
|
||||
started = _time.monotonic()
|
||||
try:
|
||||
response = requests.get(
|
||||
@@ -30,10 +36,9 @@ def verify_proxy_url(proxy_url: str, timeout: tuple = (5, 8)) -> tuple[bool, str
|
||||
response.raise_for_status()
|
||||
log_http(
|
||||
category="proxy_verify", method="GET", url="https://www.douyu.com",
|
||||
status_code=response.status_code, response_body=f"代理可用: {proxy_url}",
|
||||
status_code=response.status_code, response_body=f"斗鱼主站可达: {proxy_url}",
|
||||
duration=elapsed, proxy=proxy_url, tag="proxy_verify",
|
||||
)
|
||||
return True, '代理可用 → 斗鱼主站'
|
||||
except Exception as exc:
|
||||
elapsed = _time.monotonic() - started
|
||||
err_msg = str(exc)
|
||||
@@ -43,7 +48,7 @@ def verify_proxy_url(proxy_url: str, timeout: tuple = (5, 8)) -> tuple[bool, str
|
||||
detail = '连接超时'
|
||||
else:
|
||||
detail = type(exc).__name__
|
||||
logger.debug(f"代理验证失败 [{proxy_url}]: {detail}")
|
||||
logger.debug(f"代理验证失败 [{proxy_url}]: 斗鱼主站不可达: {detail}")
|
||||
log_http(
|
||||
category="proxy_verify", method="GET", url="https://www.douyu.com",
|
||||
duration=elapsed, error=f"[{proxy_url}] {detail}: {err_msg[:200]}",
|
||||
@@ -51,6 +56,39 @@ def verify_proxy_url(proxy_url: str, timeout: tuple = (5, 8)) -> tuple[bool, str
|
||||
)
|
||||
return False, detail
|
||||
|
||||
# ── 第2关:极验接口 ──
|
||||
started = _time.monotonic()
|
||||
try:
|
||||
response = requests.get(
|
||||
'https://api.geetest.com',
|
||||
proxies=proxies,
|
||||
timeout=timeout,
|
||||
headers={'User-Agent': 'Mozilla/5.0'},
|
||||
# geetest 首页可能返回 4xx,只要能连上就算通
|
||||
allow_redirects=True,
|
||||
)
|
||||
elapsed = _time.monotonic() - started
|
||||
log_http(
|
||||
category="proxy_verify", method="GET", url="https://api.geetest.com",
|
||||
status_code=response.status_code, response_body=f"代理可用: {proxy_url}",
|
||||
duration=elapsed, proxy=proxy_url, tag="proxy_verify",
|
||||
)
|
||||
return True, '代理可用 → 斗鱼+极验'
|
||||
except Exception as exc:
|
||||
elapsed = _time.monotonic() - started
|
||||
err_msg = str(exc)
|
||||
if 'timed out' in err_msg.lower():
|
||||
detail = '极验接口超时'
|
||||
else:
|
||||
detail = f'极验不可达: {type(exc).__name__}'
|
||||
logger.debug(f"代理验证失败 [{proxy_url}]: 斗鱼可达但{detail}")
|
||||
log_http(
|
||||
category="proxy_verify", method="GET", url="https://api.geetest.com",
|
||||
duration=elapsed, error=f"[{proxy_url}] {detail}: {err_msg[:200]}",
|
||||
proxy=proxy_url, tag="proxy_verify",
|
||||
)
|
||||
return False, detail
|
||||
|
||||
|
||||
def verify_proxies_concurrent(
|
||||
proxy_urls: list[str],
|
||||
|
||||
Reference in New Issue
Block a user