From 0e950a63cb5a3d4819d008f6d7a8620ccd9b82d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <101191390+HIllya51@users.noreply.github.com> Date: Fri, 3 May 2024 16:55:43 +0800 Subject: [PATCH] fix Update requests.py Update requests.py --- .../LunaTranslator/myutils/githubupdate.py | 2 +- .../network/libcurl/requests.py | 35 +++++++++++++------ 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/LunaTranslator/LunaTranslator/myutils/githubupdate.py b/LunaTranslator/LunaTranslator/myutils/githubupdate.py index a8e0d019..5e59ff26 100644 --- a/LunaTranslator/LunaTranslator/myutils/githubupdate.py +++ b/LunaTranslator/LunaTranslator/myutils/githubupdate.py @@ -122,7 +122,7 @@ def updatemethod(_version, progresscallback): file_size += thislen progresscallback( - "总大小{} MB 进度 {}% ".format( + "总大小{} MB 进度 {:0.2f}% ".format( int(1000 * (int(size / 1024) / 1024)) / 1000, int(10000 * (file_size / size)) / 100, ), diff --git a/LunaTranslator/LunaTranslator/network/libcurl/requests.py b/LunaTranslator/LunaTranslator/network/libcurl/requests.py index 06f750f7..d7477167 100644 --- a/LunaTranslator/LunaTranslator/network/libcurl/requests.py +++ b/LunaTranslator/LunaTranslator/network/libcurl/requests.py @@ -131,6 +131,7 @@ class Session(Sessionbase): cookies.update(self.cookies) else: cookies = self.cookies + curl_easy_reset(curl) if cookies: cookie = self._parsecookie(cookies) curl_easy_setopt(curl, CURLoption.CURLOPT_COOKIE, cookie.encode("utf8")) @@ -201,31 +202,43 @@ class Session(Sessionbase): ) def ___perform(): + error = False try: self._perform(curl) except: print_exc() - self.raise_for_status() headerqueue.put(None) - curl_easy_reset(curl) + error = True resp.queue.put(None) + if error: + print(url) + self.raise_for_status() threading.Thread(target=___perform, daemon=True).start() - headerb = b"" + headerb = "" + cnt = 1 while True: _headerb = headerqueue.get() if _headerb is None: self.raise_for_status() - headerb += _headerb - if _headerb == b"\r\n": - break - resp.headers = self._update_header_cookie(headerb.decode("utf8")) + _headerb = _headerb.decode("utf8") + + if _headerb.endswith( + "200 Connection established\r\n" + ): # HTTP/1.1 200 Connection established\r\n + cnt += 1 + elif _headerb == "\r\n": + cnt -= 1 + if cnt == 0: + break + else: + headerb += _headerb + + resp.headers = self._update_header_cookie(headerb) if proxy: - resp.status_code = int( - headerb.decode("utf8").split("\r\n")[0].split(" ")[1] - ) + resp.status_code = int(headerb[:-2].split(" ")[1]) else: resp.status_code = self._getStatusCode(curl) else: @@ -259,7 +272,7 @@ class Session(Sessionbase): resp.content = b"".join(_content) resp.headers = self._update_header_cookie(b"".join(_headers).decode("utf8")) resp.status_code = self._getStatusCode(curl) - curl_easy_reset(curl) + resp.last_error = self.last_error resp.cookies = self.cookies return resp