移除HTTP详情请求日志
This commit is contained in:
@@ -22,7 +22,6 @@ from core.geetest.common.network import (
|
||||
get_c_s,
|
||||
req_fullpage_validate,
|
||||
)
|
||||
from utils.http_logger import log_http
|
||||
|
||||
|
||||
class AccountLike(Protocol):
|
||||
@@ -246,14 +245,9 @@ class DouyuLogin:
|
||||
"""
|
||||
统一发送请求,附带分段超时和更明确的错误信息。
|
||||
代理连接失败时自动重试获取新的代理IP。
|
||||
所有请求详情会记录到 HTTP 详情日志。
|
||||
"""
|
||||
timeout = kwargs.pop('timeout', self.timeout)
|
||||
safe_url = self._safe_url(url)
|
||||
req_data = kwargs.get('data')
|
||||
req_body = req_data if req_data else kwargs.get('json')
|
||||
current_proxy = self._current_proxy_url
|
||||
tag = self.account.username if self.account else ""
|
||||
|
||||
for attempt in range(max_retries):
|
||||
self._ensure_not_stopped()
|
||||
@@ -266,30 +260,9 @@ class DouyuLogin:
|
||||
f"{method.upper()} {safe_url} -> {response.status_code} "
|
||||
f"({elapsed:.2f}s)"
|
||||
)
|
||||
# 记录请求/响应详情
|
||||
resp_body = response.text[:500] if response.text else ""
|
||||
log_http(
|
||||
category="douyu_login",
|
||||
method=method,
|
||||
url=safe_url,
|
||||
request_headers=dict(self.session.headers),
|
||||
request_body=req_body,
|
||||
status_code=response.status_code,
|
||||
response_headers=dict(response.headers),
|
||||
response_body=resp_body,
|
||||
duration=elapsed,
|
||||
proxy=current_proxy,
|
||||
tag=tag,
|
||||
)
|
||||
return response
|
||||
except requests.Timeout as exc:
|
||||
elapsed = time.monotonic() - started
|
||||
log_http(
|
||||
category="douyu_login", method=method, url=safe_url,
|
||||
request_headers=dict(self.session.headers), request_body=req_body,
|
||||
duration=elapsed, error=f"超时 timeout={timeout}: {exc}",
|
||||
proxy=current_proxy, tag=tag,
|
||||
)
|
||||
raise TimeoutError(
|
||||
f"{method.upper()} {safe_url} 超时,耗时 {elapsed:.1f}s,"
|
||||
f"timeout={timeout}"
|
||||
@@ -298,18 +271,10 @@ class DouyuLogin:
|
||||
elapsed = time.monotonic() - started
|
||||
err_str = str(exc)
|
||||
is_proxy_err = "proxy" in err_str.lower() or "Proxy" in type(exc).__name__
|
||||
log_http(
|
||||
category="douyu_login", method=method, url=safe_url,
|
||||
request_headers=dict(self.session.headers), request_body=req_body,
|
||||
duration=elapsed,
|
||||
error=f"{'代理' if is_proxy_err else ''}连接失败: {err_str[:300]}",
|
||||
proxy=current_proxy, tag=tag,
|
||||
)
|
||||
if is_proxy_err:
|
||||
logger.warning(f"代理连接失败,尝试 {attempt + 1}/{max_retries}: {exc}")
|
||||
if attempt < max_retries - 1:
|
||||
self._refresh_proxy()
|
||||
current_proxy = self._current_proxy_url
|
||||
self._sleep_interruptible(1)
|
||||
continue
|
||||
raise ConnectionError(
|
||||
@@ -325,12 +290,6 @@ class DouyuLogin:
|
||||
) from exc
|
||||
except requests.RequestException as exc:
|
||||
elapsed = time.monotonic() - started
|
||||
log_http(
|
||||
category="douyu_login", method=method, url=safe_url,
|
||||
request_headers=dict(self.session.headers), request_body=req_body,
|
||||
duration=elapsed, error=f"请求异常: {exc}",
|
||||
proxy=current_proxy, tag=tag,
|
||||
)
|
||||
raise ConnectionError(
|
||||
f"{method.upper()} {safe_url} 请求失败,耗时 {elapsed:.1f}s: {exc}"
|
||||
) from exc
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
from typing import Optional
|
||||
import time as _time
|
||||
|
||||
import requests
|
||||
from loguru import logger
|
||||
@@ -19,12 +18,9 @@ def verify_proxy_url(proxy_url: str, timeout: tuple = (5, 8)) -> tuple[bool, str
|
||||
Returns:
|
||||
(是否可用, 消息)
|
||||
"""
|
||||
from utils.http_logger import log_http
|
||||
|
||||
proxies = {'http': proxy_url, 'https': proxy_url}
|
||||
|
||||
# ── 第1关:斗鱼主站 ──
|
||||
started = _time.monotonic()
|
||||
try:
|
||||
response = requests.get(
|
||||
'https://www.douyu.com',
|
||||
@@ -32,15 +28,8 @@ def verify_proxy_url(proxy_url: str, timeout: tuple = (5, 8)) -> tuple[bool, str
|
||||
timeout=timeout,
|
||||
headers={'User-Agent': 'Mozilla/5.0'},
|
||||
)
|
||||
elapsed = _time.monotonic() - started
|
||||
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}",
|
||||
duration=elapsed, proxy=proxy_url, tag="proxy_verify",
|
||||
)
|
||||
except Exception as exc:
|
||||
elapsed = _time.monotonic() - started
|
||||
err_msg = str(exc)
|
||||
if 'Tunnel connection failed' in err_msg or '503' in err_msg:
|
||||
detail = '代理拒绝连接(白名单可能未生效)'
|
||||
@@ -49,15 +38,9 @@ def verify_proxy_url(proxy_url: str, timeout: tuple = (5, 8)) -> tuple[bool, str
|
||||
else:
|
||||
detail = type(exc).__name__
|
||||
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]}",
|
||||
proxy=proxy_url, tag="proxy_verify",
|
||||
)
|
||||
return False, detail
|
||||
|
||||
# ── 第2关:极验接口 ──
|
||||
started = _time.monotonic()
|
||||
try:
|
||||
response = requests.get(
|
||||
'https://api.geetest.com',
|
||||
@@ -67,26 +50,14 @@ def verify_proxy_url(proxy_url: str, timeout: tuple = (5, 8)) -> tuple[bool, str
|
||||
# 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
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user