This commit is contained in:
恍兮惚兮 2024-01-27 00:22:03 +08:00
parent 52515993aa
commit dd2b71f82d
3 changed files with 26 additions and 21 deletions

View File

@ -282,8 +282,8 @@ class AutoCURLHandle(CURL):
class CURLException(Exception):
def __init__(self,code) -> None:
self.errorcode=code.value
if isinstance(code,CURLcode):
self.errorcode=code.value
error=curl_easy_strerror(code).decode('utf8')
for _ in dir(CURLcode):
if _.startswith('CURLE_') and code.value==getattr(CURLcode,_):

View File

@ -19,6 +19,17 @@ class Response(ResponseBase):
def raise_for_status(self):
if self.last_error:
raise CURLException(self.last_error)
def ExceptionFilter(func):
def _wrapper(*args,**kwargs):
try:
_= func(*args,**kwargs)
return _
except CURLException as e:
if e.errorcode==CURLcode.CURLE_OPERATION_TIMEDOUT:
raise Timeout(e)
else:
raise e
return _wrapper
class Session(Sessionbase):
def __init__(self) -> None:
@ -57,18 +68,9 @@ class Session(Sessionbase):
def _getmembyte(self,mem):
return cast(mem.memory,POINTER(c_char))[:mem.size]
@ExceptionFilter
def request_impl(self,
method,scheme,server,port,param,url,headers,cookies,dataptr,datalen,proxy,stream,verify,timeout):
try:
_= self.request_impl_1(method,scheme,server,port,param,url,headers,cookies,dataptr,datalen,proxy,stream,verify,timeout)
return _
except CURLException as e:
if e.errorcode==CURLcode.CURLE_OPERATION_TIMEDOUT:
raise Timeout(e)
else:
raise e
def request_impl_1(self,
method,scheme,server,port,param,url,headers,cookies,dataptr,datalen,proxy,stream,verify,timeout):
if self._status==0:
curl=self.curl

View File

@ -23,6 +23,18 @@ class Response(ResponseBase):
error=GetLastError()
if error:
raise WinhttpException(error)
def ExceptionFilter(func):
def _wrapper(*args,**kwargs):
try:
_= func(*args,**kwargs)
return _
except WinhttpException as e:
if e.errorcode==WinhttpException.ERROR_WINHTTP_TIMEOUT:
raise Timeout(e)
else:
raise e
return _wrapper
class Session(Sessionbase):
def __init__(self) -> None:
super().__init__()
@ -60,17 +72,8 @@ class Session(Sessionbase):
if verify==False:
dwFlags=DWORD(SECURITY_FLAG_IGNORE_ALL_CERT_ERRORS)
WinHttpSetOption(curl,WINHTTP_OPTION_SECURITY_FLAGS, pointer(dwFlags),sizeof(dwFlags))
@ExceptionFilter
def request_impl(self,
method,scheme,server,port,param,url,headers,cookies,dataptr,datalen,proxy,stream,verify,timeout):
try:
_= self.request_impl_1(method,scheme,server,port,param,url,headers,cookies,dataptr,datalen,proxy,stream,verify,timeout)
return _
except WinhttpException as e:
if e.errorcode==WinhttpException.ERROR_WINHTTP_TIMEOUT:
raise Timeout(e)
else:
raise e
def request_impl_1(self,
method,scheme,server,port,param,url,headers,cookies,dataptr,datalen,proxy,stream,verify,timeout):
headers=self._parseheader(headers,cookies)
flag=WINHTTP_FLAG_SECURE if scheme=='https' else 0