[Python] python循环监控远程端口的代码 →→→→→进入此内容的聊天室

来自 , 2020-04-03, 写在 Python, 查看 116 次.
URL http://www.code666.cn/view/6e839dd9
  1. #!/usr/bin/env python
  2. # -*- coding: gbk -*-
  3. import socket,time
  4. while 1:
  5.     file_obj = open('ip.txt')
  6.     for line in file_obj:
  7.         try:
  8.             sc=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  9.             ip = line.split()[0]
  10.             port = int(line.split()[1])
  11.             print ip,port
  12.             #设置超时时间(0.0)
  13.             sc.settimeout(2)
  14.             sc.connect((ip,port))
  15.             timenow=time.localtime()
  16.             datenow = time.strftime('%Y-%m-%d %H:%M:%S', timenow)
  17.             logstr="%s:%s 连接成功->%s \n" %(ip,port,datenow)
  18.             print logstr
  19.             sc.close()
  20.         except:
  21.             file = open("log.txt", "a")
  22.             timenow=time.localtime()
  23.             datenow = time.strftime('%Y-%m-%d %H:%M:%S', timenow)
  24.             logstr="%s:%s 连接失败->%s \n" %(ip,port,datenow)
  25.             print logstr
  26.             file.write(logstr)
  27.             file.close()
  28.     print "sleep 10....."
  29.     time.sleep(10)
  30.  
  31.  
  32. #//python/8925

回复 "python循环监控远程端口的代码"

这儿你可以回复上面这条便签

captcha