|
|
@@ -3,11 +3,11 @@ import aiohttp
|
|
|
from loguru import logger
|
|
|
from proxypool.schemas import Proxy
|
|
|
from proxypool.storages.redis import RedisClient
|
|
|
-from proxypool.setting import TEST_TIMEOUT, TEST_BATCH, TEST_URL, TEST_VALID_STATUS, TEST_ANONYMOUS
|
|
|
+from proxypool.setting import TEST_TIMEOUT, TEST_BATCH, TEST_URL, TEST_VALID_STATUS, TEST_ANONYMOUS, \
|
|
|
+ TEST_DONT_SET_MAX_SCORE
|
|
|
from aiohttp import ClientProxyConnectionError, ServerDisconnectedError, ClientOSError, ClientHttpProxyError
|
|
|
from asyncio import TimeoutError
|
|
|
|
|
|
-
|
|
|
EXCEPTIONS = (
|
|
|
ClientProxyConnectionError,
|
|
|
ConnectionRefusedError,
|
|
|
@@ -23,14 +23,14 @@ class Tester(object):
|
|
|
"""
|
|
|
tester for testing proxies in queue
|
|
|
"""
|
|
|
-
|
|
|
+
|
|
|
def __init__(self):
|
|
|
"""
|
|
|
init redis
|
|
|
"""
|
|
|
self.redis = RedisClient()
|
|
|
self.loop = asyncio.get_event_loop()
|
|
|
-
|
|
|
+
|
|
|
async def test(self, proxy: Proxy):
|
|
|
"""
|
|
|
test single proxy
|
|
|
@@ -55,15 +55,18 @@ class Tester(object):
|
|
|
async with session.get(TEST_URL, proxy=f'http://{proxy.string()}', timeout=TEST_TIMEOUT,
|
|
|
allow_redirects=False) as response:
|
|
|
if response.status in TEST_VALID_STATUS:
|
|
|
- self.redis.max(proxy)
|
|
|
- logger.debug(f'proxy {proxy.string()} is valid, set max score')
|
|
|
+ if TEST_DONT_SET_MAX_SCORE:
|
|
|
+ logger.debug(f'proxy {proxy.string()} is valid, remain current score')
|
|
|
+ else:
|
|
|
+ self.redis.max(proxy)
|
|
|
+ logger.debug(f'proxy {proxy.string()} is valid, set max score')
|
|
|
else:
|
|
|
self.redis.decrease(proxy)
|
|
|
logger.debug(f'proxy {proxy.string()} is invalid, decrease score')
|
|
|
except EXCEPTIONS:
|
|
|
self.redis.decrease(proxy)
|
|
|
logger.debug(f'proxy {proxy.string()} is invalid, decrease score')
|
|
|
-
|
|
|
+
|
|
|
@logger.catch
|
|
|
def run(self):
|
|
|
"""
|
|
|
@@ -84,14 +87,15 @@ class Tester(object):
|
|
|
if not cursor:
|
|
|
break
|
|
|
|
|
|
+
|
|
|
def run_tester():
|
|
|
host = '96.113.165.182'
|
|
|
port = '3128'
|
|
|
tasks = [tester.test(Proxy(host=host, port=port))]
|
|
|
tester.loop.run_until_complete(asyncio.wait(tasks))
|
|
|
|
|
|
+
|
|
|
if __name__ == '__main__':
|
|
|
tester = Tester()
|
|
|
tester.run()
|
|
|
# run_tester()
|
|
|
-
|