如下代码,按照我自己的理解会把ip.list中所有的IP地址取出来,假设是100个,判断22端口是否开放,一共需要5秒钟就可以判断出这100个IP的状况。然而事实是代码仍然是阻塞的,在22端口不通的情况下,需要5秒后超时才能进行对下一个IP的判断,这里不理解,请大侠不吝指导,非常感谢!
import socket socket.setdefaulttimeout(5) def ping(ipp): while True: ipport = yield try: conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) conn.connect(ipport) conn.close() except Exception as e: print ipp,e else: print ipp,'OK' def run(): a = [] with open('ip.list','r') as f: for i in f.readlines(): ip = i.split()[0] port = 22 a.append((ip,port)) for i in a: task = ping(i) task.next() task.send(i) run()
繁花不似锦
慕神8447489