From 46a652bb27830fbfeb54a71f9fe8da315773d380 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=9D=E7=94=9F?= <65069312+ChangSheng3407@users.noreply.github.com> Date: Mon, 9 Jun 2025 14:41:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8F=92=E4=BB=B6=E5=95=86?= =?UTF-8?q?=E5=BA=97=E8=8E=B7=E5=8F=96=E6=8F=92=E4=BB=B6=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E6=8F=90=E7=A4=BA302=E5=BC=82=E5=B8=B8=20(#1904)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 由于调用加速地址的时候状态码为302会被认为正常返回,但是所有的加速地址又没有调用完毕导致的插件商店经常报错302异常 * 优化异常代码行 * :rotating_light: auto fix by pre-commit hooks --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- zhenxun/builtin_plugins/plugin_store/data_source.py | 4 ++-- zhenxun/utils/http_utils.py | 11 ++++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/zhenxun/builtin_plugins/plugin_store/data_source.py b/zhenxun/builtin_plugins/plugin_store/data_source.py index 6e662a81..d21ba9cd 100644 --- a/zhenxun/builtin_plugins/plugin_store/data_source.py +++ b/zhenxun/builtin_plugins/plugin_store/data_source.py @@ -91,8 +91,8 @@ class ShopManage: "plugins.json" ) extra_github_url = await extra_github_repo.get_raw_download_urls("plugins.json") - res = await AsyncHttpx.get(default_github_url) - res2 = await AsyncHttpx.get(extra_github_url) + res = await AsyncHttpx.get(default_github_url, check_status_code=200) + res2 = await AsyncHttpx.get(extra_github_url, check_status_code=200) # 检查请求结果 if res.status_code != 200 or res2.status_code != 200: diff --git a/zhenxun/utils/http_utils.py b/zhenxun/utils/http_utils.py index 962c9e01..e5f2492f 100644 --- a/zhenxun/utils/http_utils.py +++ b/zhenxun/utils/http_utils.py @@ -43,6 +43,7 @@ class AsyncHttpx: use_proxy: bool = True, proxy: dict[str, str] | None = None, timeout: int = 30, # noqa: ASYNC109 + check_status_code: int | None = None, **kwargs, ) -> Response: """Get @@ -56,6 +57,7 @@ class AsyncHttpx: use_proxy: 使用默认代理 proxy: 指定代理 timeout: 超时时间 + check_status_code: 检查状态码 """ urls = [url] if isinstance(url, str) else url return await cls._get_first_successful( @@ -67,6 +69,7 @@ class AsyncHttpx: use_proxy=use_proxy, proxy=proxy, timeout=timeout, + check_status_code=check_status_code, **kwargs, ) @@ -74,12 +77,18 @@ class AsyncHttpx: async def _get_first_successful( cls, urls: list[str], + check_status_code: int | None = None, **kwargs, ) -> Response: last_exception = None for url in urls: try: - return await cls._get_single(url, **kwargs) + logger.info(f"开始获取 {url}..") + response = await cls._get_single(url, **kwargs) + if check_status_code and response.status_code != check_status_code: + status_code = response.status_code + raise Exception(f"状态码错误:{status_code}!={check_status_code}") + return response except Exception as e: last_exception = e if url != urls[-1]: