forked from Earrow/proxy_pool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProxyPool.py
57 lines (40 loc) · 1.31 KB
/
ProxyPool.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
# coding=utf-8
"""
代理池,实现自动抓取、更新、验证代理。
"""
import time
import json
from . import checker
from .utils import get_redis, redis_http_usable, redis_https_usable
class ProxyPool:
def __init__(self):
self._redis = get_redis()
self._checker = checker.Checker()
self._checker.start()
def is_ready(self):
proxies_http_num = self._redis.scard(redis_http_usable)
proxies_https_num = self._redis.scard(redis_https_usable)
if proxies_http_num == 0 or proxies_https_num == 0:
return False
return True
def quit_scheduler(self):
self._checker.quit_scheduler()
@property
def http(self):
while not self.is_ready():
time.sleep(0.5)
proxy_str = self._redis.srandmember(redis_http_usable)
proxy = json.loads(proxy_str)
return '{}:{}'.format(proxy['ip'], proxy['port'])
@property
def https(self):
while not self.is_ready():
time.sleep(0.5)
proxy_str = self._redis.srandmember(redis_https_usable)
proxy = json.loads(proxy_str)
return '{}:{}'.format(proxy['ip'], proxy['port'])
if __name__ == '__main__':
p = ProxyPool()
for i in range(10):
print(p.http, p.https, sep=', ')
time.sleep(0.5)