mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2025-12-15 14:22:55 +08:00
增强 httpx 兼容性
* 顺便修了对tx图片服务器的ssl错误
This commit is contained in:
parent
bcfb47d9fd
commit
1a69f95146
@ -23,6 +23,22 @@ from zhenxun.utils.user_agent import get_user_agent
|
||||
|
||||
# from .browser import get_browser
|
||||
|
||||
def get_async_client(proxies=None, **kwargs):
|
||||
from httpx._config import create_ssl_context
|
||||
context = create_ssl_context(
|
||||
verify=kwargs.get("verify", True),
|
||||
cert=kwargs.get("cert"),
|
||||
trust_env=kwargs.get("trust_env", True),
|
||||
)
|
||||
context.set_ciphers("DEFAULT")
|
||||
kwargs["verify"] = context
|
||||
try:
|
||||
return httpx.AsyncClient(proxies=proxies, **kwargs)
|
||||
except TypeError:
|
||||
return httpx.AsyncClient(mounts={
|
||||
"http://": httpx.AsyncHTTPTransport(proxy=proxies.get("http")) if proxies else None,
|
||||
"https://": httpx.AsyncHTTPTransport(proxy=proxies.get("https")) if proxies else None
|
||||
}, **kwargs)
|
||||
|
||||
class AsyncHttpx:
|
||||
proxy: ClassVar[dict[str, str | None]] = {
|
||||
@ -112,7 +128,7 @@ class AsyncHttpx:
|
||||
if not headers:
|
||||
headers = get_user_agent()
|
||||
_proxy = proxy or (cls.proxy if use_proxy else None)
|
||||
async with httpx.AsyncClient(proxies=_proxy, verify=verify) as client: # type: ignore
|
||||
async with get_async_client(proxies=_proxy, verify=verify) as client: # type: ignore
|
||||
return await client.get(
|
||||
url,
|
||||
params=params,
|
||||
@ -151,7 +167,7 @@ class AsyncHttpx:
|
||||
if not headers:
|
||||
headers = get_user_agent()
|
||||
_proxy = proxy or (cls.proxy if use_proxy else None)
|
||||
async with httpx.AsyncClient(proxies=_proxy, verify=verify) as client: # type: ignore
|
||||
async with get_async_client(proxies=_proxy, verify=verify) as client: # type: ignore
|
||||
return await client.head(
|
||||
url,
|
||||
params=params,
|
||||
@ -198,7 +214,7 @@ class AsyncHttpx:
|
||||
if not headers:
|
||||
headers = get_user_agent()
|
||||
_proxy = proxy or (cls.proxy if use_proxy else None)
|
||||
async with httpx.AsyncClient(proxies=_proxy, verify=verify) as client: # type: ignore
|
||||
async with get_async_client(proxies=_proxy, verify=verify) as client: # type: ignore
|
||||
return await client.post(
|
||||
url,
|
||||
content=content,
|
||||
@ -278,7 +294,7 @@ class AsyncHttpx:
|
||||
if not headers:
|
||||
headers = get_user_agent()
|
||||
_proxy = proxy or (cls.proxy if use_proxy else None)
|
||||
async with httpx.AsyncClient(
|
||||
async with get_async_client(
|
||||
proxies=_proxy, # type: ignore
|
||||
verify=verify,
|
||||
) as client:
|
||||
@ -529,4 +545,4 @@ class UrlPathNumberNotEqual(Exception):
|
||||
|
||||
|
||||
class BrowserIsNone(Exception):
|
||||
pass
|
||||
pass
|
||||
Loading…
Reference in New Issue
Block a user