程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[python]python test network connectivity

編輯:Python

The network is disconnected for unknown reasons in the actual project , For precise disconnection time and reason , Write a script to record the disconnection time

among

ping -c 1 192.168.1.252

-c 1 Indicates a total of one execution

another -i 5 Indicates the interval 5 Seconds to send

import os
import datetime
import time
def network_test():
count = 0
while(True):
ret = os.system('ping -c 1 192.168.1.252')
if ret == 0:
time_re = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print("OK,%s" % time_re)
#return True
else:
time_re = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
with open('./log.txt','a') as f:
f.write('time:%s\n'%time_re)
print(time_re)
count += 1
if count > 20:
break
#return False
time.sleep(2)
int2 = 10
if __name__ == '__main__':
network_test()


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved