1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| #!/usr/bin/env python #-*-coding:GBK -*-
import socket,sys,time,os,signal from ping3 import ping result = [] def Seeyon_write(result): filename = 'result.txt' with open(filename, 'a+') as file_object: file_object.write(str(time.asctime( time.localtime(time.time()) ))+result) file_object.close() def Seeyon_Con(): Seeyon_sk=socket.socket(socket.AF_INET,socket.SOCK_STREAM) Seeyon_Result=Seeyon_sk.connect_ex(('59.197.224.47',80)) if Seeyon_Result == 0: Seeyon_write(' Port is Open! \n') print(str(time.asctime( time.localtime(time.time()) ))+' Port is Open!') result.append('1') else: Seeyon_write(' Port is Not Open! \n') print(str(time.asctime( time.localtime(time.time()) ))+' Port is Open!') result.append('2') def Seeyon_exit(signum, frame): Seeyon_write('成功:'+str(result.count('1'))+' '+'失败:'+str(result.count('2')) +'\n') print(' 成功:'+str(result.count('1'))+' '+'失败:'+str(result.count('2'))) sys.exit(0) def Seeyon_ping(ip_address): response = ping(ip_address) if response is not None: delay = int(response * 1000) Seeyon_write('连接延时:'+str(delay)+'\n') print ('连接延时:'+str(delay)+'\n') if __name__ == '__main__': print ('\n开始测试本机到59.197.224.47的80端口的连通性:\n') signal.signal(signal.SIGINT, Seeyon_exit) signal.signal(signal.SIGTERM, Seeyon_exit) while True: Seeyon_Con() Seeyon_ping('59.197.224.47') time.sleep(3)
|