This commit is contained in:
HIllya51 2024-01-11 00:45:06 +08:00
parent e9b7655e4d
commit d5b3f73cef
4 changed files with 23 additions and 12 deletions

View File

@ -262,6 +262,8 @@ curl_ws_send.restype=CURLcode
curl_easy_duphandle=libcurl.curl_easy_duphandle
curl_easy_duphandle.argtypes=CURL,
curl_easy_duphandle.restype=CURL
curl_easy_reset=libcurl.curl_easy_reset
curl_easy_reset.argtypes=CURL,
CURLWS_TEXT=1<<0
CURLWS_BINARY=1<<1
CURLWS_CLOSE=1<<3

View File

@ -10,6 +10,15 @@ class autostatus:
def __del__(self):
self.ref._status=0
class Response(ResponseBase):
def __init__(self):
super().__init__()
self.last_error=0
def iter_content(self,chunk_size=1024):
yield self.content
def raise_for_status(self):
if self.last_error:
raise CURLException(self.last_error)
class Session(Sessionbase):
def __init__(self) -> None:
@ -95,9 +104,10 @@ class Session(Sessionbase):
resp=Response()
resp.content=self._getmembyte(_content)
resp.status_code=self._getStatusCode(curl)
resp.last_error=self.last_error
resp.headers=self._update_header_cookie(self._getmembyte(_headers).decode('utf8'))
resp.cookies=self.cookies
curl_easy_reset(curl)
return resp
Sessionimpl[0]=Session

View File

@ -46,7 +46,7 @@ class CaseInsensitiveDict(MutableMapping):
def __repr__(self):
return str(dict(self.items()))
class Response:
class ResponseBase:
def __init__(self):
self.headers=CaseInsensitiveDict()
self.cookies={}
@ -67,8 +67,6 @@ class Response:
return charset
def json(self):
return json.loads(self.text)
def iter_content(self,chunk_size=1024):
yield self.content
class Sessionbase:
def __init__(self) -> None:
self.UA='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'

View File

@ -7,7 +7,7 @@ try:
from brotli_dec import decompress
except:
pass
class winhttp_resp(Response):
class Response(ResponseBase):
def iter_content(self,chunk_size=1024):
downloadedSize=DWORD()
buff=create_string_buffer(chunk_size)
@ -19,17 +19,16 @@ class winhttp_resp(Response):
del self.hconn
break
yield buff[:downloadedSize.value]
def raise_for_status(self):
error=GetLastError()
if error:
raise WinhttpException(error)
class Session(Sessionbase):
def __init__(self) -> None:
super().__init__()
self.hSession=AutoWinHttpHandle(WinHttpOpen(self.UA,WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,WINHTTP_NO_PROXY_NAME,WINHTTP_NO_PROXY_BYPASS,0))
if self.hSession==0:
raise WinhttpException(GetLastError())
def raise_for_status(self):
error=GetLastError()
if error:
raise WinhttpException(error)
def _getheaders(self,hreq):
@ -49,7 +48,9 @@ class Session(Sessionbase):
pointer(dwSize),
None )
if bResults==0:
self.raise_for_status()
error=GetLastError()
if error:
raise WinhttpException(error)
return dwStatusCode.value
def _set_proxy(self,hsess,proxy):
@ -81,7 +82,7 @@ class Session(Sessionbase):
raise WinhttpException(GetLastError())
headers=self._update_header_cookie(self._getheaders(hRequest))
resp=winhttp_resp()
resp=Response()
resp.status_code=self._getStatusCode(hRequest)
resp.headers=headers
resp.cookies=self.cookies