From 61382d593799c97b290a17b1203efec5ffbb21fe Mon Sep 17 00:00:00 2001 From: HIllya51 <1871535768@qq.com> Date: Wed, 10 Jan 2024 00:19:26 +0800 Subject: [PATCH] fix --- .../LunaTranslator/network/libcurl/libcurl.py | 3 + .../network/libcurl/requests.py | 58 +++++++++++-------- .../LunaTranslator/network/requests_common.py | 13 ++--- .../network/winhttp/requests.py | 14 ++--- .../LunaTranslator/translator/baidu.py | 2 +- LunaTranslator/pack.py | 7 ++- README.md | 2 +- 7 files changed, 55 insertions(+), 44 deletions(-) diff --git a/LunaTranslator/LunaTranslator/network/libcurl/libcurl.py b/LunaTranslator/LunaTranslator/network/libcurl/libcurl.py index 615b2740..6a4041e6 100644 --- a/LunaTranslator/LunaTranslator/network/libcurl/libcurl.py +++ b/LunaTranslator/LunaTranslator/network/libcurl/libcurl.py @@ -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 diff --git a/LunaTranslator/LunaTranslator/network/libcurl/requests.py b/LunaTranslator/LunaTranslator/network/libcurl/requests.py index 0d96b4d8..551504cb 100644 --- a/LunaTranslator/LunaTranslator/network/libcurl/requests.py +++ b/LunaTranslator/LunaTranslator/network/libcurl/requests.py @@ -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 diff --git a/LunaTranslator/LunaTranslator/network/requests_common.py b/LunaTranslator/LunaTranslator/network/requests_common.py index 06ea9ab3..0a653b51 100644 --- a/LunaTranslator/LunaTranslator/network/requests_common.py +++ b/LunaTranslator/LunaTranslator/network/requests_common.py @@ -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 diff --git a/LunaTranslator/LunaTranslator/network/winhttp/requests.py b/LunaTranslator/LunaTranslator/network/winhttp/requests.py index 5287c749..461bc189 100644 --- a/LunaTranslator/LunaTranslator/network/winhttp/requests.py +++ b/LunaTranslator/LunaTranslator/network/winhttp/requests.py @@ -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()) diff --git a/LunaTranslator/LunaTranslator/translator/baidu.py b/LunaTranslator/LunaTranslator/translator/baidu.py index 696a09d4..df4b7945 100644 --- a/LunaTranslator/LunaTranslator/translator/baidu.py +++ b/LunaTranslator/LunaTranslator/translator/baidu.py @@ -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 diff --git a/LunaTranslator/pack.py b/LunaTranslator/pack.py index 0617ef56..61928957 100644 --- a/LunaTranslator/pack.py +++ b/LunaTranslator/pack.py @@ -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}') \ No newline at end of file diff --git a/README.md b/README.md index 3d4cd5c4..71f59ce0 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ - +

## 简体中文 | [Русский язык](README_ru.md) | [English](README_en.md) | [Other Language Support](otherlang.md)