今天跟大家分享的是 运用pyhon语言 进行循环指定CDN节点进行url测试,说白了就是 用curl 或者 wget 加url指定 节点测试 看返回的状态码,代码如下:

#!/usr/bin/env python
#-*- coding:utf-8 -*-
import re
import os
import sys
import socket
import requests
class Testurl(object):

    def __init__(self,proxy,url):
        self.proxy   = proxy
        self.url     = url
        self.proxies = {}
        self.repeat  =  []

    def FilterNode(self):
        while True:
            nodeip   = socket.gethostbyname(self.proxy)
            self.repeat.append(nodeip)
            if self.repeat.count(nodeip) == 1:
                pass
            else:
                break

    def StartTest(self):
        for x in self.repeat:
            try:
                self.proxies['http://'] = ['http://%s:80' % str(x)]
                result = requests.get(self.url,proxies = self.proxies)
                status = re.search('[+[0-9]+]',str(result))
            except Exception as e:
                print('\033[40;31m错误\033[0m:请手动尝试检测\n1:不指定节点是否通\n2:测试URL是否为Http')
                sys.exit(1)
            if   status.group() == '[200]':
                print('\033[40;32m%s 200 OK\033[0m' % (x))
            elif status.group() == '[404]':
                print('\033[40;31m%s 404 Not Found\033[0m' % (x)) 
            elif status.group() == '[403]':
                print('\033[40;31m%s 403 Forbidden\033[0m' % (x))
            elif status.group() == '[301]':
                print('\033[40;33m%s 301 Moved Permanently\033[0m'% (x))
            elif status.group() == '[302]':
                print('\033[40;33m%s 302 Fount\033[0m'% (x))
            else:
                print('\033[40m34m无法识别\033[0m' % (x))
def Help():
        print('\033[40;33m-d 指定厂商CNAME\n-h 指定测试URL\033[0m')
if __name__ == '__main__':
    try:
        if  sys.argv[1] == '-d' and sys.argv[3] == '-h':
            Parameter1 = sys.argv[2]
            Parameter2 = sys.argv[4]
            Parameter1 = str(Parameter1)
            Parameter2 = str(Parameter2)
            value = Testurl(Parameter1,Parameter2)
            value.FilterNode()
            value.StartTest()
        else:
            Help()
    except(IndexError,SyntaxError):
            Help()

有任何反馈意见可以加我QQ:1301927919 一起交流学习;

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐