#!/bin/python # -*- coding:utf-8 -*- import socket from concurrent.futures import ThreadPoolExecutor def dict(ip, port): dic = {'ip': ip, 'port': port} return dic def conn(obj): client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.settimeout(1) #设置socket连接超时时间(单位为秒) try: client.connect((obj.result()['ip'], obj.result()['port'])) except (TimeoutError, socket.timeout): with open('disconnect.txt', 'a', encoding='utf-8') as f: f.write('%s %s 连接失败 ' % (obj.result()['ip'], obj.result()['port'])) #linux系统中运行时把 换成 else: with open('connect.txt', 'a', encoding='utf-8') as f: f.write('%s %s 连接正常 ' % (obj.result()['ip'], obj.result()['port'])) #linux系统中运行时把 换成 client.close() if __name__ == '__main__': tread = ThreadPoolExecutor() ip = ['192.168.1.1', ] #多个IP写法['192.168.1.1','192.168.2.1'] port = [80,100] #填写端口范围,从80到100 for ip in ip: for port in range(port[0],int(port[1])+1): tread.submit(dict, ip, port).add_done_callback(conn) tread.shutdown()