This commit is contained in:
HIllya51 2024-01-10 00:19:26 +08:00
parent c00734f355
commit 61382d5937
7 changed files with 55 additions and 44 deletions

View File

@ -258,6 +258,9 @@ curl_ws_recv.restype=CURLcode
curl_ws_send=libcurl.curl_ws_send
curl_ws_send.argtypes=CURL,c_void_p,c_size_t,POINTER(c_size_t),c_int64,c_uint
curl_ws_send.restype=CURLcode
curl_easy_duphandle=libcurl.curl_easy_duphandle
curl_easy_duphandle.argtypes=CURL,
curl_easy_duphandle.restype=CURL
CURLWS_TEXT=1<<0
CURLWS_BINARY=1<<1
CURLWS_CLOSE=1<<3

View File

@ -3,10 +3,21 @@ from libcurl import *
import winsharedutils
from network.requests_common import *
class autostatus:
def __init__(self,ref) -> None:
self.ref=ref
ref._status=1
def __del__(self):
self.ref._status=0
class Session(Sessionbase):
def __init__(self) -> None:
super().__init__()
self._status=0
self.curl=AutoCURLHandle(curl_easy_init())
curl_easy_setopt(self.curl,CURLoption.CURLOPT_COOKIEJAR,'')
curl_easy_setopt(self.curl,CURLoption.CURLOPT_USERAGENT,self.UA.encode('utf8'))
def raise_for_status(self):
if self.last_error:
raise CURLException(self.last_error)
@ -39,44 +50,45 @@ class Session(Sessionbase):
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 ):
if self._status==0:
curl=self.curl
__=autostatus(self)
else:
#不能多线程同时复用同一个curl对象
curl=AutoCURLHandle(curl_easy_duphandle(self.curl))
curl_easy_setopt(curl,CURLoption.CURLOPT_CUSTOMREQUEST,method.upper().encode('utf8'))
if self.curl==0 or proxy!=self.proxy:
self.curl=AutoCURLHandle(curl_easy_init())
self.proxy=proxy
curl_easy_setopt(self.curl,CURLoption.CURLOPT_COOKIEJAR,'')
curl_easy_setopt(self.curl,CURLoption.CURLOPT_USERAGENT,self.UA.encode('utf8'))
curl_easy_setopt(self.curl,CURLoption.CURLOPT_CUSTOMREQUEST,method.upper().encode('utf8'))
self.last_error=curl_easy_setopt(self.curl,CURLoption.CURLOPT_URL,url.encode('utf8'))
self.last_error=curl_easy_setopt(curl,CURLoption.CURLOPT_URL,url.encode('utf8'))
self.raise_for_status()
curl_easy_setopt(self.curl, CURLoption.CURLOPT_PORT, port )
curl_easy_setopt(curl, CURLoption.CURLOPT_PORT, port )
lheaders=Autoslist()
for _ in headers:
lheaders = curl_slist_append(cast(lheaders,POINTER(curl_slist)), _.encode('utf8'));
self.last_error=curl_easy_setopt(self.curl, CURLoption.CURLOPT_HTTPHEADER, lheaders);
self.last_error=curl_easy_setopt(curl, CURLoption.CURLOPT_HTTPHEADER, lheaders);
self.raise_for_status()
self._set_verify(self.curl,verify)
self._set_proxy(self.curl,proxy)
self._set_verify(curl,verify)
self._set_proxy(curl,proxy)
if datalen:
curl_easy_setopt(self.curl,CURLoption.CURLOPT_POSTFIELDS,dataptr)
curl_easy_setopt(self.curl,CURLoption.CURLOPT_POSTFIELDSIZE,datalen)
curl_easy_setopt(curl,CURLoption.CURLOPT_POSTFIELDS,dataptr)
curl_easy_setopt(curl,CURLoption.CURLOPT_POSTFIELDSIZE,datalen)
_content=winsharedutils.MemoryStruct()
curl_easy_setopt(self.curl,CURLoption.CURLOPT_WRITEDATA,pointer(_content))
curl_easy_setopt(self.curl,CURLoption.CURLOPT_WRITEFUNCTION,winsharedutils.WriteMemoryCallback)
curl_easy_setopt(curl,CURLoption.CURLOPT_WRITEDATA,pointer(_content))
curl_easy_setopt(curl,CURLoption.CURLOPT_WRITEFUNCTION,winsharedutils.WriteMemoryCallback)
_headers=winsharedutils.MemoryStruct()
curl_easy_setopt(self.curl,CURLoption.CURLOPT_HEADERDATA,pointer(_headers))
curl_easy_setopt(self.curl,CURLoption.CURLOPT_HEADERFUNCTION,winsharedutils.WriteMemoryCallback)
curl_easy_setopt(curl,CURLoption.CURLOPT_HEADERDATA,pointer(_headers))
curl_easy_setopt(curl,CURLoption.CURLOPT_HEADERFUNCTION,winsharedutils.WriteMemoryCallback)
self._perform(self.curl)
self._perform(curl)
self.content=self._getmembyte(_content)
self._update_header_cookie(self._getmembyte(_headers).decode('utf8'))
self.status_code=self._getStatusCode(self.curl)
self.status_code=self._getStatusCode(curl)
return self
def iter_content(self,chunk_size=1024):
yield self.content

View File

@ -50,13 +50,10 @@ class CaseInsensitiveDict(MutableMapping):
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'
self.hSession=0
self.curl=0
self.last_error=0
self.status_code=0
self.content=b'{}'
self.cookies={}
self.proxy=None
self.dfheaders=CaseInsensitiveDict({
"User-Agent": self.UA,
"Accept-Encoding": 'gzip, deflate',#br
@ -145,11 +142,11 @@ class Sessionbase:
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})
_c=[]
for k ,v in self.cookies.items():
_c.append('{}={}'.format(k,v))
cookie='; '.join(_c)
headers.update({'Cookie':cookie})
for k in sorted(headers.keys()):
_x.append('{}: {}'.format(k,headers[k]))
return _x

View File

@ -2,7 +2,11 @@
from winhttp import *
from network.requests_common import *
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()
@ -40,13 +44,7 @@ class Session(Sessionbase):
flag=WINHTTP_FLAG_SECURE if scheme=='https' else 0
#print(server,port,param,dataptr)
headers='\r\n'.join(headers)
if self.hSession==0 or proxy!=self.proxy:
self.proxy=proxy
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())
hConnect=AutoWinHttpHandle(WinHttpConnect(self.hSession,server,port,0))
if hConnect==0:
raise WinhttpException(GetLastError())

View File

@ -107,7 +107,7 @@ class BaiduV1(Tse):
not_update_cond_freq = 1 if self.query_count < update_session_after_freq else 0
not_update_cond_time = 1 if time.time() - self.begin_time < update_session_after_seconds else 0
if not (self.session and self.language_map and not_update_cond_freq and not_update_cond_time):
if not (self.session):
self.session = requests.Session()
_ = self.session.get(self.host_url, headers=self.host_headers, timeout=timeout, proxies=proxies) # must twice, send cookies.
host_html = self.session.get(self.host_url, headers=self.host_headers, timeout=timeout, proxies=proxies).text

View File

@ -35,8 +35,8 @@ def get_import_table(file_path):
return import_dlls
shutil.rmtree(targetdir)
if os.path.exists(targetdir):
shutil.rmtree(targetdir)
shutil.copytree(nuitkadist,targetdir_in)
shutil.copytree(launch,targetdir,dirs_exist_ok=True)
shutil.copytree(r'.\files',rf'{targetdir}\files')
@ -79,5 +79,6 @@ for f in set(dlls):
continue
elif os.path.exists(rf'{downlevel}\{f}'):
shutil.copy(rf'{downlevel}\{f}',targetdir_in)
os.remove(rf'{targetdir}\..\{target}')
if os.path.exists(rf'{targetdir}\..\{target}'):
os.remove(rf'{targetdir}\..\{target}')
os.system(rf'"C:\Program Files\7-Zip\7z.exe" a -m0=LZMA -mx9 {targetdir}\..\{target} {targetdir}')

View File

@ -4,7 +4,7 @@
<a href="./LICENSE"><img src="https://img.shields.io/github/license/HIllya51/LunaTranslator"></a>
<a href="https://github.com/HIllya51/LunaTranslator/releases"><img src="https://img.shields.io/github/v/release/HIllya51/LunaTranslator?color=ffa"></a>
<a href="https://github.com/HIllya51/LunaTranslator/stargazers"><img src="https://img.shields.io/github/stars/HIllya51/LunaTranslator?color=ccf"></a>
<a href="https://hillya51.github.io/download.html" target="_blank"><img src="https://img.shields.io/github/downloads/HIllya51/LunaTranslator/total.svg?logo=github"/></a>
<a href="https://hillya51.github.io/download.html" target="_blank"><img src="https://img.shields.io/badge/download-blue"/></a>
</p>
## 简体中文 | [Русский язык](README_ru.md) | [English](README_en.md) | [Other Language Support](otherlang.md)