介绍
采用python3.0写的测量本机访问对应网页速度的小软件,默认支持5个网址访问速度测试,支持异常影响次数计数。支持单次测试和自动测试。如需要更多网址测试,可自行在代码头配置个数。自动测试间隔也可在代码头配置。
样式如图:
软件下载
链接: 百度网盘分享
提取码:m0t5
代码分享
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# python3.0
# 作者:枣枣
# 邮箱:a394474295@163.com
# 博客:https://blog.csdn.net/a394474295
import tkinter
from tkinter import *
from urllib import request
from datetime import *
import time
import shelve
import webbrowser
# 配置需要测试网址个数
testUrlCnt = 5
# 配置默认循环测试间隔时间,单位秒
defLoopTestSpace = 60
#测试网速
class testUrl:
def __init__(self, master,url=u'http://www.baidu.com/',testTimes=1,respAlarmTime = 0.5):
self.conf_frame = Frame(master,width=1024)
self.conf_frame.pack(side=TOP, expand=YES, fill=X, pady=3)
# 网址:
self.s_Url = Label(self.conf_frame, text=u'网址:')
self.s_Url.pack(side=LEFT, expand=YES, fill=BOTH)
# 输入框
self.strVarUrl = StringVar(master)
self.e_url = Entry(self.conf_frame, textvariable=self.strVarUrl,width=32)
self.strVarUrl.set(url)
self.e_url.pack(side=LEFT, expand=YES, fill=BOTH)
# 次数:
self.s_TestTimes = Label(self.conf_frame, text=u'测试次数:')
self.s_TestTimes.pack(side=LEFT, expand=YES, fill=BOTH)
# 输入框
self.strVarTestTimes = StringVar(master)
self.e_testTimes = Entry(self.conf_frame, textvariable=self.strVarTestTimes,width=5)
self.strVarTestTimes.set(testTimes)
self.e_testTimes.pack(side=LEFT, expand=YES, fill=BOTH)
'''
# 循环测试时间
Label(op_frame, text=u'循环测试间隔(单位秒):').pack(side=LEFT, expand=YES, fill=BOTH)
strVarSpace = StringVar(op_frame)
Entry(op_frame, textvariable=strVarSpace, width=5).pack(side=LEFT, expand=YES, fill=BOTH)
strVarSpace.set(defLoopTestSpace)
'''
# 测试结果
self.s_resault = Label(self.conf_frame, text=u'平均响应时间:----', width = 22,anchor=NW)
self.s_resault.pack(side=LEFT, expand=YES, fill=BOTH)
# 超时
# 超时门限:
self.s_respAlarmTime = Label(self.conf_frame, text=u'网络延时判断时间:')
self.s_respAlarmTime.pack(side=LEFT, expand=YES, fill=BOTH)
# 输入框
self.strVarRespAlarmTime = StringVar(master)
self.e_respAlarmTime = Entry(self.conf_frame, textvariable=self.strVarRespAlarmTime, width=5)
self.strVarRespAlarmTime.set(respAlarmTime)
self.e_respAlarmTime.pack(side=LEFT, expand=YES, fill=BOTH)
# 网络延时次数
self.respAlarmCnt=0
self.s_respAlarmCnt = Label(self.conf_frame, text="延时次数:{0}次".format(self.respAlarmCnt), width=15, anchor=NW)
self.s_respAlarmCnt.pack(side=LEFT, expand=YES, fill=BOTH)
def updateRespTime(self):
listRespTime=[]
sumRespTime = 0.0
respOverFlag = 0
if self.strVarUrl.get()!="":
for i in range(int(self.strVarTestTimes.get())):
startTime = datetime.now()
try:
res = request.urlopen(self.strVarUrl.get(),timeout=10)
except:
respOverFlag = 1
print("{1}:网址:{0};响应时间:超时".format(self.strVarUrl.get(), datetime.now()))
else:
endTime = datetime.now()
respTime=(endTime-startTime).total_seconds()
listRespTime.append(respTime)
sumRespTime = sumRespTime + respTime
print("{2}:网址:{0};响应时间:{1:.3f}".format(self.strVarUrl.get(),respTime,datetime.now()))
if respOverFlag == 0:
avgRespTime=sumRespTime/int(self.strVarTestTimes.get())
self.s_resault.config(text = "平均响应时间:{time:.3f}秒".format(time=avgRespTime))
print("{0}:网址:{1};平均响应时间:{2:.3f}".format(datetime.now(),self.strVarUrl.get(),avgRespTime))
# 网络延时判断
if avgRespTime >= float(self.strVarRespAlarmTime.get()):
self.respAlarmCnt=self.respAlarmCnt+1
self.s_respAlarmCnt.config(text = "延时次数:{0}次".format(self.respAlarmCnt))
else:
self.s_resault.config(text="平均响应时间:超时")
self.respAlarmCnt = self.respAlarmCnt + 1
self.s_respAlarmCnt.config(text="延时次数:{0}次".format(self.respAlarmCnt))
def dynUpdateRespTime(self):
self.updateRespTime()
self.loopiId = self.s_resault.after(1000*defLoopTestSpace,self.dynUpdateRespTime)
def dynUpdateStop(self):
self.s_resault.after_cancel(self.loopiId)
def testOnce():
global testUrlCnt
global btnState
global tUrlList
global b_testOnce
global b_ContinueStart
global b_ContinueStart
if btnState == 0 :
for i in range(testUrlCnt):
tUrlList[i].updateRespTime()
def testContinueStart():
global testUrlCnt
global btnState
global tUrlList
global b_testOnce
global b_ContinueStart
global b_ContinueStart
if btnState == 0:
for i in range(testUrlCnt):
tUrlList[i].dynUpdateRespTime()
b_testOnce.config(state=DISABLED)
b_ContinueStart.config(state=DISABLED)
b_ContinueStop.config(state=NORMAL)
btnState=1
def testContinueStop():
global testUrlCnt
global btnState
global tUrlList
global b_testOnce
global b_ContinueStart
global b_ContinueStart
if btnState == 1:
for i in range(testUrlCnt):
tUrlList[i].dynUpdateStop()
b_testOnce.config(state=NORMAL)
b_ContinueStart.config(state=NORMAL)
b_ContinueStop.config(state=DISABLED)
btnState = 0
# 载入参数
top = tkinter.Tk()
top.title("本地网页访问速度测试软件V1.0")
#
f=shelve.open('config')
tUrlList=[]
for i in range(testUrlCnt):
try:
lastUrl = f["url{0}".format(i)]
except:
lastUrl = "http://www.baidu.com/"
try:
lastTestTimes = f["times{0}".format(i)]
except:
lastTestTimes = "2"
try:
respAlmTime = f["respAlarmTime{0}".format(i)]
except:
respAlmTime = "0.5"
tUrlList.append(testUrl(top,url=lastUrl,testTimes = lastTestTimes,respAlarmTime=respAlmTime))
#测试按钮
btnState = 0 # 1 代表按钮 循环测试 0 代表退出循环测试
op_frame = Frame(top)
op_frame.pack(side=TOP, expand=YES, fill=X, pady=3)
b_testOnce = Button(op_frame, text="单次测试", command=testOnce)
b_testOnce.pack(side=LEFT, expand=YES, fill=BOTH)
b_ContinueStart = Button(op_frame, text="循环{0}秒测试".format(defLoopTestSpace), command=testContinueStart)
b_ContinueStart.pack(side=LEFT, expand=YES, fill=BOTH)
b_ContinueStop = Button(op_frame, text="停止循环测试", command=testContinueStop)
b_ContinueStop.pack(side=LEFT, expand=YES, fill=BOTH)
b_ContinueStop.config(state=DISABLED)
#状态栏
statusbar = Frame(top)
statusbar.pack(side=BOTTOM, expand=YES, fill=X, pady=0)
info = Label(statusbar, text="作者:枣枣 邮箱:a394474295@163.com", bd=1, relief=SUNKEN, anchor=W)
info.pack(side=LEFT, expand=YES, fill=X)
link = Label(statusbar, text="博客:https://blog.csdn.net/a394474295", bd=1, relief=SUNKEN, anchor=W,fg="blue", cursor="hand2")
link.pack(side=LEFT, expand=YES, fill=X)
link.bind("<Button-1>", lambda event: webbrowser.open("https://blog.csdn.net/a394474295"))
top.mainloop()
# 保存参数
for i in range(testUrlCnt):
f["url{0}".format(i)]=tUrlList[i].strVarUrl.get()
f["times{0}".format(i)]=tUrlList[i].strVarTestTimes.get()
f["respAlarmTime{0}".format(i)]=tUrlList[i].strVarRespAlarmTime.get()
联系
有兴趣朋友交流邮箱:a394474295@163.com