'''
2019-03-21
程序运行说明
测试部分:通过windows自身ping程序运行60秒获取结果,保存为本地文本文件(日志文件)
画图部分:处理保存在本地的日志文件,画成图显示
使用说明
运行平台:Windows7 & Python3.x
依赖第三方包:matplotlib 包(画图要用到)
安装第三方包:
打开 CMD 输入命令 pip install matplotlib 会自动安装 matplotlib 包
BUG:
0、双击运行无效,请使用python IDLE打开,再F5运行
1、测试运行后,如果直接关闭GUI窗口,可能造成后台进程不能终止。请点击停止按钮,等待程序关闭后,再关闭窗口
2、用户输入的参数没有限制类型、范围,不要乱输入
'''
import tkinter
import pickle
import re
import subprocess
from threading import Thread
import time
import os
import time
import logging # 日志
Log = logging.getLogger("__name__") # 获取实例
formatter = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s') # 指定logger输出格式
file_handler = logging.FileHandler("net.log") # 日志文件路径
file_handler.setFormatter(formatter) # 可以通过setFormatter指定输出格式
Log.addHandler(file_handler) # 为logger添加的日志处理器
Log.setLevel(logging.DEBUG) # 设置记录的日志级别
## 程序运行控制的全局字典变量:'S'控制ping程序运行,'测试按钮计数'控制按钮行为
D_S = {'S':1,'测试按钮计数':1}
#print(D_S)
## 开始测试,把测试结果保存到日志文件(开始测试按钮操作函数)
def PING_TEST():
#print(D_S)
if D_S['测试按钮计数'] %2 != 0:
D_S['测试按钮计数'] += 1
##print("START 开始测试...")
text3.insert(1.0, 'START 开始测试...\n')
D_S['S'] = 1 # 设置为1 允许测试程序运行
K1 = 输入框2.get() # 获取输入框的内容
V1 = 输入框1.get()
K2 = 输入框4.get()
V2 = 输入框3.get()
K3 = 输入框6.get()
V3 = 输入框5.get()
K4 = 输入框8.get()
V4 = 输入框7.get()
D_任务 = {} # 准备进行PING测试的任务字典 {'备注':'IP'}
if V1:
if K1:
D_任务[K1] = V1
else:
K1 = V1
D_任务[K1] = V1
if V2:
if K2:
D_任务[K2] = V2
else:
K2 = V2
D_任务[K2] = V2
if V3:
if K3:
D_任务[K3] = V3
else:
K3 = V3
D_任务[K3] = V3
if V4:
if K4:
D_任务[K4] = V4
else:
K4 = V4
D_任务[K4] = V4
#print("准备进行PING测试的任务字典",D_任务)
text1.delete(1.0, tkinter.END) # 清除文本框内容
if len(D_任务) == 0:
text1.insert(1.0, '请先输入要测试的IP地址') # 写入内容
D_S['S'] = 1
D_S['测试按钮计数'] = 1
else:
for i in D_任务:
text1.insert(1.0, '测试任务 ' + i + ' ' + D_任务[i] + '\n')
text1.insert(5.0, '测试数据保存在 net.log 文件中\n')
text1.insert(6.0, '测试程序运行中...\n')
f = open('PING_TEST_INFO.pkl', 'wb') # 保存到文件,让后续画图程序读取
pickle.dump(D_任务, f)
f.close()
def ping(IP):
while D_S['S']:
##print("线程开始",IP)
###text3.insert(tkinter.END, '线程开始 '+IP+'\n')
###time.sleep(5)
#print("程序开始", time.time())
cmd = 'ping -t ' + IP # 长PING测试
win = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE)
#print(IP, "子进程PID", win.pid)
#print("程序暂停", time.time())
time.sleep(60) # 每60秒终止长PING进程,win会计算本次测试结果
#print("暂停结束", time.time())