This commit is contained in:
恍兮惚兮 2024-09-04 22:33:36 +08:00
parent ccbbd2a02b
commit a151564faa
3 changed files with 28 additions and 29 deletions

View File

@ -97,22 +97,28 @@ class Requester(Requester_common):
que.append(bs)
return realsize
def _filter_header(self, headertext):
header = []
for line in headertext.split("\n"):
if line.startswith("HTTP/"):
header = []
header.append(line)
return "\n".join(header)
def __getrealheader(self, headerqueue):
if isinstance(headerqueue, queue.Queue):
header = ""
header = []
while True:
_headerb = headerqueue.get()
if _headerb == 0:
break
elif _headerb == 1:
raise CURLException()
_headerb = _headerb.decode("utf8")
if _headerb.startswith("HTTP/"):
header = ""
header += _headerb
return header
elif isinstance(_headerb, Exception):
raise _headerb
header.append(_headerb)
elif isinstance(headerqueue, list):
return b"".join(headerqueue).decode("utf8")
header = headerqueue
return self._filter_header(b"".join(header).decode("utf8"))
def _setheaders(self, curl, headers, cookies):
lheaders = Autoslist()
@ -208,17 +214,11 @@ class Requester(Requester_common):
if stream:
def ___perform():
error = False
try:
self._perform(curl)
except:
print_exc()
headerqueue.put(1)
error = True
except Exception as e:
headerqueue.put(e)
resp.queue.put(None)
if error:
print(url)
raise CURLException()
threading.Thread(target=___perform, daemon=True).start()
@ -229,7 +229,7 @@ class Requester(Requester_common):
if not stream:
resp.content = b"".join(resp.queue)
resp.headers, resp.cookies = self._parseheader2dict(header)
resp.headers, resp.cookies, resp.status_text = self._parseheader2dict(header)
resp.status_code = self._getStatusCode(curl)
resp.url = url

View File

@ -177,7 +177,9 @@ class Requester(Requester_common):
if succ == 0:
MaybeRaiseException()
resp = Response(stream)
resp.headers, resp.cookies = self._parseheader2dict(self._getheaders(hRequest))
resp.headers, resp.cookies, resp.status_text = self._parseheader2dict(
self._getheaders(hRequest)
)
resp.status_code = self._getStatusCode(hRequest)
resp.url = url

View File

@ -72,6 +72,7 @@ class ResponseBase:
self.url = ""
self.cookies = {}
self.status_code = 0
self.status_text = ""
self.__content = b""
self.__content_s = []
self.content_prepared = threading.Event()
@ -156,17 +157,12 @@ class ResponseBase:
yield pending
def raise_for_status(self):
reason = ""
http_error_msg = ""
if 400 <= self.status_code < 500:
http_error_msg = (
f"{self.status_code} Client Error: {reason} for url: {self.url}"
)
http_error_msg = f"{self.status_code} Client Error: {self.status_text} for url: {self.url}"
elif 500 <= self.status_code < 600:
http_error_msg = (
f"{self.status_code} Server Error: {reason} for url: {self.url}"
)
http_error_msg = f"{self.status_code} Server Error: {self.status_text} for url: {self.url}"
if http_error_msg:
raise HTTPError(http_error_msg)
@ -324,10 +320,11 @@ class Requester_common:
return cookie
def _parseheader2dict(self, headerstr: str):
# print(headerstr)
header = CaseInsensitiveDict()
cookie = {}
for line in headerstr.split("\r\n")[1:]:
lines = headerstr.split("\r\n")
status_text = " ".join(lines[0].split(" ")[2:])
for line in lines[1:]:
idx = line.find(": ")
if idx == -1:
continue
@ -335,7 +332,7 @@ class Requester_common:
cookie.update(self._parsecookiestring(line[idx + 2 :]))
else:
header[line[:idx]] = line[idx + 2 :]
return CaseInsensitiveDict(header), cookie
return CaseInsensitiveDict(header), cookie, status_text
def _parsejson(self, _json):
databytes = json.dumps(_json).encode("utf8")