BOM发料对比

This commit is contained in:
hjq
2026-06-26 18:14:28 +08:00
parent d362e2daf5
commit ecbadd41e1

View File

@@ -59,39 +59,55 @@ def patch_drission_ws_handshake() -> None:
base_kwargs["http_proxy_host"] = None base_kwargs["http_proxy_host"] = None
base_kwargs["http_proxy_port"] = None base_kwargs["http_proxy_port"] = None
# Chrome 149 严格校验,如果 suppress_origin=True 且没有合法 Host/Origin会被 404
# 我们这里准备几个干净的 base_kwargs 变体
clean_kwargs = dict(base_kwargs)
clean_kwargs.pop("suppress_origin", None)
candidate_kwargs = [ candidate_kwargs = [
# 策略1最干净的连接什么都不加 (根据测试脚本,测试 1 成功) # 策略1最干净的连接强制关闭 suppress_origin
{ {
**base_kwargs, **clean_kwargs,
"suppress_origin": False
}, },
# 策略2DrissionPage 默认的抑制 Origin (根据测试脚本,测试 2 成功) # 策略2带上明确的 Origin
{ {
**base_kwargs, **clean_kwargs,
"suppress_origin": True
},
# 策略3加上 localhost Origin
{
**base_kwargs,
"suppress_origin": False, "suppress_origin": False,
"header": ["Origin: http://localhost"] "header": ["Origin: http://127.0.0.1"]
},
# 策略3尊重原始调用的参数DrissionPage 默认行为)
{
**base_kwargs
} }
] ]
last_err = None last_err = None
for candidate in candidate_kwargs: for i, candidate in enumerate(candidate_kwargs, 1):
try: try:
# 强制使用 127.0.0.1,因为在 Docker 内 localhost 可能解析异常 # 强制使用 127.0.0.1,因为在 Docker 内 localhost 可能解析异常
target_url = address.replace("localhost", "127.0.0.1") target_url = address.replace("localhost", "127.0.0.1")
log("INFO", f"[DEBUG-WS] 尝试策略 {i} 连接 {target_url} (suppress_origin={candidate.get('suppress_origin')}, header={candidate.get('header')})")
return raw_ws_create_connection(target_url, **candidate) return raw_ws_create_connection(target_url, **candidate)
except WebSocketBadStatusException as ws_err: except WebSocketBadStatusException as ws_err:
log("WARN", f"[DEBUG-WS] 策略 {i} 失败 (WebSocketBadStatusException): {ws_err}")
last_err = ws_err last_err = ws_err
except Exception as other_err: except Exception as other_err:
log("WARN", f"[DEBUG-WS] 策略 {i} 失败 (Exception): {other_err}")
last_err = other_err last_err = other_err
break break
raise last_err raise last_err
if is_linux_env(): if is_linux_env():
# 如果是老版的 create_connection替换它
dp_driver_module.create_connection = resilient_create_connection dp_driver_module.create_connection = resilient_create_connection
# 覆盖 websocket-client 原生的 create_connection 避免某些模块直接引用
import websocket
websocket.create_connection = resilient_create_connection
# 还有可能是通过导入的方式,比如 from websocket import create_connection
# 我们需要在 driver.py 中覆盖它
import DrissionPage._base.driver as driver_mod
driver_mod.create_connection = resilient_create_connection
dp_driver_module._DTSK_WS_PATCHED = True dp_driver_module._DTSK_WS_PATCHED = True
@@ -307,11 +323,13 @@ def get_page(headless: bool = False, port: int = 9222) -> ChromiumPage:
# #endregion # #endregion
try: try:
log("INFO", f"[DEBUG] 准备实例化 ChromiumPage, address={opt.address}, local_port={opt.local_port}")
page = ChromiumPage(opt) page = ChromiumPage(opt)
log("OK", "[DEBUG] ChromiumPage 实例化成功!")
return page return page
except Exception as e: except Exception as e:
actual_address = opt.address or f"127.0.0.1:{port}" actual_address = opt.address or f"127.0.0.1:{port}"
log("WARN", f"[DEBUG] ChromiumPage 初始化失败: {e}") log("WARN", f"[DEBUG] ChromiumPage 初始化失败 (第一次尝试): {e}")
devtools_payload = probe_devtools_endpoints(actual_address, log_output=True) if opt.address else {"version": None, "list": None} devtools_payload = probe_devtools_endpoints(actual_address, log_output=True) if opt.address else {"version": None, "list": None}
fallback_page_ws = get_first_page_ws_address(devtools_payload) fallback_page_ws = get_first_page_ws_address(devtools_payload)
@@ -350,10 +368,10 @@ def get_page(headless: bool = False, port: int = 9222) -> ChromiumPage:
new_co.set_browser_path(browser_path) new_co.set_browser_path(browser_path)
page = ChromiumPage(new_co) page = ChromiumPage(new_co)
log("OK", "[DEBUG] 清理后重试成功!") log("OK", "[DEBUG] 清理后重试实例化 ChromiumPage 成功!")
return page return page
except Exception as retry_e: except Exception as retry_e:
log("ERR", f"[DEBUG] 清理后重试依然失败: {retry_e}") log("ERR", f"[DEBUG] 清理后重试依然失败 (第二次尝试): {retry_e}")
e = retry_e e = retry_e
# #region debug-point B:devtools-http-probe # #region debug-point B:devtools-http-probe