可以测试到某个服务器的某个端口的连通性。
#!/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)
推荐python3运行。