Skip to content

Commit

Permalink
modify proxy valid use queue
Browse files Browse the repository at this point in the history
  • Loading branch information
highroom committed Apr 5, 2018
1 parent b8d9e30 commit 663788c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 24 deletions.
43 changes: 23 additions & 20 deletions Schedule/ProxyCheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,35 @@


class ProxyCheck(ProxyManager, Thread):
def __init__(self):
def __init__(self, queue, item_dict):
ProxyManager.__init__(self)
Thread.__init__(self)
self.log = LogHandler('proxy_check')
self.queue = queue
self.item_dict = item_dict

def run(self):
self.db.changeTable(self.useful_proxy_queue)
while True:
for proxy, count in self.db.getAll().items():
if validUsefulProxy(proxy):
# 验证通过计数器减1
if count and int(count) > 0:
self.db.put(proxy, num=int(count) - 1)
else:
pass
self.log.info('ProxyCheck: {} validation pass'.format(proxy))
if self.queue.qsize():
proxy = self.queue.get()
count = self.item_dict[proxy]
if validUsefulProxy(proxy):
# 验证通过计数器减1
if count and int(count) > 0:
self.db.put(proxy, num=int(count) - 1)
else:
self.log.info('ProxyCheck: {} validation fail'.format(proxy))
if count and int(count) > FAIL_COUNT:
self.log.info('ProxyCheck: {} fail too many, delete!'.format(proxy))
self.db.delete(proxy)
else:
self.db.put(proxy, num=int(count) + 1)
sleep(60 * 5)
pass
self.log.info('ProxyCheck: {} validation pass'.format(proxy))
else:
self.log.info('ProxyCheck: {} validation fail'.format(proxy))
if count and int(count) > FAIL_COUNT:
self.log.info('ProxyCheck: {} fail too many, delete!'.format(proxy))
self.db.delete(proxy)
else:
self.db.put(proxy, num=int(count) + 1)
self.queue.task_done()


if __name__ == '__main__':
p = ProxyCheck()
p.run()
# p = ProxyCheck()
# p.run()
pass
25 changes: 21 additions & 4 deletions Schedule/ProxyValidSchedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@
sys.path.append('../')

from Schedule.ProxyCheck import ProxyCheck
from Manager.ProxyManager import ProxyManager
from queue import Queue
import time


class ProxyValidSchedule(object):
class ProxyValidSchedule(ProxyManager, object):
def __init__(self):
pass
ProxyManager.__init__(self)
self.queue = Queue()

def __validProxy(self, threads=5):
"""
Expand All @@ -31,7 +35,7 @@ def __validProxy(self, threads=5):
"""
thread_list = list()
for index in range(threads):
thread_list.append(ProxyCheck())
thread_list.append(ProxyCheck(self.queue, self.item_dict))

for thread in thread_list:
thread.daemon = True
Expand All @@ -41,7 +45,20 @@ def __validProxy(self, threads=5):
thread.join()

def main(self):
self.__validProxy()
self.put_queue()
while True:
if self.queue.qsize():
self.__validProxy()
else:
print('Time sleep 5 minutes.')
time.sleep(60 * 5)
self.put_queue()

def put_queue(self):
self.db.changeTable(self.useful_proxy_queue)
self.item_dict = self.db.getAll()
for item in self.item_dict:
self.queue.put(item)


def run():
Expand Down

0 comments on commit 663788c

Please sign in to comment.