Download utils/Client.py from Rfym21/Chat2API: direct link, hf CLI and curl.
- Browser
- Download file 1.91 kB
-
https://huggingface.co/spaces/Rfym21/Chat2API/resolve/bcb45841cfafb83ca241176763dab7b9f2a33e9b/utils/Client.py
- Command line
-
hf download hf://spaces/Rfym21/Chat2API@bcb45841cfafb83ca241176763dab7b9f2a33e9b/utils/Client.py
-
curl -L -o Client.py https://huggingface.co/spaces/Rfym21/Chat2API/resolve/bcb45841cfafb83ca241176763dab7b9f2a33e9b/utils/Client.py
1.91 kB
| import random | |
| from curl_cffi.requests import AsyncSession | |
| class Client: | |
| def __init__(self, proxy=None, timeout=15, verify=True, impersonate='safari15_3'): | |
| self.proxies = {"http": proxy, "https": proxy} | |
| self.timeout = timeout | |
| self.verify = verify | |
| self.impersonate = impersonate | |
| # impersonate=self.impersonate | |
| # self.ja3 = "" | |
| # self.akamai = "" | |
| # ja3=self.ja3, akamai=self.akamai | |
| self.session = AsyncSession(proxies=self.proxies, timeout=self.timeout, impersonate=self.impersonate, verify=self.verify) | |
| self.session2 = AsyncSession(proxies=self.proxies, timeout=self.timeout, impersonate=self.impersonate, verify=self.verify) | |
| async def post(self, *args, **kwargs): | |
| r = await self.session.post(*args, **kwargs) | |
| return r | |
| async def post_stream(self, *args, headers=None, cookies=None, **kwargs): | |
| if self.session: | |
| headers = headers or self.session.headers | |
| cookies = cookies or self.session.cookies | |
| r = await self.session2.post(*args, headers=headers, cookies=cookies, **kwargs) | |
| return r | |
| async def get(self, *args, **kwargs): | |
| r = await self.session.get(*args, **kwargs) | |
| return r | |
| async def request(self, *args, **kwargs): | |
| r = await self.session.request(*args, **kwargs) | |
| return r | |
| async def put(self, *args, **kwargs): | |
| r = await self.session.put(*args, **kwargs) | |
| return r | |
| async def close(self): | |
| if self.session: | |
| try: | |
| await self.session.close() | |
| del self.session | |
| except Exception: | |
| pass | |
| if self.session2: | |
| try: | |
| await self.session2.close() | |
| del self.session2 | |
| except Exception: | |
| pass | |