| 
									
										
										
											
												[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
   * can contains network definition
   * propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
     keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
   * retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
   * local_addresses can be "192.168.0.1/24" (it supports IPv6)
   * support_ipv4 & support_ipv6: both True by default
     see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
   * either a full network description
   * either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
											
										 
											2021-04-05 10:43:33 +02:00
										 |  |  | # SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							| 
									
										
										
										
											2021-05-24 16:59:35 +02:00
										 |  |  | # lint: pylint | 
					
						
							| 
									
										
										
										
											2021-09-07 13:34:35 +02:00
										 |  |  | # pylint: disable=missing-module-docstring, global-statement | 
					
						
							| 
									
										
										
											
												[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
   * can contains network definition
   * propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
     keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
   * retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
   * local_addresses can be "192.168.0.1/24" (it supports IPv6)
   * support_ipv4 & support_ipv6: both True by default
     see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
   * either a full network description
   * either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
											
										 
											2021-04-05 10:43:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | import asyncio | 
					
						
							|  |  |  | import threading | 
					
						
							|  |  |  | import concurrent.futures | 
					
						
							| 
									
										
										
										
											2021-08-14 19:36:30 +02:00
										 |  |  | from types import MethodType | 
					
						
							| 
									
										
										
										
											2021-04-14 17:23:15 +02:00
										 |  |  | from timeit import default_timer | 
					
						
							| 
									
										
										
											
												[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
   * can contains network definition
   * propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
     keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
   * retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
   * local_addresses can be "192.168.0.1/24" (it supports IPv6)
   * support_ipv4 & support_ipv6: both True by default
     see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
   * either a full network description
   * either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
											
										 
											2021-04-05 10:43:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | import httpx | 
					
						
							| 
									
										
										
										
											2021-09-28 15:26:34 +02:00
										 |  |  | import anyio | 
					
						
							| 
									
										
										
											
												[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
   * can contains network definition
   * propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
     keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
   * retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
   * local_addresses can be "192.168.0.1/24" (it supports IPv6)
   * support_ipv4 & support_ipv6: both True by default
     see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
   * either a full network description
   * either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
											
										 
											2021-04-05 10:43:33 +02:00
										 |  |  | import h2.exceptions | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from .network import get_network, initialize | 
					
						
							| 
									
										
										
										
											2021-04-27 10:03:19 +02:00
										 |  |  | from .client import get_loop | 
					
						
							| 
									
										
										
											
												[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
   * can contains network definition
   * propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
     keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
   * retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
   * local_addresses can be "192.168.0.1/24" (it supports IPv6)
   * support_ipv4 & support_ipv6: both True by default
     see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
   * either a full network description
   * either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
											
										 
											2021-04-05 10:43:33 +02:00
										 |  |  | from .raise_for_httperror import raise_for_httperror | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # queue.SimpleQueue: Support Python 3.6 | 
					
						
							|  |  |  | try: | 
					
						
							|  |  |  |     from queue import SimpleQueue | 
					
						
							|  |  |  | except ImportError: | 
					
						
							|  |  |  |     from queue import Empty | 
					
						
							|  |  |  |     from collections import deque | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     class SimpleQueue: | 
					
						
							|  |  |  |         """Minimal backport of queue.SimpleQueue""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def __init__(self): | 
					
						
							|  |  |  |             self._queue = deque() | 
					
						
							|  |  |  |             self._count = threading.Semaphore(0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def put(self, item): | 
					
						
							|  |  |  |             self._queue.append(item) | 
					
						
							|  |  |  |             self._count.release() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def get(self): | 
					
						
							| 
									
										
										
										
											2021-05-24 16:59:35 +02:00
										 |  |  |             if not self._count.acquire(True):  #pylint: disable=consider-using-with | 
					
						
							| 
									
										
										
											
												[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
   * can contains network definition
   * propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
     keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
   * retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
   * local_addresses can be "192.168.0.1/24" (it supports IPv6)
   * support_ipv4 & support_ipv6: both True by default
     see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
   * either a full network description
   * either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
											
										 
											2021-04-05 10:43:33 +02:00
										 |  |  |                 raise Empty | 
					
						
							|  |  |  |             return self._queue.popleft() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | THREADLOCAL = threading.local() | 
					
						
							| 
									
										
										
										
											2021-05-24 16:59:35 +02:00
										 |  |  | """Thread-local data is data for thread specific values.""" | 
					
						
							| 
									
										
										
											
												[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
   * can contains network definition
   * propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
     keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
   * retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
   * local_addresses can be "192.168.0.1/24" (it supports IPv6)
   * support_ipv4 & support_ipv6: both True by default
     see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
   * either a full network description
   * either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
											
										 
											2021-04-05 10:43:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | def reset_time_for_thread(): | 
					
						
							|  |  |  |     THREADLOCAL.total_time = 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def get_time_for_thread(): | 
					
						
							| 
									
										
										
										
											2021-05-19 11:24:18 +02:00
										 |  |  |     """returns thread's total time or None""" | 
					
						
							|  |  |  |     return THREADLOCAL.__dict__.get('total_time') | 
					
						
							| 
									
										
										
											
												[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
   * can contains network definition
   * propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
     keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
   * retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
   * local_addresses can be "192.168.0.1/24" (it supports IPv6)
   * support_ipv4 & support_ipv6: both True by default
     see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
   * either a full network description
   * either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
											
										 
											2021-04-05 10:43:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def set_timeout_for_thread(timeout, start_time=None): | 
					
						
							|  |  |  |     THREADLOCAL.timeout = timeout | 
					
						
							|  |  |  |     THREADLOCAL.start_time = start_time | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def set_context_network_name(network_name): | 
					
						
							|  |  |  |     THREADLOCAL.network = get_network(network_name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def get_context_network(): | 
					
						
							| 
									
										
										
										
											2021-05-19 11:24:18 +02:00
										 |  |  |     """If set return thread's network.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     If unset, return value from :py:obj:`get_network`. | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     return THREADLOCAL.__dict__.get('network') or get_network() | 
					
						
							| 
									
										
										
											
												[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
   * can contains network definition
   * propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
     keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
   * retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
   * local_addresses can be "192.168.0.1/24" (it supports IPv6)
   * support_ipv4 & support_ipv6: both True by default
     see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
   * either a full network description
   * either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
											
										 
											2021-04-05 10:43:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def request(method, url, **kwargs): | 
					
						
							|  |  |  |     """same as requests/requests/api.py request(...)""" | 
					
						
							| 
									
										
										
										
											2021-04-14 17:23:15 +02:00
										 |  |  |     time_before_request = default_timer() | 
					
						
							| 
									
										
										
											
												[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
   * can contains network definition
   * propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
     keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
   * retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
   * local_addresses can be "192.168.0.1/24" (it supports IPv6)
   * support_ipv4 & support_ipv6: both True by default
     see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
   * either a full network description
   * either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
											
										 
											2021-04-05 10:43:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # timeout (httpx) | 
					
						
							|  |  |  |     if 'timeout' in kwargs: | 
					
						
							|  |  |  |         timeout = kwargs['timeout'] | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         timeout = getattr(THREADLOCAL, 'timeout', None) | 
					
						
							|  |  |  |         if timeout is not None: | 
					
						
							|  |  |  |             kwargs['timeout'] = timeout | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # 2 minutes timeout for the requests without timeout | 
					
						
							|  |  |  |     timeout = timeout or 120 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # ajdust actual timeout | 
					
						
							|  |  |  |     timeout += 0.2  # overhead | 
					
						
							|  |  |  |     start_time = getattr(THREADLOCAL, 'start_time', time_before_request) | 
					
						
							|  |  |  |     if start_time: | 
					
						
							| 
									
										
										
										
											2021-04-14 17:23:15 +02:00
										 |  |  |         timeout -= default_timer() - start_time | 
					
						
							| 
									
										
										
											
												[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
   * can contains network definition
   * propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
     keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
   * retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
   * local_addresses can be "192.168.0.1/24" (it supports IPv6)
   * support_ipv4 & support_ipv6: both True by default
     see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
   * either a full network description
   * either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
											
										 
											2021-04-05 10:43:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # raise_for_error | 
					
						
							|  |  |  |     check_for_httperror = True | 
					
						
							|  |  |  |     if 'raise_for_httperror' in kwargs: | 
					
						
							|  |  |  |         check_for_httperror = kwargs['raise_for_httperror'] | 
					
						
							|  |  |  |         del kwargs['raise_for_httperror'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # requests compatibility | 
					
						
							|  |  |  |     if isinstance(url, bytes): | 
					
						
							|  |  |  |         url = url.decode() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # network | 
					
						
							|  |  |  |     network = get_context_network() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # do request | 
					
						
							| 
									
										
										
										
											2021-04-27 10:03:19 +02:00
										 |  |  |     future = asyncio.run_coroutine_threadsafe(network.request(method, url, **kwargs), get_loop()) | 
					
						
							| 
									
										
										
											
												[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
   * can contains network definition
   * propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
     keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
   * retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
   * local_addresses can be "192.168.0.1/24" (it supports IPv6)
   * support_ipv4 & support_ipv6: both True by default
     see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
   * either a full network description
   * either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
											
										 
											2021-04-05 10:43:33 +02:00
										 |  |  |     try: | 
					
						
							|  |  |  |         response = future.result(timeout) | 
					
						
							|  |  |  |     except concurrent.futures.TimeoutError as e: | 
					
						
							|  |  |  |         raise httpx.TimeoutException('Timeout', request=None) from e | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # requests compatibility | 
					
						
							|  |  |  |     # see also https://www.python-httpx.org/compatibility/#checking-for-4xx5xx-responses | 
					
						
							|  |  |  |     response.ok = not response.is_error | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # update total_time. | 
					
						
							|  |  |  |     # See get_time_for_thread() and reset_time_for_thread() | 
					
						
							|  |  |  |     if hasattr(THREADLOCAL, 'total_time'): | 
					
						
							| 
									
										
										
										
											2021-04-14 17:23:15 +02:00
										 |  |  |         time_after_request = default_timer() | 
					
						
							| 
									
										
										
											
												[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
   * can contains network definition
   * propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
     keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
   * retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
   * local_addresses can be "192.168.0.1/24" (it supports IPv6)
   * support_ipv4 & support_ipv6: both True by default
     see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
   * either a full network description
   * either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
											
										 
											2021-04-05 10:43:33 +02:00
										 |  |  |         THREADLOCAL.total_time += time_after_request - time_before_request | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # raise an exception | 
					
						
							|  |  |  |     if check_for_httperror: | 
					
						
							|  |  |  |         raise_for_httperror(response) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return response | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def get(url, **kwargs): | 
					
						
							|  |  |  |     kwargs.setdefault('allow_redirects', True) | 
					
						
							|  |  |  |     return request('get', url, **kwargs) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def options(url, **kwargs): | 
					
						
							|  |  |  |     kwargs.setdefault('allow_redirects', True) | 
					
						
							|  |  |  |     return request('options', url, **kwargs) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def head(url, **kwargs): | 
					
						
							|  |  |  |     kwargs.setdefault('allow_redirects', False) | 
					
						
							|  |  |  |     return request('head', url, **kwargs) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def post(url, data=None, **kwargs): | 
					
						
							|  |  |  |     return request('post', url, data=data, **kwargs) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def put(url, data=None, **kwargs): | 
					
						
							|  |  |  |     return request('put', url, data=data, **kwargs) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def patch(url, data=None, **kwargs): | 
					
						
							|  |  |  |     return request('patch', url, data=data, **kwargs) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def delete(url, **kwargs): | 
					
						
							|  |  |  |     return request('delete', url, **kwargs) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-14 19:36:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-24 16:59:35 +02:00
										 |  |  | async def stream_chunk_to_queue(network, queue, method, url, **kwargs): | 
					
						
							| 
									
										
										
											
												[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
   * can contains network definition
   * propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
     keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
   * retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
   * local_addresses can be "192.168.0.1/24" (it supports IPv6)
   * support_ipv4 & support_ipv6: both True by default
     see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
   * either a full network description
   * either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
											
										 
											2021-04-05 10:43:33 +02:00
										 |  |  |     try: | 
					
						
							|  |  |  |         async with network.stream(method, url, **kwargs) as response: | 
					
						
							| 
									
										
										
										
											2021-05-24 16:59:35 +02:00
										 |  |  |             queue.put(response) | 
					
						
							| 
									
										
										
										
											2021-08-16 10:11:45 +02:00
										 |  |  |             # aiter_raw: access the raw bytes on the response without applying any HTTP content decoding | 
					
						
							|  |  |  |             # https://www.python-httpx.org/quickstart/#streaming-responses | 
					
						
							|  |  |  |             async for chunk in response.aiter_raw(65536): | 
					
						
							| 
									
										
										
											
												[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
   * can contains network definition
   * propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
     keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
   * retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
   * local_addresses can be "192.168.0.1/24" (it supports IPv6)
   * support_ipv4 & support_ipv6: both True by default
     see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
   * either a full network description
   * either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
											
										 
											2021-04-05 10:43:33 +02:00
										 |  |  |                 if len(chunk) > 0: | 
					
						
							| 
									
										
										
										
											2021-05-24 16:59:35 +02:00
										 |  |  |                     queue.put(chunk) | 
					
						
							| 
									
										
										
										
											2021-09-28 15:26:34 +02:00
										 |  |  |     except (httpx.StreamClosed, anyio.ClosedResourceError): | 
					
						
							| 
									
										
										
										
											2021-09-10 23:32:30 +02:00
										 |  |  |         # the response was queued before the exception. | 
					
						
							|  |  |  |         # the exception was raised on aiter_raw. | 
					
						
							|  |  |  |         # we do nothing here: in the finally block, None will be queued | 
					
						
							|  |  |  |         # so stream(method, url, **kwargs) generator can stop | 
					
						
							| 
									
										
										
										
											2021-08-14 19:36:30 +02:00
										 |  |  |         pass | 
					
						
							| 
									
										
										
										
											2021-09-10 23:32:30 +02:00
										 |  |  |     except Exception as e:  # pylint: disable=broad-except | 
					
						
							|  |  |  |         # broad except to avoid this scenario: | 
					
						
							|  |  |  |         # exception in network.stream(method, url, **kwargs) | 
					
						
							|  |  |  |         # -> the exception is not catch here | 
					
						
							|  |  |  |         # -> queue None (in finally) | 
					
						
							|  |  |  |         # -> the function below steam(method, url, **kwargs) has nothing to return | 
					
						
							| 
									
										
										
										
											2021-05-24 16:59:35 +02:00
										 |  |  |         queue.put(e) | 
					
						
							| 
									
										
										
											
												[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
   * can contains network definition
   * propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
     keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
   * retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
   * local_addresses can be "192.168.0.1/24" (it supports IPv6)
   * support_ipv4 & support_ipv6: both True by default
     see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
   * either a full network description
   * either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
											
										 
											2021-04-05 10:43:33 +02:00
										 |  |  |     finally: | 
					
						
							| 
									
										
										
										
											2021-05-24 16:59:35 +02:00
										 |  |  |         queue.put(None) | 
					
						
							| 
									
										
										
											
												[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
   * can contains network definition
   * propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
     keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
   * retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
   * local_addresses can be "192.168.0.1/24" (it supports IPv6)
   * support_ipv4 & support_ipv6: both True by default
     see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
   * either a full network description
   * either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
											
										 
											2021-04-05 10:43:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-28 15:26:34 +02:00
										 |  |  | def _stream_generator(method, url, **kwargs): | 
					
						
							|  |  |  |     queue = SimpleQueue() | 
					
						
							|  |  |  |     network = get_context_network() | 
					
						
							|  |  |  |     future = asyncio.run_coroutine_threadsafe( | 
					
						
							|  |  |  |         stream_chunk_to_queue(network, queue, method, url, **kwargs), | 
					
						
							|  |  |  |         get_loop() | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # yield chunks | 
					
						
							|  |  |  |     obj_or_exception = queue.get() | 
					
						
							|  |  |  |     while obj_or_exception is not None: | 
					
						
							|  |  |  |         if isinstance(obj_or_exception, Exception): | 
					
						
							|  |  |  |             raise obj_or_exception | 
					
						
							|  |  |  |         yield obj_or_exception | 
					
						
							|  |  |  |         obj_or_exception = queue.get() | 
					
						
							|  |  |  |     future.result() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-14 19:36:30 +02:00
										 |  |  | def _close_response_method(self): | 
					
						
							|  |  |  |     asyncio.run_coroutine_threadsafe( | 
					
						
							|  |  |  |         self.aclose(), | 
					
						
							|  |  |  |         get_loop() | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2021-09-28 15:26:34 +02:00
										 |  |  |     # reach the end of _self.generator ( _stream_generator ) to an avoid memory leak. | 
					
						
							|  |  |  |     # it makes sure that : | 
					
						
							|  |  |  |     # * the httpx response is closed (see the stream_chunk_to_queue function) | 
					
						
							|  |  |  |     # * to call future.result() in _stream_generator | 
					
						
							|  |  |  |     for _ in self._generator:  # pylint: disable=protected-access | 
					
						
							|  |  |  |         continue | 
					
						
							| 
									
										
										
										
											2021-08-14 19:36:30 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
   * can contains network definition
   * propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
     keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
   * retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
   * local_addresses can be "192.168.0.1/24" (it supports IPv6)
   * support_ipv4 & support_ipv6: both True by default
     see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
   * either a full network description
   * either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
											
										 
											2021-04-05 10:43:33 +02:00
										 |  |  | def stream(method, url, **kwargs): | 
					
						
							|  |  |  |     """Replace httpx.stream.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Usage: | 
					
						
							| 
									
										
										
										
											2021-09-28 17:44:00 +02:00
										 |  |  |     response, stream = poolrequests.stream(...) | 
					
						
							| 
									
										
										
											
												[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
   * can contains network definition
   * propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
     keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
   * retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
   * local_addresses can be "192.168.0.1/24" (it supports IPv6)
   * support_ipv4 & support_ipv6: both True by default
     see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
   * either a full network description
   * either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
											
										 
											2021-04-05 10:43:33 +02:00
										 |  |  |     for chunk in stream: | 
					
						
							|  |  |  |         ... | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     httpx.Client.stream requires to write the httpx.HTTPTransport version of the | 
					
						
							|  |  |  |     the httpx.AsyncHTTPTransport declared above. | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2021-09-28 15:26:34 +02:00
										 |  |  |     generator = _stream_generator(method, url, **kwargs) | 
					
						
							| 
									
										
										
										
											2021-08-14 19:36:30 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # yield response | 
					
						
							| 
									
										
										
										
											2021-09-28 15:26:34 +02:00
										 |  |  |     response = next(generator)  # pylint: disable=stop-iteration-return | 
					
						
							| 
									
										
										
										
											2021-08-14 19:36:30 +02:00
										 |  |  |     if isinstance(response, Exception): | 
					
						
							|  |  |  |         raise response | 
					
						
							| 
									
										
										
										
											2021-09-28 15:26:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     response._generator = generator  # pylint: disable=protected-access | 
					
						
							| 
									
										
										
										
											2021-08-14 19:36:30 +02:00
										 |  |  |     response.close = MethodType(_close_response_method, response) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-28 17:44:00 +02:00
										 |  |  |     return response, generator |