首页 » 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.

#coding:gb2312
#description:check urls and test ping average speed
#author:LSA
#Date:20170218

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 = p.stdout.read()                                   
    reg = re.compile("平均 = (\d+)ms", re.IGNORECASE)
    if not reg.findall(out):
        speurls.append(tempUrl)
        return str(10000)
    else:
        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','')
        urls.append(temp)
        count0 = count0 + 1

    print 'Beginning check urls:'
    for url in urls:
        tempUrl = url
        count1 = count1 + 1
        try :
            opener.open(tempUrl)
            ave = ping_ave_speed(tempUrl)
            aves.append(int(ave))
            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)+')'
            time.sleep(1)
        except urllib2.URLError:
            
            print tempUrl+'---failed('+str(count1)+'/'+str(count0)+')'
            time.sleep(1)
        time.sleep(0.1)
        aves.sort()
    file0.close()

    
    

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
        exit(0)
    else:
        fname = options.fname

    
    test_url(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!

Comment