python代理池的构建2——代理ip是否可用的处理和检查
阅读原文时间:2023年07月08日阅读:1

上一篇博客地址:python代理池的构建1——代理IP类的构建,以及配置文件、日志文件、requests请求头

一、代理ip是否可用的处理(httpbin_validator.py)

#-*-coding:utf-8-*-
#check ip
'''
目标:检查代理IP速度,匿名程度以及支持的协议类型.
步骤:
检查代理IP速度和匿名程度;

代理IP速度:就是从发送请求到获取响应的时间间隔

匿名程度检查:

http://httpbin.org/gethttps://httpbin.org/get 发送请求

如果响应的origin 中有',分割的两个IP就是透明代理IP

如果响应的headers 中包含Proxy-Connection 说明是匿名代理IP,否则就是高匿代理IP检查代理IP协议类型

如果http://httpbin. org/get 发送请求可以成功,说明支持http协议
如果https://httpbin. org/get 发送请求可以成功,说明支持https协议

'''
import time
import requests
import sys
import json

sys.path.append("..") #这一部分就是告诉你你要导入的模块在什么位置(相对于本模块地址)
sys.path.append("../..")
from utils.http import get_requests_headers
from settings import TEST_TIMEOUT
from utils.log import logger
from domain import Proxy

def check_proxy(proxy):
proxies = { #分别对着一个代理ip,进行http尝试和https尝试
'http':'http://{}:{}'.format(proxy.ip,proxy.port),
'https': 'https://{}:{}'.format(proxy.ip, proxy.port),
}

http,http\_nick\_type,http\_speed = \_\_check\_http\_proxies(proxies)  
https, https\_nick\_type, https\_speed = \_\_check\_http\_proxies(proxies,False)  
#0->http,1->https,2->http and https  
if http and https:   #按之前的逻辑进行判断  
    proxy.protocol=2  
    proxy.nick\_type=http\_nick\_type  
    proxy.speed=http\_speed  
elif http:  
    proxy.protocol = 0  
    proxy.nick\_type = http\_nick\_type  
    proxy.speed = http\_speed  
elif https:  
    proxy.protocol = 1  
    proxy.nick\_type = https\_nick\_type  
    proxy.speed = https\_speed  
return proxy

def __check_http_proxies(proxies,is_http=True): #检查代理可用不
nick_type=-1
speed=-1

if is\_http:  
    test\_url = 'http://httpbin.org/get'  
else:  
    test\_url = 'https://httpbin.org/get'

try:  #加上try,防止requests没访问到程序直接中断  
    start = time.time()  #这个是记录当前时间  
    response = requests.get(test\_url,headers=get\_requests\_headers(),proxies=proxies,timeout=TEST\_TIMEOUT)  
    #print(response.text)  
    if response.ok:  
        #ip speed  
        speed = round(time.time()-start)  
        dic = json.loads(response.text)  #把返回信息转化成json类型,也就是字典类型  
        #ip origin  
        origin = dic\['origin'\]  
        proxy\_connection = dic\['headers'\].get('Proxy-Connection',None)  
        #这里用get的原因是,如果获取不到内容可以赋值为None,而不会报错  
        if ',' in origin:  
            nick\_type=2  
        elif proxy\_connection:  
            nick\_type=1  
        else :  
            nick\_type=0  
        return True,nick\_type,speed  
    else:  
        return False,nick\_type,speed  
except Exception as ex:  
    #logger.exception(ex)  
    return False,nick\_type,speed

if __name__ == '__main__': #程序测试
proxy = Proxy('117.95.55.40',port='9999')
print(check_proxy(proxy))

用这部分代码的时候只需要给check_proxy()传一个代理ip对象参数(这个对象在上一篇我们构建过),然后它就会自动检查该代理ip可用性,最后更新这个代理ip信息然后把这个对象返回

二、python代理池的构建的其他链接

python代理池的构建5——对mongodb数据库里面代理ip检查

python代理池的构建4——mongdb数据库的增删改查

python代理池的构建3——爬取代理ip

python代理池的构建1——代理IP类的构建,以及配置文件、日志文件、requests请求头

三、关于代码一些问题解决链接:

协程gevent模块和猴子补丁

python中schedule模块的简单使用 || importlib.import_module动态导入模块

Python中“*”和“**”的用法 || yield的用法 || ‘$in’和'$nin' || python @property的含义