use utils.deadline() in utils.http.request, not raw sigalrm

This commit is contained in:
jesopo 2019-09-02 15:50:21 +01:00
parent d42d694e64
commit b7b2f31c1c

View file

@ -73,24 +73,21 @@ def request(url: str, method: str="GET", get_params: dict={},
if not "User-Agent" in headers: if not "User-Agent" in headers:
headers["User-Agent"] = USER_AGENT headers["User-Agent"] = USER_AGENT
signal.signal(signal.SIGALRM, lambda _1, _2: throw_timeout()) with utils.deadline(seconds=5):
signal.alarm(5) try:
try: response = requests.request(
response = requests.request( method.upper(),
method.upper(), url,
url, headers=headers,
headers=headers, params=get_params,
params=get_params, data=post_data,
data=post_data, json=json_data,
json=json_data, allow_redirects=allow_redirects,
allow_redirects=allow_redirects, stream=True
stream=True )
) response_content = response.raw.read(RESPONSE_MAX, decode_content=True)
response_content = response.raw.read(RESPONSE_MAX, decode_content=True) except DeadlineExceededException:
except TimeoutError: raise HTTPTimeoutException()
raise HTTPTimeoutException()
finally:
signal.signal(signal.SIGALRM, signal.SIG_IGN)
response_headers = utils.CaseInsensitiveDict(dict(response.headers)) response_headers = utils.CaseInsensitiveDict(dict(response.headers))
content_type = response.headers.get("Content-Type", "").split(";", 1)[0] content_type = response.headers.get("Content-Type", "").split(";", 1)[0]