def validate_proxy_ip(p: ProxyIP): # logger.debug('Validating ip: {}'.format(p.ip)) policy = ValidationPolicy(proxy_ip=p) if not policy.should_validate(): return v = Validator(host=p.ip, port=int(p.port), using_https=policy.should_try_https()) try: v.validate() except (KeyboardInterrupt, SystemExit): logger.info('KeyboardInterrupt terminates validate_proxy_ip: ' + p.ip) meta = v.meta if v.meta else {} validated_ip = ProxyIP(ip=p.ip, port=p.port, **meta) # save valid ip into database validated_ip.latency = v.latency validated_ip.stability = v.success_rate validated_ip.is_valid = v.valid validated_ip.is_anonymous = v.anonymous # Increase attempts and https_attempts validated_ip.attempts = validated_ip.attempts + 1 if v.using_https: validated_ip.https_attempts = validated_ip.https_attempts + 1 if v.valid: validated_ip.is_https = v.using_https # logger.debug('Save valid ip into database: \n' + validated_ip.__str__()) save_ip(validated_ip)
def test_should_validate_policy_attempts_3_after_24h_in_48h(p: ProxyIP): p.attempts = 3 p.created_at = datetime.now() - timedelta(hours=25) policy = ValidationPolicy(proxy_ip=p) assert policy.should_validate()
def test_should_try_https_attempts_3(valid_http_proxy: ProxyIP): valid_http_proxy.attempts = 3 policy = ValidationPolicy(proxy_ip=valid_http_proxy) assert not policy.should_try_https()
def test_should_validate_policy_attempts_3(p: ProxyIP): p.attempts = 3 policy = ValidationPolicy(proxy_ip=p) assert not policy.should_validate()