国产睡熟迷奷白丝护士系列精品,中文色字幕网站,免费h网站在线观看的,亚洲开心激情在线

      <sup id="hb9fh"></sup>
          1. 千鋒教育-做有情懷、有良心、有品質(zhì)的職業(yè)教育機(jī)構(gòu)

            手機(jī)站
            千鋒教育

            千鋒學(xué)習(xí)站 | 隨時(shí)隨地免費(fèi)學(xué)

            千鋒教育

            掃一掃進(jìn)入千鋒手機(jī)站

            領(lǐng)取全套視頻
            千鋒教育

            關(guān)注千鋒學(xué)習(xí)站小程序
            隨時(shí)隨地免費(fèi)學(xué)習(xí)課程

            當(dāng)前位置:首頁  >  技術(shù)干貨  > Python測試遠(yuǎn)程端口連接時(shí)間

            Python測試遠(yuǎn)程端口連接時(shí)間

            來源:千鋒教育
            發(fā)布人:xqq
            時(shí)間: 2023-11-07 08:45:57 1699317957

            最近自己服務(wù)器訪問別人的服務(wù)器,有時(shí)候會報(bào)超時(shí)錯(cuò)誤,有時(shí)候又能夠正常訪問別人服務(wù)器。

            思路

            最開始猜測是網(wǎng)絡(luò)不穩(wěn)定造成的,但是自己沒有收集什么時(shí)候超時(shí),什么時(shí)候能正常訪問別人服務(wù)器的日志,搞網(wǎng)絡(luò)運(yùn)維的同學(xué)根本不鳥我(其實(shí),這活本來就是運(yùn)維的事,有點(diǎn)小心塞,不過想起蜘蛛俠的名言)。

            能力越大,責(zé)任就越大

            寫個(gè)python腳本,然后,在python腳本里面使用telnet去連接別人服務(wù)器對應(yīng)的端口,然后,計(jì)算連接前后的時(shí)間長短。

            解決

            importos

            importcsv

            importtime

            importargparse

            importtelnetlib

            fromdatetimeimportdatetime

            #測試遠(yuǎn)程服務(wù)端口連接耗時(shí)

            #python3windows_telnet.py192.168.10.2180

            parser=argparse.ArgumentParser()

            parser.add_argument("ip",type=str,help="ip")

            parser.add_argument("port",type=str,help="port")

            args=parser.parse_args()

            timeFormat="%Y-%m-%d%H:%M:%S.%f"

            starTimeTitle="開始連接時(shí)間"

            endTimeTitle="結(jié)束連接時(shí)間"

            differenceTimeTitle="連接總耗時(shí)"

            whileTrue:

            starTime=datetime.now()

            starTimeView=starTime.strftime(timeFormat)

            print("開始連接:{0}".format(starTimeView))

            tn=telnetlib.Telnet(args.ip,args.port)

            endTime=datetime.now()

            endTimeView=endTime.strftime(timeFormat)

            print("連接完成:{0}".format(endTimeView))

            tn.close()

            print("連接結(jié)束")

            differenceTime=endTime-starTime

            print("連接消耗:{0}".format(differenceTime))

            nowTime=datetime.now()

            csvFileName="{0}.csv".format(nowTime.strftime("%Y-%m-%d"))

            ifos.path.exists(csvFileName)isnotTrue:

            withopen(csvFileName,"w",newline="")ascsvfile:

            fieldnames=[starTimeTitle,endTimeTitle,differenceTimeTitle]

            writer=csv.DictWriter(csvfile,fieldnames=fieldnames)

            writer.writeheader()

            withopen(csvFileName,"a",newline="")ascsvfile:

            fieldnames=[starTimeTitle,endTimeTitle,differenceTimeTitle]

            writer=csv.DictWriter(csvfile,fieldnames=fieldnames)

            writer.writerow({starTimeTitle:starTimeView,endTimeTitle:endTimeView,differenceTimeTitle:differenceTime})

            time.sleep(0.2)

            這里涉及到幾個(gè)Python的知識點(diǎn):

            ●獲取當(dāng)前時(shí)間,計(jì)算時(shí)間差以及時(shí)間格式化

            ●telnetlib的使用

            ●生成csv文件以及對文件讀寫

            ●在whileTrue這個(gè)死循環(huán)里面需要避免cpu飆到100%問題,則需要在最后一行添加time.sleep(0.2)

            接下來一個(gè)一個(gè)談這些點(diǎn):

            Python3獲取當(dāng)前時(shí)間

            fromdatetimeimportdatetime

            starTime=datetime.now()

            endTime=datetime.now()

            這樣獲取出來的時(shí)間,我們一般需要在進(jìn)行格式化處理才能夠展現(xiàn)給用戶看。

            Python3時(shí)間格式化

            在上面的基礎(chǔ)上,我們可以,這樣做

            timeFormat="%Y-%m-%d%H:%M:%S.%f"

            starTimeView=starTime.strftime(timeFormat)

            使用strftime方法處理,具體可以查看Python3文檔的date.strftime(format)部分。

            Python3計(jì)算時(shí)間差

            differenceTime=endTime-starTime

            對,就這樣相減,就完事了。

            telnetlib的使用

            importtelnetlib

            tn=telnetlib.Telnet("192.168.10.21","80")

            csv文件創(chuàng)建

            importos

            importcsv

            csvFileName="{0}.csv".format(nowTime.strftime("%Y-%m-%d"))

            ifos.path.exists(csvFileName)isnotTrue:

            withopen(csvFileName,"w",newline="")ascsvfile:

            fieldnames=[starTimeTitle,endTimeTitle,differenceTimeTitle]

            writer=csv.DictWriter(csvfile,fieldnames=fieldnames)

            writer.writeheader()

            這里是先判斷文件是否存在,如果不存在,就創(chuàng)建一個(gè)csv文件,并且寫好表頭。

            csv文件追加

            withopen(csvFileName,"a",newline="")ascsvfile:

            fieldnames=[starTimeTitle,endTimeTitle,differenceTimeTitle]

            writer=csv.DictWriter(csvfile,fieldnames=fieldnames)

            writer.writerow({starTimeTitle:starTimeView,endTimeTitle:endTimeView,differenceTimeTitle:differenceTime})

            死循環(huán)避免CPU飚高

            循環(huán)里面最后添加一行:

            importtime

            time.sleep(0.2)

            讓線程休眠一段時(shí)間,這樣就避免死循環(huán)占用cpu太高。

            使用腳本

            python3windows_telnet.py192.168.10.2180

            以后就可以通過這個(gè)腳本監(jiān)測遠(yuǎn)程端口連接問題,并每天生成一個(gè)日志文件。

            以上內(nèi)容為大家介紹了python中的反斜杠,希望對大家有所幫助,如果想要了解更多Python相關(guān)知識,請關(guān)注IT培訓(xùn)機(jī)構(gòu):千鋒教育。http://www.parentadvocate.org/

            聲明:本站稿件版權(quán)均屬千鋒教育所有,未經(jīng)許可不得擅自轉(zhuǎn)載。
            10年以上業(yè)內(nèi)強(qiáng)師集結(jié),手把手帶你蛻變精英
            請您保持通訊暢通,專屬學(xué)習(xí)老師24小時(shí)內(nèi)將與您1V1溝通
            免費(fèi)領(lǐng)取
            今日已有369人領(lǐng)取成功
            劉同學(xué) 138****2860 剛剛成功領(lǐng)取
            王同學(xué) 131****2015 剛剛成功領(lǐng)取
            張同學(xué) 133****4652 剛剛成功領(lǐng)取
            李同學(xué) 135****8607 剛剛成功領(lǐng)取
            楊同學(xué) 132****5667 剛剛成功領(lǐng)取
            岳同學(xué) 134****6652 剛剛成功領(lǐng)取
            梁同學(xué) 157****2950 剛剛成功領(lǐng)取
            劉同學(xué) 189****1015 剛剛成功領(lǐng)取
            張同學(xué) 155****4678 剛剛成功領(lǐng)取
            鄒同學(xué) 139****2907 剛剛成功領(lǐng)取
            董同學(xué) 138****2867 剛剛成功領(lǐng)取
            周同學(xué) 136****3602 剛剛成功領(lǐng)取
            相關(guān)推薦HOT