吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3359|回复: 4
收起左侧

[Python 原创] 用python测试url

[复制链接]
LSA 发表于 2017-3-24 16:09
本帖最后由 LSA 于 2017-3-24 16:13 编辑

前段时间在找google镜像站的时候,找到很多但是有些是失效的有些响应很慢,于是写了一个python脚本来测试收集到的镜像站的速度,这是v1.0版还有一些不足,不过能基本满足测试url的需求,有空再完善。

功能描述:测试url能否打开并且利用ping测试响应速度,最后统计出响应速度前3快的url,对于一些能打开但是ping不通的url(可能防火墙阻挡)另外统计。

效果图:

testurl

testurl




代码打包: check_url_speed.rar (1.18 KB, 下载次数: 7)

代码展示:
[Python] 纯文本查看 复制代码
#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 '===========Statistics==========='
    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


免费评分

参与人数 5吾爱币 +5 热心值 +5 收起 理由
13066627762 + 1 + 1 用心讨论,共获提升!
txt + 1 + 1 用心讨论,共获提升!
faithg + 1 + 1 我很赞同!
yulewg + 1 + 1 热心回复!
cwcauc + 1 + 1 用心讨论,共获提升!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

cwcauc 发表于 2017-3-24 16:17
厉害厉害谢谢分享
一剪没 发表于 2017-3-24 18:00 来自手机
cwx9912 发表于 2017-8-26 19:55
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【热心】和【CB】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
liulangcao 发表于 2017-8-27 09:16
感谢分享
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-19 13:17

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表