.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
raise_for_httperror.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Raise exception for an HTTP response is an error.
3
4"""
5
6from searx.exceptions import (
7 SearxEngineCaptchaException,
8 SearxEngineTooManyRequestsException,
9 SearxEngineAccessDeniedException,
10)
11from searx import get_setting
12
13
15 if resp.status_code in [429, 503]:
16 if ('__cf_chl_jschl_tk__=' in resp.text) or (
17 '/cdn-cgi/challenge-platform/' in resp.text
18 and 'orchestrate/jsch/v1' in resp.text
19 and 'window._cf_chl_enter(' in resp.text
20 ):
21 return True
22 if resp.status_code == 403 and '__cf_chl_captcha_tk__=' in resp.text:
23 return True
24 return False
25
26
28 return resp.status_code == 403 and '<span class="cf-error-code">1020</span>' in resp.text
29
30
32 if resp.headers.get('Server', '').startswith('cloudflare'):
34 # https://support.cloudflare.com/hc/en-us/articles/200170136-Understanding-Cloudflare-Challenge-Passage-Captcha-
35 # suspend for 2 weeks
37 message='Cloudflare CAPTCHA', suspended_time=get_setting('search.suspended_times.cf_SearxEngineCaptcha')
38 )
39
42 message='Cloudflare Firewall',
43 suspended_time=get_setting('search.suspended_times.cf_SearxEngineAccessDenied'),
44 )
45
46
48 if resp.status_code == 503 and '"https://www.google.com/recaptcha/' in resp.text:
50 message='ReCAPTCHA', suspended_time=get_setting('search.suspended_times.recaptcha_SearxEngineCaptcha')
51 )
52
53
59def raise_for_httperror(resp):
60 """Raise exception for an HTTP response is an error.
61
62 Args:
63 resp (requests.Response): Response to check
64
65 Raises:
66 requests.HTTPError: raise by resp.raise_for_status()
67 searx.exceptions.SearxEngineAccessDeniedException: raise when the HTTP status code is 402 or 403.
68 searx.exceptions.SearxEngineTooManyRequestsException: raise when the HTTP status code is 429.
69 searx.exceptions.SearxEngineCaptchaException: raise when if CATPCHA challenge is detected.
70 """
71 if resp.status_code and resp.status_code >= 400:
73 if resp.status_code in (402, 403):
74 raise SearxEngineAccessDeniedException(message='HTTP error ' + str(resp.status_code))
75 if resp.status_code == 429:
77 resp.raise_for_status()