.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.network.network.Network Class Reference

Public Member Functions

 __init__ (self, bool enable_http=True, bool verify=True, bool enable_http2=False, int max_connections=None, int max_keepalive_connections=None, float keepalive_expiry=None, str|dict[str, str]|None proxies=None, bool using_tor_proxy=False, str|list[str]|None local_addresses=None, int retries=0, bool retry_on_http_error=False, int max_redirects=30, str logger_name=None)
 check_parameters (self)
Generator[str] iter_ipaddresses (self)
 get_ipaddress_cycle (self)
Generator[tuple[str, list[str]]] iter_proxies (self)
Generator[tuple[tuple[str, str],...], str, str] get_proxy_cycles (self)
 log_response (self, httpx.Response response)
httpx.AsyncClient get_client (self, bool|None verify=None, int|None max_redirects=None)
 aclose (self)
SXNG_Response patch_response (self, httpx.Response response, bool do_raise_for_httperror)
 is_valid_response (self, httpx.Response response)
SXNG_Response call_client (self, bool stream, str method, str url, **t.Any kwargs)
SXNG_Response request (self, str method, str url, **t.Any kwargs)
 stream (self, str method, str url, **kwargs)
 aclose_all (cls)

Static Public Member Functions

bool check_tor_proxy (httpx.AsyncClient client, proxies)
dict[str, t.Any] extract_kwargs_clients (dict[str, t.Any] kwargs)
 extract_do_raise_for_httperror (dict[str, t.Any] kwargs)

Public Attributes

 enable_http = enable_http
 verify = verify
 enable_http2 = enable_http2
 max_connections = max_connections
 max_keepalive_connections = max_keepalive_connections
 keepalive_expiry = keepalive_expiry
 proxies = proxies
 using_tor_proxy = using_tor_proxy
 local_addresses = local_addresses
 retries = retries
 retry_on_http_error = retry_on_http_error
 max_redirects = max_redirects

Protected Attributes

 _local_addresses_cycle = self.get_ipaddress_cycle()
Generator[tuple[tuple[str, str],...], str, str] _proxies_cycle = self.get_proxy_cycles()
dict _clients = {}
 _logger = logger.getChild(logger_name) if logger_name else logger

Static Protected Attributes

dict _TOR_CHECK_RESULT = {}

Static Private Attributes

tuple __slots__

Detailed Description

Definition at line 45 of file network.py.

Constructor & Destructor Documentation

◆ __init__()

searx.network.network.Network.__init__ ( self,
bool enable_http = True,
bool verify = True,
bool enable_http2 = False,
int max_connections = None,
int max_keepalive_connections = None,
float keepalive_expiry = None,
str | dict[str, str] | None proxies = None,
bool using_tor_proxy = False,
str | list[str] | None local_addresses = None,
int retries = 0,
bool retry_on_http_error = False,
int max_redirects = 30,
str logger_name = None )

Definition at line 68 of file network.py.

84 ):
85
86 self.enable_http = enable_http
87 self.verify = verify
88 self.enable_http2 = enable_http2
89 self.max_connections = max_connections
90 self.max_keepalive_connections = max_keepalive_connections
91 self.keepalive_expiry = keepalive_expiry
92 self.proxies = proxies
93 self.using_tor_proxy = using_tor_proxy
94 self.local_addresses = local_addresses
95 self.retries = retries
96 self.retry_on_http_error = retry_on_http_error
97 self.max_redirects = max_redirects
98 self._local_addresses_cycle = self.get_ipaddress_cycle()
99 self._proxies_cycle = self.get_proxy_cycles()
100 self._clients = {}
101 self._logger = logger.getChild(logger_name) if logger_name else logger
102 self.check_parameters()
103

Member Function Documentation

◆ aclose()

searx.network.network.Network.aclose ( self)

Definition at line 217 of file network.py.

217 async def aclose(self):
218 async def close_client(client):
219 try:
220 await client.aclose()
221 except httpx.HTTPError:
222 pass
223
224 await asyncio.gather(*[close_client(client) for client in self._clients.values()], return_exceptions=False)
225

References _clients.

◆ aclose_all()

searx.network.network.Network.aclose_all ( cls)

Definition at line 310 of file network.py.

310 async def aclose_all(cls):
311 await asyncio.gather(*[network.aclose() for network in NETWORKS.values()], return_exceptions=False)
312
313

◆ call_client()

SXNG_Response searx.network.network.Network.call_client ( self,
bool stream,
str method,
str url,
**t.Any kwargs )

Definition at line 272 of file network.py.

272 async def call_client(self, stream: bool, method: str, url: str, **kwargs: t.Any) -> SXNG_Response:
273 retries = self.retries
274 was_disconnected = False
275 do_raise_for_httperror = Network.extract_do_raise_for_httperror(kwargs)
276 kwargs_clients = Network.extract_kwargs_clients(kwargs)
277 while retries >= 0: # pragma: no cover
278 client = await self.get_client(**kwargs_clients)
279 cookies = kwargs.pop("cookies", None)
280 client.cookies = httpx.Cookies(cookies)
281 try:
282 if stream:
283 return client.stream(method, url, **kwargs)
284
285 response = await client.request(method, url, **kwargs)
286 if self.is_valid_response(response) or retries <= 0:
287 return self.patch_response(response, do_raise_for_httperror)
288 except httpx.RemoteProtocolError as e:
289 if not was_disconnected:
290 # the server has closed the connection:
291 # try again without decreasing the retries variable & with a new HTTP client
292 was_disconnected = True
293 await client.aclose()
294 self._logger.warning('httpx.RemoteProtocolError: the server has disconnected, retrying')
295 continue
296 if retries <= 0:
297 raise e
298 except (httpx.RequestError, httpx.HTTPStatusError) as e:
299 if retries <= 0:
300 raise e
301 retries -= 1
302

References _logger, get_client(), is_valid_response(), patch_response(), and retries.

Referenced by request(), and stream().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_parameters()

searx.network.network.Network.check_parameters ( self)

Definition at line 104 of file network.py.

104 def check_parameters(self):
105 for address in self.iter_ipaddresses():
106 if '/' in address:
107 ipaddress.ip_network(address, False)
108 else:
109 ipaddress.ip_address(address)
110
111 if self.proxies is not None and not isinstance(self.proxies, (str, dict)):
112 raise ValueError('proxies type has to be str, dict or None')
113

References iter_ipaddresses(), searx.enginelib.Engine.proxies, and proxies.

Here is the call graph for this function:

◆ check_tor_proxy()

bool searx.network.network.Network.check_tor_proxy ( httpx.AsyncClient client,
proxies )
static

Definition at line 167 of file network.py.

167 async def check_tor_proxy(client: httpx.AsyncClient, proxies) -> bool:
168 if proxies in Network._TOR_CHECK_RESULT:
169 return Network._TOR_CHECK_RESULT[proxies]
170
171 result = True
172 # ignore client._transport because it is not used with all://
173 for transport in client._mounts.values(): # pylint: disable=protected-access
174 if isinstance(transport, AsyncHTTPTransportNoHttp):
175 continue
176 if getattr(transport, "_pool") and getattr(
177 # pylint: disable=protected-access
178 transport._pool, # type: ignore
179 "_rdns",
180 False,
181 ):
182 continue
183 return False
184 response = await client.get("https://check.torproject.org/api/ip", timeout=60)
185 if not response.json()["IsTor"]:
186 result = False
187 Network._TOR_CHECK_RESULT[proxies] = result
188 return result
189

Referenced by get_client().

Here is the caller graph for this function:

◆ extract_do_raise_for_httperror()

searx.network.network.Network.extract_do_raise_for_httperror ( dict[str, t.Any] kwargs)
static

Definition at line 239 of file network.py.

239 def extract_do_raise_for_httperror(kwargs: dict[str, t.Any]):
240 do_raise_for_httperror = True
241 if 'raise_for_httperror' in kwargs:
242 do_raise_for_httperror = kwargs['raise_for_httperror']
243 del kwargs['raise_for_httperror']
244 return do_raise_for_httperror
245

◆ extract_kwargs_clients()

dict[str, t.Any] searx.network.network.Network.extract_kwargs_clients ( dict[str, t.Any] kwargs)
static

Definition at line 227 of file network.py.

227 def extract_kwargs_clients(kwargs: dict[str, t.Any]) -> dict[str, t.Any]:
228 kwargs_clients: dict[str, t.Any] = {}
229 if 'verify' in kwargs:
230 kwargs_clients['verify'] = kwargs.pop('verify')
231 if 'max_redirects' in kwargs:
232 kwargs_clients['max_redirects'] = kwargs.pop('max_redirects')
233 if 'allow_redirects' in kwargs:
234 # see https://github.com/encode/httpx/pull/1808
235 kwargs['follow_redirects'] = kwargs.pop('allow_redirects')
236 return kwargs_clients
237

◆ get_client()

httpx.AsyncClient searx.network.network.Network.get_client ( self,
bool | None verify = None,
int | None max_redirects = None )

Definition at line 190 of file network.py.

190 async def get_client(self, verify: bool | None = None, max_redirects: int | None = None) -> httpx.AsyncClient:
191 verify = self.verify if verify is None else verify
192 max_redirects = self.max_redirects if max_redirects is None else max_redirects
193 local_address = next(self._local_addresses_cycle)
194 proxies = next(self._proxies_cycle) # is a tuple so it can be part of the key
195 key = (verify, max_redirects, local_address, proxies)
196 hook_log_response = self.log_response if sxng_debug else None
197 if key not in self._clients or self._clients[key].is_closed:
198 client = new_client(
199 self.enable_http,
200 verify,
201 self.enable_http2,
202 self.max_connections,
203 self.max_keepalive_connections,
204 self.keepalive_expiry,
205 dict(proxies),
206 local_address,
207 0,
208 max_redirects,
209 hook_log_response,
210 )
211 if self.using_tor_proxy and not await self.check_tor_proxy(client, proxies):
212 await client.aclose()
213 raise httpx.ProxyError('Network configuration problem: not using Tor')
214 self._clients[key] = client
215 return self._clients[key]
216

References _clients, _local_addresses_cycle, _proxies_cycle, check_tor_proxy(), enable_http, enable_http2, keepalive_expiry, log_response(), max_connections, max_keepalive_connections, max_redirects, using_tor_proxy, and verify.

Referenced by call_client().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_ipaddress_cycle()

searx.network.network.Network.get_ipaddress_cycle ( self)

Definition at line 122 of file network.py.

122 def get_ipaddress_cycle(self):
123 while True:
124 count = 0
125 for address in self.iter_ipaddresses():
126 if '/' in address:
127 for a in ipaddress.ip_network(address, False).hosts():
128 yield str(a)
129 count += 1
130 else:
131 a = ipaddress.ip_address(address)
132 yield str(a)
133 count += 1
134 if count == 0:
135 yield None
136

References iter_ipaddresses().

Here is the call graph for this function:

◆ get_proxy_cycles()

Generator[tuple[tuple[str, str], ...], str, str] searx.network.network.Network.get_proxy_cycles ( self)

Definition at line 150 of file network.py.

150 def get_proxy_cycles(self) -> Generator[tuple[tuple[str, str], ...], str, str]: # not sure type is correct
151 proxy_settings: dict[str, t.Any] = {}
152 for pattern, proxy_urls in self.iter_proxies():
153 proxy_settings[pattern] = cycle(proxy_urls)
154 while True:
155 # pylint: disable=stop-iteration-return
156 yield tuple((pattern, next(proxy_url_cycle)) for pattern, proxy_url_cycle in proxy_settings.items())
157

References iter_proxies().

Here is the call graph for this function:

◆ is_valid_response()

searx.network.network.Network.is_valid_response ( self,
httpx.Response response )

Definition at line 262 of file network.py.

262 def is_valid_response(self, response: httpx.Response):
263 # pylint: disable=too-many-boolean-expressions
264 if (
265 (self.retry_on_http_error is True and 400 <= response.status_code <= 599)
266 or (isinstance(self.retry_on_http_error, list) and response.status_code in self.retry_on_http_error)
267 or (isinstance(self.retry_on_http_error, int) and response.status_code == self.retry_on_http_error)
268 ):
269 return False
270 return True
271

References retry_on_http_error.

Referenced by call_client().

Here is the caller graph for this function:

◆ iter_ipaddresses()

Generator[str] searx.network.network.Network.iter_ipaddresses ( self)

Definition at line 114 of file network.py.

114 def iter_ipaddresses(self) -> Generator[str]:
115 local_addresses = self.local_addresses
116 if not local_addresses:
117 return
118 if isinstance(local_addresses, str):
119 local_addresses = [local_addresses]
120 yield from local_addresses
121

References local_addresses.

Referenced by check_parameters(), and get_ipaddress_cycle().

Here is the caller graph for this function:

◆ iter_proxies()

Generator[tuple[str, list[str]]] searx.network.network.Network.iter_proxies ( self)

Definition at line 137 of file network.py.

137 def iter_proxies(self) -> Generator[tuple[str, list[str]]]:
138 if not self.proxies:
139 return
140 # https://www.python-httpx.org/compatibility/#proxy-keys
141 if isinstance(self.proxies, str):
142 yield 'all://', [self.proxies]
143 else:
144 for pattern, proxy_url in self.proxies.items():
145 pattern: str = PROXY_PATTERN_MAPPING.get(pattern, pattern)
146 if isinstance(proxy_url, str):
147 proxy_url = [proxy_url]
148 yield pattern, proxy_url
149

References searx.enginelib.Engine.proxies, and proxies.

Referenced by get_proxy_cycles().

Here is the caller graph for this function:

◆ log_response()

searx.network.network.Network.log_response ( self,
httpx.Response response )

Definition at line 158 of file network.py.

158 async def log_response(self, response: httpx.Response):
159 request = response.request
160 status = f"{response.status_code} {response.reason_phrase}"
161 response_line = f"{response.http_version} {status}"
162 content_type = response.headers.get("Content-Type")
163 content_type = f' ({content_type})' if content_type else ''
164 self._logger.debug(f'HTTP Request: {request.method} {request.url} "{response_line}"{content_type}')
165

References _logger.

Referenced by get_client().

Here is the caller graph for this function:

◆ patch_response()

SXNG_Response searx.network.network.Network.patch_response ( self,
httpx.Response response,
bool do_raise_for_httperror )

Definition at line 246 of file network.py.

246 def patch_response(self, response: httpx.Response, do_raise_for_httperror: bool) -> SXNG_Response:
247 if isinstance(response, httpx.Response):
248 response = t.cast(SXNG_Response, response)
249 # requests compatibility (response is not streamed)
250 # see also https://www.python-httpx.org/compatibility/#checking-for-4xx5xx-responses
251 response.ok = not response.is_error
252
253 # raise an exception
254 if do_raise_for_httperror:
255 try:
256 raise_for_httperror(response)
257 except:
258 self._logger.warning(f"HTTP Request failed: {response.request.method} {response.request.url}")
259 raise
260 return response
261

References _logger.

Referenced by call_client().

Here is the caller graph for this function:

◆ request()

SXNG_Response searx.network.network.Network.request ( self,
str method,
str url,
**t.Any kwargs )

Definition at line 303 of file network.py.

303 async def request(self, method: str, url: str, **kwargs: t.Any) -> SXNG_Response:
304 return await self.call_client(False, method, url, **kwargs)
305

References call_client().

Here is the call graph for this function:

◆ stream()

searx.network.network.Network.stream ( self,
str method,
str url,
** kwargs )

Definition at line 306 of file network.py.

306 async def stream(self, method: str, url: str, **kwargs):
307 return await self.call_client(True, method, url, **kwargs)
308

References call_client().

Here is the call graph for this function:

Member Data Documentation

◆ __slots__

tuple searx.network.network.Network.__slots__
staticprivate
Initial value:
= (
'enable_http',
'verify',
'enable_http2',
'max_connections',
'max_keepalive_connections',
'keepalive_expiry',
'local_addresses',
'proxies',
'using_tor_proxy',
'max_redirects',
'retries',
'retry_on_http_error',
'_local_addresses_cycle',
'_proxies_cycle',
'_clients',
'_logger',
)

Definition at line 47 of file network.py.

◆ _clients

dict searx.network.network.Network._clients = {}
protected

Definition at line 100 of file network.py.

Referenced by aclose(), and get_client().

◆ _local_addresses_cycle

searx.network.network.Network._local_addresses_cycle = self.get_ipaddress_cycle()
protected

Definition at line 98 of file network.py.

Referenced by get_client().

◆ _logger

searx.network.network.Network._logger = logger.getChild(logger_name) if logger_name else logger
protected

Definition at line 101 of file network.py.

Referenced by call_client(), log_response(), and patch_response().

◆ _proxies_cycle

Generator[tuple[tuple[str, str], ...], str, str] searx.network.network.Network._proxies_cycle = self.get_proxy_cycles()
protected

Definition at line 99 of file network.py.

Referenced by get_client().

◆ _TOR_CHECK_RESULT

dict searx.network.network.Network._TOR_CHECK_RESULT = {}
staticprotected

Definition at line 66 of file network.py.

◆ enable_http

searx.network.network.Network.enable_http = enable_http

Definition at line 86 of file network.py.

Referenced by get_client().

◆ enable_http2

searx.network.network.Network.enable_http2 = enable_http2

Definition at line 88 of file network.py.

Referenced by get_client().

◆ keepalive_expiry

searx.network.network.Network.keepalive_expiry = keepalive_expiry

Definition at line 91 of file network.py.

Referenced by get_client().

◆ local_addresses

searx.network.network.Network.local_addresses = local_addresses

Definition at line 94 of file network.py.

Referenced by iter_ipaddresses().

◆ max_connections

searx.network.network.Network.max_connections = max_connections

Definition at line 89 of file network.py.

Referenced by get_client().

◆ max_keepalive_connections

searx.network.network.Network.max_keepalive_connections = max_keepalive_connections

Definition at line 90 of file network.py.

Referenced by get_client().

◆ max_redirects

searx.network.network.Network.max_redirects = max_redirects

Definition at line 97 of file network.py.

Referenced by get_client().

◆ proxies

searx.network.network.Network.proxies = proxies

Definition at line 92 of file network.py.

Referenced by check_parameters(), and iter_proxies().

◆ retries

searx.network.network.Network.retries = retries

Definition at line 95 of file network.py.

Referenced by call_client().

◆ retry_on_http_error

searx.network.network.Network.retry_on_http_error = retry_on_http_error

Definition at line 96 of file network.py.

Referenced by is_valid_response().

◆ using_tor_proxy

searx.network.network.Network.using_tor_proxy = using_tor_proxy

Definition at line 93 of file network.py.

Referenced by get_client().

◆ verify

searx.network.network.Network.verify = verify

Definition at line 87 of file network.py.

Referenced by get_client().


The documentation for this class was generated from the following file:
  • /home/andrew/Documents/code/public/searxng/searx/network/network.py