-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvalidator.py
63 lines (51 loc) · 1.92 KB
/
validator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
# _*_ coding:utf-8 _*_
# @Author :triangle
# @Time :2019/4/30 15:58
# @Filename :validator.py
import os
import asyncio
import aiohttp
from settings import VALIDATOR_BASE_URL, VALIDATOR_BATCH_COUNT, REQUEST_TIMEOUT
from logger import logger
from db import RedisClient
VALIDATOR_BASE_URL = os.environ.get("VALIDATOR_BASE_URL") or VALIDATOR_BASE_URL
class Validator:
def __init__(self):
self.redis = RedisClient()
async def test_proxy(self, proxy):
"""
测试代理
:param proxy: 指定代理
"""
async with aiohttp.ClientSession() as session:
try:
if isinstance(proxy, bytes):
proxy = proxy.decode("utf8")
async with session.get(
VALIDATOR_BASE_URL, proxy=proxy, timeout=REQUEST_TIMEOUT
) as resp:
if resp.status == 200:
self.redis.increase_proxy_score(proxy)
logger.info("Validator √ {}".format(proxy))
else:
self.redis.reduce_proxy_score(proxy)
logger.info("Validator × {}".format(proxy))
except:
self.redis.reduce_proxy_score(proxy)
logger.info("Validator × {}".format(proxy))
def run(self):
"""
启动校验器
"""
logger.info("Validator working...")
logger.info("Validator website is {}".format(VALIDATOR_BASE_URL))
proxies = self.redis.all_proxies()
loop = asyncio.get_event_loop()
for i in range(0, len(proxies), VALIDATOR_BATCH_COUNT):
_proxies = proxies[i : i + VALIDATOR_BATCH_COUNT]
tasks = [self.test_proxy(proxy) for proxy in _proxies]
if tasks:
loop.run_until_complete(asyncio.wait(tasks))
logger.info("Validator resting...")
validator = Validator()