Use python to test url(check_url_speed v1.0) | LSABLOG

首页 » Program » Python » 正文

Use python to test url(check_url_speed v1.0)

Environment: win7+python2.7

This program can test url,if the url can open,output normal and use ping to test average speed,else output failed.Then if the url can open but can not ping,I give the url a big number(10000) and output ‘Can not ping  but can open ‘.Finally,I count total urls,normal urls and their ping average speed,also output those urls whick can not ping but can open.

#description:check urls and test ping average speed

import urllib2,time
import subprocess  
import re
import optparse
#from threading import Thread

global count0,count1,count2
count0 = 0
count1 = 0
count2 = 0
global urlave
urlave = {}
global aves
aves = []
global speurls
speurls = []

notping = 'Can not ping but can open!'

def ping_ave_speed(tempUrl):
    global speurls
    pingtempUrl = tempUrl.split('/')[2]
    p = subprocess.Popen(["ping.exe",pingtempUrl],
    stdin = subprocess.PIPE,
    stdout = subprocess.PIPE,
    stderr = subprocess.PIPE,
    shell = True)  
    out =                                   
    reg = re.compile("平均 = (\d+)ms", re.IGNORECASE)
    if not reg.findall(out):
        return str(10000)
        return str(reg.findall(out)[0])

def test_url(fname):
    global count0
    global count1
    global count2
    global aves
    global urlave
    opener = urllib2.build_opener()
    opener.addheaders = [('User-agent', 'Mozilla/49.0.2')]
    file0 = open(fname)
    lines = file0.readlines()
    urls = []
    for line in lines:
        temp = line.replace('\n','')
        count0 = count0 + 1

    print 'Beginning check urls:'
    for url in urls:
        tempUrl = url
        count1 = count1 + 1
        try :
            ave = ping_ave_speed(tempUrl)
            urlave[int(ave)] = tempUrl
            print tempUrl+'---normal[ping ave speed:'+ave+']'+'('+str(count1)+'/'+str(count0)+')'
            count2 = count2 + 1
        except urllib2.HTTPError:
            print tempUrl+'---failed('+str(count1)+'/'+str(count0)+')'
        except urllib2.URLError:
            print tempUrl+'---failed('+str(count1)+'/'+str(count0)+')'


if __name__=='__main__':
    Usage = "%prog -f <urlfile>"
    parser = optparse.OptionParser(Usage,version="%prog v1.0")
    parser.add_option('-f',dest='fname',type='string',help='input a urls file name')
    (options,args) = parser.parse_args()
    if(options.fname == None):
        print parser.usage
        fname = options.fname

    print '===========Statistical==========='
    print 'Total check: '+str(count0)+' urls,'+'normal: '+str(count2)+' url'
    print urlave[aves[0]]+'[ping ave speed:'+str(aves[0])+']'+'---1'
    print urlave[aves[1]]+'[ping ave speed:'+str(aves[1])+']'+'---2'
    print urlave[aves[2]]+'[ping ave speed:'+str(aves[2])+']'+'---3'
    print '-----------'
    print notping
    for speurl in speurls:
        print speurl

The program have some bugs,I will update it,let us looking forward to next version!