近日接到人行通知,要求對金融行業(yè)IPv6規(guī)模部署情況進(jìn)行專項排查,其中一項內(nèi)容就是針對門戶網(wǎng)站連接穩(wěn)定性進(jìn)行監(jiān)控,具體要求就是:
金融服務(wù)機(jī)構(gòu)提供正式報告說明穩(wěn)定性測試數(shù)據(jù)。(不少于15天時間,每隔1小時發(fā)起門戶網(wǎng)站連接,記錄連接失敗率),應(yīng)網(wǎng)絡(luò)同事要求,結(jié)合自己最近學(xué)習(xí)的Python,寫了如下程序,將執(zhí)行結(jié)果保存到文件中,源碼如下,僅供Python愛好者一起分析學(xué)習(xí):
************************************************
#coding=utf-8
import requests
import csv
import datetime
import time
from requests.exceptions import RequestException
from threading import Timer
#本程序運(yùn)行每間隔指定時間,產(chǎn)生一條數(shù)據(jù),數(shù)據(jù)文件為csv格式,每行文件內(nèi)容為:
#當(dāng)前時間,聯(lián)通IPv6耗時,聯(lián)通IPv4耗時,聯(lián)通耗時結(jié)果,電信IPv6耗時,電信IPv4耗時,電信耗時結(jié)果
#是否滿足要求公式:IPv6耗時-IPv4耗時<75ms,通過sucess;IPv6耗時-IPv4耗時>75ms,不通過fail
#定義獲取時間方法
def get_time():
url1 = 'http://[xx:xx:xxxx::x]'#門戶網(wǎng)站聯(lián)通6,需要調(diào)整為自己的IP
url2 = 'http://xx.xx.xx.xx' #門戶網(wǎng)站聯(lián)通4,需要調(diào)整為自己的IP
url3 = 'http://[xx:xx:xx::x]' #門戶網(wǎng)站電信6,需要調(diào)整為自己的IP
url4 = 'http://xx.x.xx.xx' #門戶網(wǎng)站電信4,需要調(diào)整為自己的IP
destList = [url1,url2,url3,url4]
#print(destList)
now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
timeList = []
dataList = []
#當(dāng)前時間
dataList.Append(str(now_time))
timeOut = 'timeOut'
timePass = 'sucess'
timeFail = 'fail'
other = 'none'
#IPv6-IPv4耗時最低合格時間75ms
timeLimit = 75
#獲取每個請求的時間ms,超時數(shù)組則存放timeOut
for url in destList:
#print(url)
try:
reqDest = requests.get(url)
timeList.append(reqDest.elapsed.total_seconds()*1000)
except (RequestException):
timeList.append(timeOut)
#準(zhǔn)備數(shù)據(jù)行
#聯(lián)通線路請求耗時數(shù)據(jù)
if(timeOut != timeList[0]):
dataList.append(str(round(timeList[0],3))+'ms')
else:
dataList.append(str(timeList[0]))
if(timeOut != timeList[1]):
dataList.append(str(round(timeList[1],3))+'ms')
else:
dataList.append(str(timeList[1]))
if(timeOut != timeList[0] and timeOut != timeList[1]):
ltTimeLast= timeList[0]-timeList[1]
if(ltTimeLast < timeLimit):
dataList.append(timePass)
else:
dataList.append(timeFail)
else:
dataList.append(other)
#電信請求耗時數(shù)據(jù)
if(timeOut != timeList[2]):
dataList.append(str(round(timeList[2],3))+'ms')
else:
dataList.append(str(timeList[2]))
if(timeOut != timeList[3]):
dataList.append(str(round(timeList[3],3))+'ms')
else:
dataList.append(str(timeList[3]))
if(timeOut != timeList[2] and timeOut != timeList[3]):
dxTimeLast= timeList[2]-timeList[3]
if(dxTimeLast < timeLimit):
dataList.append(timePass)
else:
dataList.append(timeFail)
else:
dataList.append(other)
return dataList
#定義寫文件title方法
def writeFileTitle():
with open('IPv6-IPv4TimeResult.csv', "a+") as file:
titleList = ['當(dāng)前時間','聯(lián)通IPv6耗時','聯(lián)通IPv4耗時','聯(lián)通耗時結(jié)果','電信IPv6耗時','電信IPv4耗時','電信耗時結(jié)果']
csv_file = csv.writer(file)
csv_file.writerow(titleList)
file.close()
#定義增量寫一行數(shù)據(jù)方法
def writeReq_time():
with open('IPv6-IPv4TimeResult.csv', "a+") as file:
begin_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print(begin_time + ' begin to request')
csv_file = csv.writer(file)
list = []
list.append(get_time())
datas = list
csv_file.writerows(datas)
end_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print(end_time + ' end to write')
file.close()
#主程序入口
#調(diào)用文件title
writeFileTitle()
#線程延遲時間1s執(zhí)行
timer_interval=1
#線程間隔3600s執(zhí)行
timer_sleep = 3600
t=Timer(timer_interval,writeReq_time)
t.start()
while True:
time.sleep(timer_sleep)
writeReq_time()
*******************************
程序執(zhí)行界面及結(jié)果如下:
程序運(yùn)行界面