diff options
| author | 2019-09-02 15:50:21 +0100 | |
|---|---|---|
| committer | 2019-09-02 15:50:21 +0100 | |
| commit | b7b2f31c1cf6c1ff7fb3a088515b5a8e08a02103 (patch) | |
| tree | ccf3fb39df38feea59c1eb81a9642ae28cb3d9de /src/utils | |
| parent | move deadline alarm time check inside try/finally (diff) | |
| signature | ||
use utils.deadline() in utils.http.request, not raw sigalrm
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/http.py | 33 |
1 files changed, 15 insertions, 18 deletions
diff --git a/src/utils/http.py b/src/utils/http.py index d63a99cc..f60cbbc6 100644 --- a/src/utils/http.py +++ b/src/utils/http.py @@ -73,24 +73,21 @@ def request(url: str, method: str="GET", get_params: dict={}, if not "User-Agent" in headers: headers["User-Agent"] = USER_AGENT - signal.signal(signal.SIGALRM, lambda _1, _2: throw_timeout()) - signal.alarm(5) - try: - response = requests.request( - method.upper(), - url, - headers=headers, - params=get_params, - data=post_data, - json=json_data, - allow_redirects=allow_redirects, - stream=True - ) - response_content = response.raw.read(RESPONSE_MAX, decode_content=True) - except TimeoutError: - raise HTTPTimeoutException() - finally: - signal.signal(signal.SIGALRM, signal.SIG_IGN) + with utils.deadline(seconds=5): + try: + response = requests.request( + method.upper(), + url, + headers=headers, + params=get_params, + data=post_data, + json=json_data, + allow_redirects=allow_redirects, + stream=True + ) + response_content = response.raw.read(RESPONSE_MAX, decode_content=True) + except DeadlineExceededException: + raise HTTPTimeoutException() response_headers = utils.CaseInsensitiveDict(dict(response.headers)) content_type = response.headers.get("Content-Type", "").split(";", 1)[0] |
