有很多ip和端口,需要用类似与telnet的方式检测一下端口的连通性
import socket
import os
file_obj = open('ip.txt')
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
for line in file_obj:
ip = line.split()[0]
p = int(line.split()[1])
try:
s.connect((ip,p))
s.close()
print ip+":"+str(p)+" open"
except:
print ip+":"+str(p)+" close"
file_obj.close()
import socket
import os
file_obj = open('ip.txt')
for line in file_obj:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip = line.split()[0]
p = int(line.split()[1])
try:
s.connect((ip,p))
s.close()
print ip+":"+str(p)+" open"
except:
print ip+":"+str(p)+" close"
file_obj.close()