#配置文件#端口检测配置[port_check_info]#检测ip地址或域名#address = baidu.com#address = 127.0.0.1address = www.anchnet.com,www.51cto.com,www.baidu.com#检查的端口,如多个端口使用,隔开,端口范围使用'-'#ports = 80,8080....ports = 20-25,80,443,1433,1521,3306,3389,6379,8080,27017#日志配置[loginfo]#日志目录logdir_name = logdir#日志文件名称logfile_name = check_port.log def _get_body(self): """ 获取address和port :return: list """ address_list = self.address_list.split(',') port_list = self.port_list.split(',') # 处理端口范围,返回range range_flag = False port_range = None content_List_range = [] for port in port_list: if '-' in port: range_flag = True port_range = range(int(port.split('-')[0]),int(port.split('-')[1])+1) port_list.remove(port) # 处理总体list for add in address_list: if range_flag: for port in port_range: content_List_range.append(add + ':' + str(port)) # 合并range和普通list content_List = [ add+':'+port for add in address_list for port in port_list ] content_List_range.extend(content_List) return content_List_range def run(self): """ 进行端口检测 :return: """ for content in self._get_body(): content_list = content.split(':') body = { 'host': content_list[0], 'port': content_list[1], 'encode': 'tlCHS1u3IgF4sC57m6KOP3Oaj1Y1kfLq' } try: response = requests.post(url=self.url,data=body,headers=self.headers) port_status = re.findall("msg:'(.*?)'", response.text) if len(port_status) > 0: # print('%s,port status is:%s' % (content, port_status)) self.logoper.info('%s,port status is:%s' % (content, port_status)) else: self.logoper.info('%s,port status is:%s' % (content, port_status)) # print('Occer error!请输入正确的地址和端口') except Exception as e: print(e)