阿里云9.9元云服务器限时购买

python 检测免费代理ip是否有效

python 检测免费代理ip是否有效,免费ip获取地址https://www.zdaye.com/free/

import requests

IPAgents = [
    "218.89.51.167:9091",
    "120.37.121.209:9091",
    "113.195.207.249:9091",
    "113.195.207.249:9091",
    "111.47.170.136:9091",
    "111.72.218.180:9091",
    ]

#检查代理IP
def check_proxy(IPAgents):
    try:
        # 设置重连次数
        requests.adapters.DEFAULT_RETRIES = 3
        proxy = f"http://{IPAgents}"
        thisIP = "".join(IPAgents.split(":")[0:1])#现在ip去掉端口
        res = requests.get(url="http://icanhazip.com/",timeout=2,proxies={"http":proxy})
        proxyIP = res.text.strip()#返回的有效ip
        if(proxyIP == thisIP):
            print("代理IP:'"+ proxyIP + "'有效!")
        else:
            print("代理IP无效!")
    except Exception as e:
        print(str(e))

# 检查IP是否有效
for ckip in IPAgents:
    check_proxy(ckip)

 

posted @ 2023-06-15 13:32  HIIT  阅读(117)  评论(0编辑  收藏  举报
阿里云限时红包 最高 ¥ 2000 云产品通用红包,可叠加官网常规优惠使用