|
|
@@ -1,6 +1,10 @@
|
|
|
import asyncio
|
|
|
import aiohttp
|
|
|
import time
|
|
|
+<<<<<<< HEAD
|
|
|
+=======
|
|
|
+
|
|
|
+>>>>>>> dbb6bb89903b1cd158470ac472a5a930f7f0978b
|
|
|
try:
|
|
|
from aiohttp import ClientError
|
|
|
except:
|
|
|
@@ -12,7 +16,7 @@ from proxypool.setting import *
|
|
|
class Tester(object):
|
|
|
def __init__(self):
|
|
|
self.redis = RedisClient()
|
|
|
-
|
|
|
+
|
|
|
async def test_single_proxy(self, proxy):
|
|
|
"""
|
|
|
测试单个代理
|
|
|
@@ -26,17 +30,17 @@ class Tester(object):
|
|
|
proxy = proxy.decode('utf-8')
|
|
|
real_proxy = 'http://' + proxy
|
|
|
print('正在测试', proxy)
|
|
|
- async with session.get(TEST_URL, proxy=real_proxy, timeout=15) as response:
|
|
|
+ async with session.get(TEST_URL, proxy=real_proxy, timeout=15, allow_redirects=False) as response:
|
|
|
if response.status in VALID_STATUS_CODES:
|
|
|
self.redis.max(proxy)
|
|
|
print('代理可用', proxy)
|
|
|
else:
|
|
|
self.redis.decrease(proxy)
|
|
|
- print('请求响应码不合法,IP', proxy)
|
|
|
+ print('请求响应码不合法 ', response.status, 'IP', proxy)
|
|
|
except (ClientError, aiohttp.client_exceptions.ClientConnectorError, asyncio.TimeoutError, AttributeError):
|
|
|
self.redis.decrease(proxy)
|
|
|
print('代理请求失败', proxy)
|
|
|
-
|
|
|
+
|
|
|
def run(self):
|
|
|
"""
|
|
|
测试主函数
|
|
|
@@ -46,9 +50,8 @@ class Tester(object):
|
|
|
try:
|
|
|
proxies = self.redis.all()
|
|
|
loop = asyncio.get_event_loop()
|
|
|
- batch_size = 100
|
|
|
- for i in range(0, len(proxies), batch_size):
|
|
|
- test_proxies = proxies[i:i + batch_size]
|
|
|
+ for i in range(0, len(proxies), BATCH_TEST_SIZE):
|
|
|
+ test_proxies = proxies[i:i + BATCH_TEST_SIZE]
|
|
|
tasks = [self.test_single_proxy(proxy) for proxy in test_proxies]
|
|
|
loop.run_until_complete(asyncio.wait(tasks))
|
|
|
time.sleep(5)
|