def find_new_proxy(tn=0): ips = [] if tn == 0: ips = Proxy.get_ip_list_ip66() if tn == 1: ips = Proxy.get_ip_list_xc(0, 1) if tn == 2: ips = Proxy.get_ip_list_xc(1, 1) if tn == 3: ips = Proxy.get_ip_list_xc(2, 1) if tn == 4: ips = Proxy.get_ip_list_xc(3, 1) if tn == 5: ips = Proxy.get_ip_list_89ip(100) if not ips: return True url = 'http://www.baidu.com' # 测试有没有网 if Proxy.Http.http_get('http://www.baidu.com', BaiduHeader) is None: return True test_times = 5 for proxy in ips: info = db_public.proxy2_select2(proxy) if info: continue lost = 0 count = 0 total = 0.0 if Proxy.test_get(proxy, url, BaiduHeader) is None: continue # 测试proxy bt = time.time() for x in range(0, test_times): if Proxy.test_get(proxy, url, BaiduHeader) is None: lost += 1 et = time.time() total += et - bt count += test_times print('proxy={}, loss rate={}, avg={}, count={} lost={} '.format( proxy, lost * 1.0 / test_times, (et - bt) / test_times, test_times, lost)) # 丢包了 if lost >= 1: continue # 时间长 if total > 3: continue db_public.proxy2_insert_or_update(proxy, count, lost, total) return True
def test_proxy(proxy): test_times = 5 v = db_public.proxy2_select2(proxy) if not v: return True proxy = v[0] count = v[1] lost = v[2] total = v[3] url = 'http://www.baidu.com' # 测试有没有网 if Proxy.Http.http_get('http://www.baidu.com', BaiduHeader) is None: return False # 测试proxy nlost = 0 bt = time.time() for x in range(0, test_times): if Proxy.test_get(proxy, url, BaiduHeader) is None: nlost += 1 et = time.time() total += et - bt count += test_times lost += nlost db_public.proxy2_insert_or_update(proxy, count, lost, total) print('proxy={}, loss rate={}, avg={}, count={} lost={} '.format( proxy, nlost * 1.0 / test_times, (et - bt) / test_times, test_times, nlost)) # print('proxy={}, loss rate={}, avg={}, count={} lost={} '.format(proxy, lost*1.0/count, total/count, count, lost)) return True
def test_proxy_get(proxy=''): proxys = db_public.proxy2_select() for v in proxys: proxy = v[0] count = v[1] lost = v[2] total = v[3] url = 'http://www.baidu.com' # 测试有没有网 if Proxy.Http.http_get('http://www.baidu.com', BaiduHeader) is None: return 0 # 测试proxy bt = time.time() for x in range(0, 5): if Proxy.test_get(proxy, url, BaiduHeader) is None: lost += 1 et = time.time() total += et - bt count += 5 db_public.proxy2_insert_or_update(proxy, count, lost, total) return