<option id="mwy0y"><strong id="mwy0y"></strong></option>
  • <ul id="mwy0y"><sup id="mwy0y"></sup></ul>
  • <ul id="mwy0y"></ul>
  • <del id="mwy0y"><dfn id="mwy0y"></dfn></del><ul id="mwy0y"><sup id="mwy0y"></sup></ul>
  • <abbr id="mwy0y"></abbr>

    千鋒教育-做有情懷、有良心、有品質(zhì)的職業(yè)教育機構(gòu)

    400-811-9990
    手機站
    千鋒教育

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

    千鋒教育

    掃一掃進入千鋒手機站

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

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

    上海
    • 北京
    • 鄭州
    • 武漢
    • 成都
    • 西安
    • 沈陽
    • 廣州
    • 南京
    • 深圳
    • 大連
    • 青島
    • 杭州
    • 重慶
    當(dāng)前位置:長沙千鋒IT培訓(xùn)  >  技術(shù)干貨  >  Python測試遠程端口連接時間

    Python測試遠程端口連接時間

    來源:千鋒教育
    發(fā)布人:xqq
    時間: 2023-11-06 04:54:32

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

    思路

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

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

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

    解決

    importos

    importcsv

    importtime

    importargparse

    importtelnetlib

    fromdatetimeimportdatetime

    #測試遠程服務(wù)端口連接耗時

    #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="開始連接時間"

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

    differenceTimeTitle="連接總耗時"

    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)

    這里涉及到幾個Python的知識點:

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

    ●telnetlib的使用

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

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

    接下來一個一個談這些點:

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

    fromdatetimeimportdatetime

    starTime=datetime.now()

    endTime=datetime.now()

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

    Python3時間格式化

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

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

    starTimeView=starTime.strftime(timeFormat)

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

    Python3計算時間差

    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)建一個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)

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

    使用腳本

    python3windows_telnet.py192.168.10.2180

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

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

    聲明:本站稿件版權(quán)均屬千鋒教育所有,未經(jīng)許可不得擅自轉(zhuǎn)載。

    猜你喜歡LIKE

    python實現(xiàn)WSGI的框架

    2023-11-13

    python打開文本文件有哪些方法?

    2023-11-13

    python使用loguru操作日志

    2023-11-13

    最新文章NEW

    python-=是什么意思

    2023-11-13

    pythonre是什么?

    2023-11-13

    python列表追加元素出錯的解決

    2023-11-13

    相關(guān)推薦HOT

    更多>>

    快速通道 更多>>

    最新開班信息 更多>>

    網(wǎng)友熱搜 更多>>