[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
|
|
|
|
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():
|
2021-05-24 16:59:35 +02:00
|
|
|
global THREADLOCAL
|
[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 = 0
|
|
|
|
|
|
|
|
|
|
|
|
def get_time_for_thread():
|
2021-05-19 11:24:18 +02:00
|
|
|
"""returns thread's total time or None"""
|
2021-05-24 16:59:35 +02:00
|
|
|
global THREADLOCAL
|
2021-05-19 11:24:18 +02:00
|
|
|
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):
|
2021-05-24 16:59:35 +02:00
|
|
|
global THREADLOCAL
|
[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.timeout = timeout
|
|
|
|
THREADLOCAL.start_time = start_time
|
|
|
|
|
|
|
|
|
|
|
|
def set_context_network_name(network_name):
|
2021-05-24 16:59:35 +02:00
|
|
|
global THREADLOCAL
|
[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.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`.
|
|
|
|
"""
|
2021-05-24 16:59:35 +02:00
|
|
|
global THREADLOCAL
|
2021-05-19 11:24:18 +02:00
|
|
|
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-05-24 16:59:35 +02:00
|
|
|
global THREADLOCAL
|
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-08-31 10:51:50 +02:00
|
|
|
except httpx.ResponseClosed:
|
2021-08-14 19:36:30 +02:00
|
|
|
# the response was closed
|
|
|
|
pass
|
[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
|
|
|
except (httpx.HTTPError, OSError, h2.exceptions.ProtocolError) as e:
|
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-08-14 19:36:30 +02:00
|
|
|
def _close_response_method(self):
|
|
|
|
asyncio.run_coroutine_threadsafe(
|
|
|
|
self.aclose(),
|
|
|
|
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
|
|
|
def stream(method, url, **kwargs):
|
|
|
|
"""Replace httpx.stream.
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
stream = poolrequests.stream(...)
|
|
|
|
response = next(stream)
|
|
|
|
for chunk in stream:
|
|
|
|
...
|
|
|
|
|
|
|
|
httpx.Client.stream requires to write the httpx.HTTPTransport version of the
|
|
|
|
the httpx.AsyncHTTPTransport declared above.
|
|
|
|
"""
|
2021-05-24 16:59:35 +02:00
|
|
|
queue = SimpleQueue()
|
|
|
|
future = asyncio.run_coroutine_threadsafe(
|
|
|
|
stream_chunk_to_queue(get_network(), queue, method, url, **kwargs),
|
|
|
|
get_loop()
|
|
|
|
)
|
2021-08-14 19:36:30 +02:00
|
|
|
|
|
|
|
# yield response
|
|
|
|
response = queue.get()
|
|
|
|
if isinstance(response, Exception):
|
|
|
|
raise response
|
|
|
|
response.close = MethodType(_close_response_method, response)
|
|
|
|
yield response
|
|
|
|
|
|
|
|
# yield chunks
|
2021-05-24 16:59:35 +02:00
|
|
|
chunk_or_exception = queue.get()
|
[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
|
|
|
while chunk_or_exception is not None:
|
|
|
|
if isinstance(chunk_or_exception, Exception):
|
|
|
|
raise chunk_or_exception
|
|
|
|
yield chunk_or_exception
|
2021-05-24 16:59:35 +02:00
|
|
|
chunk_or_exception = queue.get()
|
2021-08-14 19:36:30 +02:00
|
|
|
future.result()
|