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

Classes

class  Network
 

Functions

 get_network (name=None)
 
 check_network_configuration ()
 
 initialize (settings_engines=None, settings_outgoing=None)
 
 done ()
 

Variables

 logger = logger.getChild('network')
 
str DEFAULT_NAME = '__DEFAULT__'
 
dict NETWORKS = {}
 
dict PROXY_PATTERN_MAPPING
 
dict ADDRESS_MAPPING = {'ipv4': '0.0.0.0', 'ipv6': '::'}
 

Function Documentation

◆ check_network_configuration()

searx.network.network.check_network_configuration ( )

Definition at line 303 of file network.py.

303def check_network_configuration():
304 async def check():
305 exception_count = 0
306 for network in NETWORKS.values():
307 if network.using_tor_proxy:
308 try:
309 await network.get_client()
310 except Exception: # pylint: disable=broad-except
311 network._logger.exception('Error') # pylint: disable=protected-access
312 exception_count += 1
313 return exception_count
314
315 future = asyncio.run_coroutine_threadsafe(check(), get_loop())
316 exception_count = future.result()
317 if exception_count > 0:
318 raise RuntimeError("Invalid network configuration")
319
320

◆ done()

searx.network.network.done ( )
Close all HTTP client

Avoid a warning at exit
See https://github.com/encode/httpx/pull/2026

Note: since Network.aclose has to be async, it is not possible to call this method on Network.__del__
So Network.aclose is called here using atexit.register

Definition at line 406 of file network.py.

406def done():
407 """Close all HTTP client
408
409 Avoid a warning at exit
410 See https://github.com/encode/httpx/pull/2026
411
412 Note: since Network.aclose has to be async, it is not possible to call this method on Network.__del__
413 So Network.aclose is called here using atexit.register
414 """
415 try:
416 loop = get_loop()
417 if loop:
418 future = asyncio.run_coroutine_threadsafe(Network.aclose_all(), loop)
419 # wait 3 seconds to close the HTTP clients
420 future.result(3)
421 finally:
422 NETWORKS.clear()
423
424

Referenced by searx.network.network.initialize().

+ Here is the caller graph for this function:

◆ get_network()

searx.network.network.get_network ( name = None)

Definition at line 299 of file network.py.

299def get_network(name=None):
300 return NETWORKS.get(name or DEFAULT_NAME)
301
302

◆ initialize()

searx.network.network.initialize ( settings_engines = None,
settings_outgoing = None )

Definition at line 321 of file network.py.

321def initialize(settings_engines=None, settings_outgoing=None):
322 # pylint: disable=import-outside-toplevel)
323 from searx.engines import engines
324 from searx import settings
325
326 # pylint: enable=import-outside-toplevel)
327
328 settings_engines = settings_engines or settings['engines']
329 settings_outgoing = settings_outgoing or settings['outgoing']
330
331 # default parameters for AsyncHTTPTransport
332 # see https://github.com/encode/httpx/blob/e05a5372eb6172287458b37447c30f650047e1b8/httpx/_transports/default.py#L108-L121 # pylint: disable=line-too-long
333 default_params = {
334 'enable_http': False,
335 'verify': settings_outgoing['verify'],
336 'enable_http2': settings_outgoing['enable_http2'],
337 'max_connections': settings_outgoing['pool_connections'],
338 'max_keepalive_connections': settings_outgoing['pool_maxsize'],
339 'keepalive_expiry': settings_outgoing['keepalive_expiry'],
340 'local_addresses': settings_outgoing['source_ips'],
341 'using_tor_proxy': settings_outgoing['using_tor_proxy'],
342 'proxies': settings_outgoing['proxies'],
343 'max_redirects': settings_outgoing['max_redirects'],
344 'retries': settings_outgoing['retries'],
345 'retry_on_http_error': None,
346 }
347
348 def new_network(params, logger_name=None):
349 nonlocal default_params
350 result = {}
351 result.update(default_params)
352 result.update(params)
353 if logger_name:
354 result['logger_name'] = logger_name
355 return Network(**result)
356
357 def iter_networks():
358 nonlocal settings_engines
359 for engine_spec in settings_engines:
360 engine_name = engine_spec['name']
361 engine = engines.get(engine_name)
362 if engine is None:
363 continue
364 network = getattr(engine, 'network', None)
365 yield engine_name, engine, network
366
367 if NETWORKS:
368 done()
369 NETWORKS.clear()
370 NETWORKS[DEFAULT_NAME] = new_network({}, logger_name='default')
371 NETWORKS['ipv4'] = new_network({'local_addresses': '0.0.0.0'}, logger_name='ipv4')
372 NETWORKS['ipv6'] = new_network({'local_addresses': '::'}, logger_name='ipv6')
373
374 # define networks from outgoing.networks
375 for network_name, network in settings_outgoing['networks'].items():
376 NETWORKS[network_name] = new_network(network, logger_name=network_name)
377
378 # define networks from engines.[i].network (except references)
379 for engine_name, engine, network in iter_networks():
380 if network is None:
381 network = {}
382 for attribute_name, attribute_value in default_params.items():
383 if hasattr(engine, attribute_name):
384 network[attribute_name] = getattr(engine, attribute_name)
385 else:
386 network[attribute_name] = attribute_value
387 NETWORKS[engine_name] = new_network(network, logger_name=engine_name)
388 elif isinstance(network, dict):
389 NETWORKS[engine_name] = new_network(network, logger_name=engine_name)
390
391 # define networks from engines.[i].network (references)
392 for engine_name, engine, network in iter_networks():
393 if isinstance(network, str):
394 NETWORKS[engine_name] = NETWORKS[network]
395
396 # the /image_proxy endpoint has a dedicated network.
397 # same parameters than the default network, but HTTP/2 is disabled.
398 # It decreases the CPU load average, and the total time is more or less the same
399 if 'image_proxy' not in NETWORKS:
400 image_proxy_params = default_params.copy()
401 image_proxy_params['enable_http2'] = False
402 NETWORKS['image_proxy'] = new_network(image_proxy_params, logger_name='image_proxy')
403
404
405@atexit.register
::1337x
Definition 1337x.py:1

References searx.network.network.done().

+ Here is the call graph for this function:

Variable Documentation

◆ ADDRESS_MAPPING

dict searx.network.network.ADDRESS_MAPPING = {'ipv4': '0.0.0.0', 'ipv6': '::'}

Definition at line 35 of file network.py.

◆ DEFAULT_NAME

str searx.network.network.DEFAULT_NAME = '__DEFAULT__'

Definition at line 19 of file network.py.

◆ logger

searx.network.network.logger = logger.getChild('network')

Definition at line 18 of file network.py.

◆ NETWORKS

dict searx.network.network.NETWORKS = {}

Definition at line 20 of file network.py.

◆ PROXY_PATTERN_MAPPING

dict searx.network.network.PROXY_PATTERN_MAPPING
Initial value:
1= {
2 'http': 'http://',
3 'https': 'https://',
4 'socks4': 'socks4://',
5 'socks5': 'socks5://',
6 'socks5h': 'socks5h://',
7 'http:': 'http://',
8 'https:': 'https://',
9 'socks4:': 'socks4://',
10 'socks5:': 'socks5://',
11 'socks5h:': 'socks5h://',
12}

Definition at line 22 of file network.py.