AMSC 请求从 httpx 改为 urllib,彻底绕过代理干扰
This commit is contained in:
@@ -10,12 +10,13 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
import ssl
|
||||||
import time
|
import time
|
||||||
from datetime import datetime, timedelta, timezone
|
from datetime import datetime, timedelta, timezone
|
||||||
from typing import Any, Dict, Optional
|
from typing import Any, Dict, Optional
|
||||||
from urllib.parse import quote
|
from urllib.parse import quote
|
||||||
|
from urllib.request import Request, urlopen
|
||||||
|
|
||||||
import httpx
|
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
|
||||||
from src.utils.metrics import record_source_call
|
from src.utils.metrics import record_source_call
|
||||||
@@ -191,11 +192,14 @@ class AmscAwosSourceMixin:
|
|||||||
return headers
|
return headers
|
||||||
|
|
||||||
def _http_get_json(self, url: str, *, headers: Optional[Dict[str, str]] = None) -> Optional[Dict[str, Any]]:
|
def _http_get_json(self, url: str, *, headers: Optional[Dict[str, str]] = None) -> Optional[Dict[str, Any]]:
|
||||||
with httpx.Client(verify=False, trust_env=False) as client:
|
ctx = ssl.create_default_context()
|
||||||
response = client.get(url, headers=headers, timeout=getattr(self, "timeout", 10.0))
|
ctx.check_hostname = False
|
||||||
response.raise_for_status()
|
ctx.verify_mode = ssl.CERT_NONE
|
||||||
|
req = Request(url, headers=headers or {})
|
||||||
|
with urlopen(req, timeout=getattr(self, "timeout", 10.0), context=ctx) as resp:
|
||||||
|
raw = resp.read().decode("utf-8", errors="replace")
|
||||||
try:
|
try:
|
||||||
return response.json()
|
return json.loads(raw)
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user