This commit is contained in:
HIllya51 2024-01-10 11:23:34 +08:00
parent 61382d5937
commit d7ebef036a
3 changed files with 21 additions and 14 deletions

View File

@ -49,15 +49,22 @@ class Session(Sessionbase):
def _getmembyte(self,mem):
return cast(mem.memory,POINTER(c_char))[:mem.size]
def request_impl(self,
method,scheme,server,port,param,url,headers,dataptr,datalen,proxy,stream,verify ):
method,scheme,server,port,param,url,headers,cookies,dataptr,datalen,proxy,stream,verify):
headers=self._parseheader(headers,None)#curl对于headers中有cookie且session中也有cookie的情况下不会自动合并而是会有两个cookie键所以不能在header里放cookie
if self._status==0:
curl=self.curl
__=autostatus(self)
else:
#不能多线程同时复用同一个curl对象
curl=AutoCURLHandle(curl_easy_duphandle(self.curl))
if cookies:
cookies.update(self.cookies)
else:
cookies=self.cookies
if cookies:
cookie=self._parsecookie(cookies)
curl_easy_setopt(curl, CURLoption.CURLOPT_COOKIE, cookie.encode('utf8'));
curl_easy_setopt(curl,CURLoption.CURLOPT_CUSTOMREQUEST,method.upper().encode('utf8'))
self.last_error=curl_easy_setopt(curl,CURLoption.CURLOPT_URL,url.encode('utf8'))

View File

@ -140,16 +140,16 @@ class Sessionbase:
_x=[]
if cookies:
self.cookies.update(cookies)
_c=[]
for k ,v in self.cookies.items():
_c.append('{}={}'.format(k,v))
cookie='; '.join(_c)
headers.update({'Cookie':cookie})
cookie=self._parsecookie(cookies)
headers.update({'Cookie':cookie})
for k in sorted(headers.keys()):
_x.append('{}: {}'.format(k,headers[k]))
return _x
def _parsecookie(self,cookie):
_c=[]
for k ,v in cookie.items():
_c.append('{}={}'.format(k,v))
return '; '.join(_c)
def _update_header_cookie(self,headerstr):
self.headers,cookies=self._parseheader2dict(headerstr)
self.cookies.update(cookies)
@ -162,7 +162,7 @@ class Sessionbase:
if line[:idx].lower()=='set-cookie':
_c=line[idx+2:].split('; ')[0]
_idx=_c.find('=')
cookie[_c[:_idx]]=_c[idx+1:]
cookie[_c[:_idx]]=_c[_idx+1:]
else:
header[line[:idx]]=line[idx+2:]
return CaseInsensitiveDict(header),cookie
@ -199,9 +199,9 @@ class Sessionbase:
scheme,server,port,param,url=self._parseurl(url,params)
headers,dataptr,datalen=self._parsedata(data,headers,json)
headers=self._parseheader(headers,cookies)
proxy= proxies.get(scheme,None) if proxies else None
_= self.request_impl(method,scheme,server,port,param,url,headers,dataptr,datalen,proxy,stream,verify)
_= self.request_impl(method,scheme,server,port,param,url,headers,cookies,dataptr,datalen,proxy,stream,verify)
return _
Sessionimpl=[Sessionbase]

View File

@ -39,8 +39,8 @@ class Session(Sessionbase):
winhttpsetproxy(hsess,proxy)
def request_impl(self,
method,scheme,server,port,param,url,headers,dataptr,datalen,proxy,stream,verify):
method,scheme,server,port,param,url,headers,cookies,dataptr,datalen,proxy,stream,verify):
headers=self._parseheader(headers,cookies)
flag=WINHTTP_FLAG_SECURE if scheme=='https' else 0
#print(server,port,param,dataptr)
headers='\r\n'.join(headers)