port

# -*- coding: UTF-8 -* ############################################################################### # Copyright (C), 2021, TP-Link Technofies Co., Ltd. # # filename: Switch_PowerCycle.py # version: 1.0.0 # first create: wangchangmao@tp-link.com.cn # history: # 2021-8-23 | First created. # 2025-4-29 | Modified by wangzhixiang1@tp-link.com.hk # 2025-7-15 | Modified by lishangying@tp-link.com.hk ############################################################################### import sys import time import random import serial import thread import telnetlib import re import os import logging import PacketCreate import PyG4800Ctrl import PyXG800Ctrl import powerswitch_ctrl import GWSChamberCtrl import Gateway_basic import my_ping import PpelCtrl import paperless_recorder start_time = time.time() f = open('./power_cycle_config.ini', 'r') lines = f.readlines() for line in lines: if 'xg800_ip' in line: xg800_ip = [] xg800_ip_str = line.split('=')[1].strip('\n').strip('[').strip(']') xg800_ip_list = xg800_ip_str.split(',') for xg_temp in xg800_ip_list: xg800_ip.append(xg_temp.strip("'").strip("'")) elif 'xg800_port' in line and 'xg800_port_num' not in line: xg800_port = [] xg800_port_str = line.split('=')[1].strip('\n').lstrip('[').rstrip(']') xg800_port_list = xg800_port_str.split('],[') for xg_temp in xg800_port_list: xg_temp_list = [] xg800_port_list_str = xg_temp.split(',') for temp in xg800_port_list_str: xg_temp_list.append(int(temp)) xg800_port.append(xg_temp_list) elif 'xg800_port_num' in line: zika = [] zika_str = line.split('=')[1].strip('\n').strip('[').strip(']') zika_list = zika_str.split(',') for xg_temp in zika_list: zika.append(int(xg_temp.strip("'").strip("'"))) elif 'packet_speed' in line: packet_speed=[] packet_speed_str = line.split('=')[1].strip('\n').strip('\n').lstrip('[').rstrip(']') packet_speed_list = packet_speed_str.split(',') for packet_temp in packet_speed_list: packet_speed.append(int(packet_temp)) elif 'flow_ctrl' in line: flow_ctrl=[] flow_ctrl_str = line.split('=')[1].strip('\n').strip('\n').lstrip('[').rstrip(']') flow_ctrl_list = flow_ctrl_str.split(',') for flow_temp in flow_ctrl_list: flow_ctrl.append(int(flow_temp)) elif 'by_pass' in line: by_pass=[] by_pass_str = line.split('=')[1].strip('\n').strip('\n').lstrip('[').rstrip(']') by_pass_list = by_pass_str.split(',') for by_pass_temp in by_pass_list: by_pass.append(int(by_pass_temp)) elif 'dut_ip' in line: dut_ip = {} dut_ip_str = line.split('=')[1].strip('\n').strip('\n').lstrip('{').rstrip('}') if len(dut_ip_str) != 0: dut_ip_list = dut_ip_str.split(',') for dut_ip_temp in dut_ip_list: dut_ip_dict = dut_ip_temp.split(':') if dut_ip_dict[1] != "''" and len(dut_ip_dict[1]) != 0: dut_ip[int(dut_ip_dict[0])]=dut_ip_dict[1].lstrip("'").rstrip("'") elif 'poe_port_list' in line: poe_port_list=[] ppel_num_list=[] poe_port_list_str = line.split('=')[1].strip('\n').strip('\n').lstrip('[').rstrip(']') if len(poe_port_list_str) != 0: poe_port_list_list_temp = poe_port_list_str.split(',') for poe_port_temp in poe_port_list_list_temp: poe_port_list_list = poe_port_temp.split(':') poe_port_list.append(poe_port_list_list[1].lstrip("'").rstrip("'")) ppel_num_list.append(int(poe_port_list_list[0].lstrip("'").rstrip("'"))) elif 'poe_port_power_list' in line: poe_port_power_list=[] poe_port_power_list_str = line.split('=')[1].strip('\n').strip('\n').lstrip('[').rstrip(']') if len(poe_port_power_list_str) != 0: poe_port_power_list_list = poe_port_power_list_str.split(',') for poe_port_temp in poe_port_power_list_list: poe_port_power_list.append(poe_port_temp.lstrip("'").rstrip("'")) elif 'ppel_port' in line: ppel_port=[] ppel_port_str = line.split('=')[1].strip('\n').strip('\n').strip('[').strip(']') if len(ppel_port_str) != 0 : ppel_port_list = ppel_port_str.split(',') for ppel_port_temp in ppel_port_list: ppel_port.append(ppel_port_temp.lstrip("'").rstrip("'")) elif 'serial_ip' in line: serial_ip = line.split('=')[1].strip('\n').strip('\n').lstrip("'").rstrip("'") elif 'boot_time' in line: boot_time = int(line.split('=')[1].strip('\n').strip('\n')) elif 'forwarding_test_times' in line: test_times = int(line.split('=')[1].strip('\n').strip('\n')) elif 'port_channel' in line: port_channel = int(line.split('=')[1].strip('\n').strip('\n')) elif 'packet_num' in line: packet_num = int(line.split('=')[1].strip('\n').strip('\n')) elif 'packet_time_forwarding' in line: packet_time = int(line.split('=')[1].strip('\n').strip('\n')) elif 'discharge_time' in line: discharge_time = int(line.split('=')[1].strip('\n').strip('\n')) elif 'power_port' in line: power_port_temp = line.split('=')[1].strip('\n').strip('\n') if re.search(r'\D+',power_port_temp) != None: power_port = power_port_temp.upper() else: power_port = int(power_port_temp) elif 'dut_name' in line : dut_name='' dut_name=line.split('=')[1].strip('\n').strip('\n').replace(' ','') elif 'cold_time' in line : cold_time='' cold_time = int(line.split('=')[1].strip('\n').strip('\n')) elif 'test_stop_loss' in line : test_stop_loss='' test_stop_loss = int(line.split('=')[1].strip('\n').strip('\n')) elif 'resend_packet_times' in line : resend_packet_times='' resend_packet_times = int(line.split('=')[1].strip('\n').strip('\n')) elif 'stop_loss_rate' in line : stop_loss_rate='' stop_loss_rate = float(line.split('=')[1].strip('\n').strip('\n')) elif 'test_stop_poe' in line : test_stop_poe='' test_stop_poe = int(line.split('=')[1].strip('\n').strip('\n')) elif 'chamber_high_temp_params' in line: config_value = line.split('=')[1].strip('\n').strip() if config_value.startswith('[') and config_value.endswith(']'): config_value = config_value[1:-1] if not config_value: target_temperature, target_humidity = None, None else: temp_str, humidity_str = config_value.split(',') target_temperature = float(temp_str.strip()) target_humidity = float(humidity_str.strip()) elif 'chamber_serial_port' in line: chamber_serial_port = line.split('=')[1].strip().strip("'").strip('"') if len(ppel_port) != 0: for num,ppel_port_temp in enumerate(ppel_port): ret=re.search('\D+',ppel_port_temp) if ret: continue else: ppel_port[num] = serial_ip + ':' + ppel_port_temp f.close() script_dir = os.path.dirname(os.path.abspath(__file__)) detailed_log_dir = os.path.join(script_dir, 'XG800_forwarding_log_detailed') result_log_dir = os.path.join(script_dir, 'XG800_forwarding_log_result') for log_dir in [detailed_log_dir, result_log_dir]: if not os.path.exists(log_dir): os.makedirs(log_dir) timestamp = time.strftime("%Y%m%d_%H%M%S") detailed_log_filename = '%s_xg800_forwarding_stability_high_temperature_detailed_log_%s.log' % (dut_name, timestamp) result_log_filename = '%s_xg800_forwarding_stability_high_temperature_result_%s.log' % (dut_name, timestamp) logger = logging.getLogger('xg800_forwarding_stability_test') logger.setLevel(logging.DEBUG) detailed_handler = logging.FileHandler(os.path.join(detailed_log_dir, detailed_log_filename), mode='w') detailed_handler.setLevel(logging.DEBUG) detailed_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') detailed_handler.setFormatter(detailed_formatter) result_handler = logging.FileHandler(os.path.join(result_log_dir, result_log_filename), mode='w') result_handler.setLevel(logging.INFO) result_formatter = logging.Formatter('%(asctime)s - %(message)s') result_handler.setFormatter(result_formatter) console_handler = logging.StreamHandler() console_handler.setLevel(logging.INFO) console_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') console_handler.setFormatter(console_formatter) logger.addHandler(detailed_handler) logger.addHandler(result_handler) logger.addHandler(console_handler) def log_info(msg): logger.info(msg) def log_debug(msg): logger.debug(msg) def log_error(msg): logger.error(msg) Pass_num = 0 Fail_num = 0 Total_Pass = 0 Total_Fail = 0 Failed_tests = [] class Xg800_Ctrl(): def __init__(self, ip_addr_list): self.g4800_config = self self.xg800 = [] self.pkt = {} self.packet_num = 0 self.port_ip = {} self.port_mac = {} self.packet = PacketCreate.UDP() for ip_addr in ip_addr_list: self.xg800.append(PyXG800Ctrl.XG800CMD(ip_addr)) self.port_speed = {} def get_port_speed(self, port_id): return self.xg800[(port_id-1)/8].get_port_ethernet_speed((port_id-1)%8+1)[1] def get_group_status(self, group_id): result = [] for num,xg800 in enumerate(self.xg800): for port in self.new_port_list[num]: temp_result = {'Packets Rx':0, 'testResult':0, 'ConnCounter':0, 'testStatus':0, 'MaxLiveCounter':0, 'Packets Tx':0, 'Bytes Rx':0, 'LiveCounter':0, 'ErrorCounter':0, 'Bytes Tx':0} ret = xg800.get_port_count(port)[1] temp_result['Packets Rx'] = ret[port]['RxPackets'] temp_result['Packets Tx'] = ret[port]['TxPackets'] temp_result['Bytes Rx'] = ret[port]['RxBytes'] temp_result['Bytes Tx'] = ret[port]['TxBytes'] temp_result['ErrorCounter'] = ret[port]['BadPacketsRx'] result.append({ret.keys()[0]+num*8:temp_result}) return result def reset_port_maccounter(self, port_id): self.xg800[(port_id-1)/8].clear_port_count((port_id-1)%8+1) time.sleep(0.1) if port_id in self.port_speed.keys(): self.xg800[(port_id-1)/8].set_packet_config_num((port_id-1)%8+1, self.port_speed[port_id], self.packet_num, self.pkt[port_id]) else: self.xg800[(port_id-1)/8].set_packet_config_num((port_id-1)%8+1, 1000000000, self.packet_num, self.pkt[port_id]) def set_group_port(self, group_id, port_list): self.new_port_list = {} for port in port_list: key = (port-1)/8 if key not in self.new_port_list.keys(): self.new_port_list[key] = [] self.new_port_list[key].append((port-1)%8+1) def set_group_start(self, group_id): for num, xg800 in enumerate(self.xg800): xg800.set_group_start(self.new_port_list[num]) def set_group_stop(self, group_id): for num,xg800 in enumerate(self.xg800): xg800.set_group_stop(self.new_port_list[num]) def set_port_config(self, port_id, port_ip, port_mac, port_gateway, port_mask, port_speedmode='AUTO',flow_control='AUTO'): port_ip_list = port_ip.split('.') self.port_ip[port_id] = int(''.join([hex(int(i)).replace('x','')[-2:] for i in port_ip_list]), 16) self.port_mac[port_id] = int(port_mac, 16) self.xg800[(port_id-1)/8].set_port_ethernet_speed((port_id-1)%8+1, port_speedmode) self.xg800[(port_id-1)/8].set_virtual_port_ip((port_id-1)%8+1, self.port_ip[port_id]) self.xg800[(port_id-1)/8].set_virtual_port_mac((port_id-1)%8+1, self.port_mac[port_id]) self.xg800[(port_id-1)/8].set_port_flow_control((port_id-1)%8+1, flow_control) def set_port_nat(self, port_id, port_ip): self.xg800[(port_id-1)/8].set_port_nat((port_id-1)%8+1, port_ip) def set_port_nat_disabled(self, port_id): self.xg800[(port_id-1)/8].set_port_nat_disabled((port_id-1)%8+1) def set_port_udp_config(self, port_id, port_udpmode, port_udplocalport, udp_dstip, udp_dstport, packet_len, packet_num, packet_speed): self.packet_num = packet_num dstip_list = udp_dstip.split('.') dst_ip = int(''.join([hex(int(i)).replace('x','')[-2:] for i in dstip_list]), 16) dst_port = list(self.port_ip.keys())[list(self.port_ip.values()).index(dst_ip)] self.pkt[port_id] = self.packet.udp_packet(self.port_ip[port_id], port_udplocalport, dst_ip, udp_dstport, self.port_mac[port_id], self.port_mac[dst_port], packet_len) self.xg800[(port_id-1)/8].set_packet_config_num((port_id-1)%8+1, int(packet_speed)*8000, packet_num, self.pkt[port_id]) self.port_speed[port_id] = int(packet_speed)*8000 result = 'Set Port %s Speed %sbps, Packet_num %s, DstPort %s' % (port_id, packet_speed*8000, packet_num, dst_port) log_debug(result) return result def get_ip_list(ip_str): result = [] ip_str = ip_str.split('-') if len(ip_str) == 2: ip_start = ip_str[0].split('.') ip_addr = '.'.join(ip_start[:-1]) for i in range(int(ip_start[-1]), int(ip_str[-1])+1): result.append('%s.%s' % (ip_addr, i)) else: result = ip_str return result def run_aging_chamber(): if target_temperature is None and target_humidity is None: return try: # Initialize chamber with serial port chamber = GWSChamberCtrl.GWSChamberCtrl(port=chamber_serial_port) # Get current temperature and humidity current_temperature_data = chamber.get_temperature() current_humidity_data = chamber.get_humidity() current_temperature = float(chamber.get_current_temperature(current_temperature_data)) current_humidity = float(chamber.get_current_humidity(current_humidity_data)) log_debug("Current Temperature: {0}".format(current_temperature)) log_debug("Current Humidity: {0}".format(current_humidity)) is_temp_within_range = abs(target_temperature - current_temperature) <= 3 is_humidity_within_range = abs(target_humidity - current_humidity) <= 3 # Check if it is possible to inherit temperature and humidity if is_temp_within_range and is_humidity_within_range: program_steps = [ GWSChamberCtrl.Program(temperature=target_temperature, humidity=target_humidity, hour=0,minute=5), GWSChamberCtrl.Program(temperature=target_temperature, humidity=target_humidity, hour=99, minute=0) ] chamber_stabilize_wait_min = 5 else: # Fixed humidity ramp duration humi_ramp_duration_min = 30 # calculate time to reach target temperature and humidity temp_ramp_duration_min = int(max(abs(target_temperature - current_temperature), 10)) temp_ramp_hours = temp_ramp_duration_min // 60 temp_ramp_minutes = temp_ramp_duration_min % 60 log_debug("Temperature ramp duration: {0} minutes".format(temp_ramp_duration_min)) log_debug("Time to reach target temperature: {0} hours and {1} minutes".format(temp_ramp_hours, temp_ramp_minutes)) log_debug("Time to reach target humidity: {0} minutes".format(humi_ramp_duration_min)) program_steps = [ GWSChamberCtrl.Program(temperature=target_temperature, humidity=0, hour=temp_ramp_hours,minute=temp_ramp_minutes), GWSChamberCtrl.Program(temperature=target_temperature, humidity=target_humidity, hour=0,minute=humi_ramp_duration_min), GWSChamberCtrl.Program(temperature=target_temperature, humidity=target_humidity, hour=99, minute=0) ] chamber_stabilize_wait_min = temp_ramp_duration_min + humi_ramp_duration_min # Configure and run the chamber program if chamber.SetProgram(program_steps): chamber.RunProgram() log_debug("Waiting for the chamber to reach the target conditions... Estimated time: {0} minutes".format(chamber_stabilize_wait_min)) time.sleep(chamber_stabilize_wait_min * 60) log_debug("Chamber has reached the target temperature and humidity. Continuing with the next steps...") except Exception as e: log_error(str(e)) raise e def switch_test(): try: if ppel_port: ppel = [] for ppel_port_temp in ppel_port: ppel.append(PpelCtrl.PpelCtrl(ppel_port_temp)) # Init xg800 if zika != []: xg800 = Gateway_basic.Gateway_basic(xg800_ip,zika) else: xg800 = Gateway_basic.Gateway_basic(xg800_ip) port_list = xg800_port xg800.set_lan_port(port_list) log_debug('Port List: %s' % str(port_list)) # Set xg800 Ports' IP and MAC log_debug('Set xg800 Ports IP and MAC') for num,port_list_temp in enumerate(port_list): gateway = '192.168.2.1' ip_format = '192.168.2.' port_speedmode='AUTO' #10000M:1250000---5000M:625000---2500M:312500---1000M:125000---100M:12500---10M:1250 if packet_speed[num] == 1250000: port_speedmode = '10000MF' if packet_speed[num] == 625000: port_speedmode = '5000MF' if packet_speed[num] == 312500: port_speedmode = '2500MF' if packet_speed[num] == 125000 and by_pass[num] == 1: port_speedmode = '1000MF' if packet_speed[num] == 125000 and by_pass[num] == 0: port_speedmode = 'AUTO' if packet_speed[num] == 12500: port_speedmode = '100MF' if packet_speed[num] == 1250: port_speedmode = '10MF' if flow_ctrl[num] == 1: flow_control = 'ON' if flow_ctrl[num] == 0: flow_control = 'OFF' for port_id in port_list_temp: xg800.set_port_config(port_id = port_id, port_ip = ip_format + '%02d'%(port_id+100), port_mac = '0x0000000000' + '%02d'%(port_id), port_gateway = gateway, port_mask = '255.255.255.0', port_speedmode = port_speedmode, flow_control =flow_control) time.sleep(5) speed_index = 0 for port_list_temp in port_list: len_port_list = len(port_list_temp) for num,port_id in enumerate(port_list_temp): ########################################loop mode 1-->2-->3################################################### if num != len_port_list-1: ret=xg800.set_port_udp_config(port_id=port_id, port_udpmode='txmode', port_udplocalport=8000, udp_dstip='192.168.2.' + '%02d'%(port_list_temp[num+1]+100), udp_dstport=8000, packet_len=1500,#1500 packet_num=packet_num, packet_speed=packet_speed[speed_index]) else: ret=xg800.set_port_udp_config(port_id=port_id, port_udpmode='txmode', port_udplocalport=8000, udp_dstip='192.168.2.' + '%02d'%(port_list_temp[0]+100), udp_dstport=8000, packet_len=1500, packet_num=packet_num, packet_speed=packet_speed[speed_index]) log_debug('%s \r\n' % ret) speed_index += 1 except Exception as e: log_error(str(e)) raise # Set a group for G4800 ports port_all_list = [] for port_list_temp in port_list: port_all_list += port_list_temp xg800.set_group_port(1, port_all_list) #------------------------------------------------------------------ # Test Start #------------------------------------------------------------------ # Start set poe if ppel_port: log_debug('%s [Start Set Poe]\n' % time.strftime('%Y-%m-%d %H:%M:%S')) for num, index_poe_port in enumerate(poe_port_list): ret = ppel[ppel_num_list[num] - 1].get('power', index_poe_port) log_debug('%s : %s\r\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), ret)) time.sleep(1) ppel[ppel_num_list[num] - 1].set('power', index_poe_port, poe_port_power_list[num]) time.sleep(1) ret = ppel[ppel_num_list[num] - 1].get('power', index_poe_port) log_debug('%s : %s\r\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), ret)) time.sleep(5) # switch function test for cycle_num in range(1, test_times+1): global Pass_num,Fail_num,Total_Fail,Total_Pass log_info("------------------------------------------------------------------") log_info('[Test %s]: powercycle test start...' % cycle_num) log_debug('No.%s test starts......' % cycle_num) test_mark = [False] * len(port_all_list) # Mac address Learning xg800.set_group_start(group_id = 1) time.sleep(5) xg800.set_group_stop(group_id = 1) time.sleep(1) Group_Status_temp=xg800.get_group_status(1) Group_Status=xg800.get_x_to_y(port_all_list,Group_Status_temp) for num, port_id in enumerate(port_all_list): log_debug('%s [Test %s]: Port %d Tx Packets are %d, Rx Packets are %d.\r\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), cycle_num, port_id, Group_Status[num][port_id]['Packets Tx'],Group_Status[num][port_id]['Packets Rx'])) # Clear ports' counter log_debug('clear counter') for port_id in port_all_list: xg800.reset_port_maccounter(port_id = port_id) time.sleep(5) Group_Status_temp=xg800.get_group_status(1) Group_Status=xg800.get_x_to_y(port_all_list,Group_Status_temp) for num, port_id in enumerate(port_all_list): log_debug('%s [Test %s]: Port %d Tx Packets are %d, Rx Packets are %d.\r\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), cycle_num, port_id, Group_Status[num][port_id]['Packets Tx'], Group_Status[num][port_id]['Packets Rx'])) # Send packets For Switching test log_debug('%s [Test %s]: xg800 send packets to check Lan to Lan function\r\n' % (time.strftime('%Y-%m-%d %H:%M:%S'),cycle_num)) log_debug('start send pkts') xg800.set_group_start(group_id = 1) # Start check poe if ppel_port: log_debug('Start PoE Check') log_debug('%s [Start PoE Check]\n' % time.strftime('%Y-%m-%d %H:%M:%S')) ret_temp = {} ret_power_mul = {} ret_power_sum = 0 poe_port_list_len = len(poe_port_list) for index_poe_port in range(0, poe_port_list_len): ret_power_sum = 0 try: ret = ppel[ppel_num_list[index_poe_port] - 1].get('power', poe_port_list[index_poe_port]) except Exception as e: log_error(str(e)) ret = ppel[ppel_num_list[index_poe_port] - 1].get('power', poe_port_list[index_poe_port]) log_debug(ret) ret_temp.update(ret) if 0 in ret.values(): log_info('%s [Test %s]: Test failed because of PoE Down!' % (time.strftime('%Y-%m-%d %H:%M:%S'),cycle_num)) break else: log_debug('%s [Test %s]: %s' % (time.strftime('%Y-%m-%d %H:%M:%S'), cycle_num, ret)) log_debug('ret_temp = %s\n' % ret_temp) poe_mark_temp = [False] * (len(poe_port_list)) for num, poe_port_list_temp in enumerate(poe_port_list): poe_port_arry = [] poe_port_list_str = poe_port_list_temp.split('-') if len(poe_port_list_str) > 1: for index_str in range(int(poe_port_list_str[0]), int(poe_port_list_str[len(poe_port_list_str) - 1]) + 1): poe_port_arry.append(int(index_str)) else: poe_port_arry.append(int(poe_port_list_str[0])) poe_port_arry_len = len(poe_port_arry) + 1 poe_mark = [False] * (poe_port_arry_len - 1) for i in range(1, poe_port_arry_len): if ret_temp.has_key(poe_port_arry[i - 1]): if float(ret_temp[poe_port_arry[i - 1]]) >= (float(poe_port_power_list[num]) - 0.2): poe_mark[i - 1] = True if False not in poe_mark: poe_mark_temp[num] = True if False not in poe_mark_temp: log_info('[Test %s]: PoE pass' % cycle_num) else: if Pass_num > 0: Total_Pass = Total_Pass - 1 if Fail_num == 0: Total_Fail = Total_Fail + 1 Failed_tests.append(cycle_num) log_info('[Test %s]: PoE fail' % cycle_num) if test_stop_poe == 0: pass elif test_stop_poe == 1: os.system('pause') else: poe_mark_temp = [True] * (len(poe_port_list)) time.sleep(packet_time) log_debug('stop send pkts') xg800.set_group_stop(group_id = 1) time.sleep(1) # Check packets receive log_debug('%s [Test %s]: Check xg800...\r\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), cycle_num)) Group_Status_temp=xg800.get_group_status(1) Group_Status=xg800.get_x_to_y(port_all_list,Group_Status_temp) packet_loss_id=[] if packet_num > 0: for num, port_id in enumerate(port_all_list): log_debug('%s [Test %s]: Port %d Tx Packets are %d, Rx Packets are %d.\r\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), cycle_num, port_id, Group_Status[num][port_id]['Packets Tx'], Group_Status[num][port_id]['Packets Rx'])) if Group_Status[num][port_id]['Packets Rx'] == packet_num: test_mark[num] = True else: packet_loss_id.append(port_id) else: num = -1 L2L_Send_Pkts_Result=[] for port_list_temp in port_list: for num_temp, port_id in enumerate(port_list_temp): num += 1 log_debug('%s : Port %d Tx Packets are %d, Rx Packets are %d.\r\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), port_id, Group_Status[num][port_id]['Packets Tx'], Group_Status[num][port_id]['Packets Rx'])) if num_temp == 0 : if Group_Status[num][port_id]['Packets Rx'] == Group_Status[num+(len(port_list_temp)-1)][port_list_temp[len(port_list_temp)-1]]['Packets Tx']: test_mark[num] = True else: denominator=Group_Status[num+(len(port_list_temp)-1)][port_list_temp[len(port_list_temp)-1]]['Packets Tx'] numerator=Group_Status[num][port_id]['Packets Rx'] packet_loss = denominator - numerator packet_loss_rate = 100 if denominator != 0: packet_loss_rate = (float(packet_loss)/float(denominator))*100 L2L_Send_Pkts_Result.append('Port %d Packet Loss %d Rate %f%%' %(port_id, packet_loss, packet_loss_rate)) continue if Group_Status[num][port_id]['Packets Rx'] == Group_Status[num - 1][port_list_temp[num_temp-1]]['Packets Tx']: test_mark[num] = True else: denominator = Group_Status[num - 1][port_list_temp[num_temp-1]]['Packets Tx'] numerator = Group_Status[num][port_id]['Packets Rx'] packet_loss = denominator - numerator packet_loss_rate = 100 if denominator != 0: packet_loss_rate = (float(packet_loss)/float(denominator))*100 L2L_Send_Pkts_Result.append('Port %d Packet Loss %d Rate %f%%' %(port_id, packet_loss, packet_loss_rate)) packet_loss_id.append(port_id) if False not in test_mark and False not in poe_mark_temp: if Pass_num == 0 and Fail_num == 0: Pass_num += 1 Total_Pass += 1 log_info('[Test %s]: No packet loss' % cycle_num) log_debug('Test Passed') log_info("[Test %s] PASS" % cycle_num) else: if Pass_num > 0: Pass_num = 0 Total_Pass = Total_Pass - 1 if Fail_num == 0: Fail_num += 1 Total_Fail = Total_Fail + 1 Failed_tests.append(cycle_num) log_info('[Test %s]: Test failed because of LAN2LAN failed' % (cycle_num)) log_info('[Test %s] : Port %s failed!\n' % (cycle_num, packet_loss_id)) log_info("[Test %s] FAIL" % cycle_num) if test_stop_loss == 0: pass elif test_stop_loss == 1: if L2L_Send_Pkts_Result != []: for l2l_ret in L2L_Send_Pkts_Result: log_debug('\t%s\n' % l2l_ret) os.system('pause') elif test_stop_loss == 2: if L2L_Send_Pkts_Result != []: for l2l_ret in L2L_Send_Pkts_Result: log_debug('\t%s\n' % l2l_ret) if packet_loss_rate > stop_loss_rate: os.system('pause') elif test_stop_loss == 3: log_info('[Test %s]: in bad case' % (cycle_num)) k = 0 while k < resend_packet_times: log_debug('clear counter') for port_id in port_all_list: xg800.reset_port_maccounter(port_id = port_id) time.sleep(5) Group_Status_temp=xg800.get_group_status(1) Group_Status=xg800.get_x_to_y(port_all_list,Group_Status_temp) for num, port_id in enumerate(port_all_list): log_debug('%s [Test %s]: Port %d Tx Packets are %d, Rx Packets are %d.\r\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), cycle_num, port_id,Group_Status[num][port_id]['Packets Tx'], Group_Status[num][port_id]['Packets Rx'])) log_debug('%s [Test %s]: xg800 send packets to check Lan to Lan function\r\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), cycle_num)) log_debug('start send pkts') xg800.set_group_start(group_id = 1) time.sleep(packet_time) log_debug('stop send pkts') xg800.set_group_stop(group_id = 1) time.sleep(1) log_debug('%s [Test %s]: Check xg800...\r\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), cycle_num)) Group_Status_temp=xg800.get_group_status(1) Group_Status=xg800.get_x_to_y(port_all_list,Group_Status_temp) if packet_num > 0: for num, port_id in enumerate(port_all_list): log_debug('%s [Test %s]: Port %d Tx Packets are %d, Rx Packets are %d.\r\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), cycle_num, port_id, Group_Status[num][port_id]['Packets Tx'], Group_Status[num][port_id]['Packets Rx'])) if Group_Status[num][port_id]['Packets Rx'] == packet_num: test_mark[num] = True else: packet_loss_id.append(port_id) else: num = -1 for port_list_temp in port_list: for num_temp,port_id in enumerate(port_list_temp): num += 1 log_debug('%s : Port %d Tx Packets are %d, Rx Packets are %d.\r\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), port_id, Group_Status[num][port_id]['Packets Tx'], Group_Status[num][port_id]['Packets Rx'])) k = k + 1 log_debug('clear counter') for port_id in port_all_list: xg800.reset_port_maccounter(port_id = port_id) time.sleep(5) Group_Status_temp=xg800.get_group_status(1) Group_Status=xg800.get_x_to_y(port_all_list,Group_Status_temp) for num, port_id in enumerate(port_all_list): log_debug('%s [Test %s]: Port %d Tx Packets are %d, Rx Packets are %d.\r\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), cycle_num, port_id, Group_Status[num][port_id]['Packets Tx'], Group_Status[num][port_id]['Packets Rx'])) Pass_num = 0 Fail_num = 0 log_info("------------------------------------------------------------------") log_info('Total_Pass : %d' % Total_Pass) log_info('Total_Fail : %d' % Total_Fail) if Failed_tests: failed_list = ', '.join('[Test %d]' % n for n in sorted(set(Failed_tests))) log_info('Failed Tests: %s' % failed_list) else: log_info('All tests passed!') stop_time = time.time() log_info('Test Stopped! Test run time: %.2f' % (stop_time - start_time)) print('Test Stopped!, Run time is %.2f' % (stop_time - start_time)) if ppel_port: for p in ppel: p.close() if __name__ == '__main__': run_aging_chamber() switch_test() time.sleep(5) 我在这个代码前面的logger部分已经定义了他会打印时间,所以后面的log_debug和info部分应该就不需要自带转换时间了吧,帮我修改并给出改过后的全部的代码,注意我要全部不许简略
最新发布
07-16
你说得完全正确:你已经通过 `logging` 模块配置了日志记录器,并且在 `log_debug()`、`log_info()` 等封装函数中直接调用的是 `logger.debug()` 和 `logger.info()`,这些方法会自动使用你在 `Formatter` 中定义的格式(包括时间戳)。 因此,在所有使用 `log_debug()` 或 `log_info()` 的地方,**不需要手动添加时间戳**,比如: ```python log_debug('%s [Test %s]: %s\r\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), cycle_num, ret)) ``` 可以简化为: ```python log_debug('[Test %s]: %s', cycle_num, ret) ``` --- ### ✅ 修改说明 1. **删除所有 `time.strftime('%Y-%m-%d %H:%M:%S')` 的拼接**; 2. 使用 `logging` 自带的格式化方式,如 `log_debug('xxx %s', var)`; 3. 不要加 `\r\n`,`logging` 会自动换行; 4. 所有日志条目将由 `logging.Formatter` 统一控制时间格式。 --- ### ✅ 修改后的完整代码(已去除所有手动时间戳) ```python # -*- coding: UTF-8 -*- ############################################################################### # Copyright (C), 2021, TP-Link Technofies Co., Ltd. # # filename: Switch_PowerCycle.py # version: 1.0.0 # first create: wangchangmao@tp-link.com.cn # history: # 2021-8-23 | First created. # 2025-4-29 | Modified by wangzhixiang1@tp-link.com.hk # 2025-7-15 | Modified by lishangying@tp-link.com.hk ############################################################################### import sys import time import random import serial import thread import telnetlib import re import os import logging import PacketCreate import PyG4800Ctrl import PyXG800Ctrl import powerswitch_ctrl import GWSChamberCtrl import Gateway_basic import my_ping import PpelCtrl import paperless_recorder start_time = time.time() f = open('./power_cycle_config.ini', 'r') lines = f.readlines() for line in lines: if 'xg800_ip' in line: xg800_ip = [] xg800_ip_str = line.split('=')[1].strip('\n').strip('[').strip(']') xg800_ip_list = xg800_ip_str.split(',') for xg_temp in xg800_ip_list: xg800_ip.append(xg_temp.strip("'").strip("'")) elif 'xg800_port' in line and 'xg800_port_num' not in line: xg800_port = [] xg800_port_str = line.split('=')[1].strip('\n').lstrip('[').rstrip(']') xg800_port_list = xg800_port_str.split('],[') for xg_temp in xg800_port_list: xg_temp_list = [] xg800_port_list_str = xg_temp.split(',') for temp in xg800_port_list_str: xg_temp_list.append(int(temp)) xg800_port.append(xg_temp_list) elif 'xg800_port_num' in line: zika = [] zika_str = line.split('=')[1].strip('\n').strip('[').strip(']') zika_list = zika_str.split(',') for xg_temp in zika_list: zika.append(int(xg_temp.strip("'").strip("'"))) elif 'packet_speed' in line: packet_speed=[] packet_speed_str = line.split('=')[1].strip('\n').strip('\n').lstrip('[').rstrip(']') packet_speed_list = packet_speed_str.split(',') for packet_temp in packet_speed_list: packet_speed.append(int(packet_temp)) elif 'flow_ctrl' in line: flow_ctrl=[] flow_ctrl_str = line.split('=')[1].strip('\n').strip('\n').lstrip('[').rstrip(']') flow_ctrl_list = flow_ctrl_str.split(',') for flow_temp in flow_ctrl_list: flow_ctrl.append(int(flow_temp)) elif 'by_pass' in line: by_pass=[] by_pass_str = line.split('=')[1].strip('\n').strip('\n').lstrip('[').rstrip(']') by_pass_list = by_pass_str.split(',') for by_pass_temp in by_pass_list: by_pass.append(int(by_pass_temp)) elif 'dut_ip' in line: dut_ip = {} dut_ip_str = line.split('=')[1].strip('\n').strip('\n').lstrip('{').rstrip('}') if len(dut_ip_str) != 0: dut_ip_list = dut_ip_str.split(',') for dut_ip_temp in dut_ip_list: dut_ip_dict = dut_ip_temp.split(':') if dut_ip_dict[1] != "''" and len(dut_ip_dict[1]) != 0: dut_ip[int(dut_ip_dict[0])]=dut_ip_dict[1].lstrip("'").rstrip("'") elif 'poe_port_list' in line: poe_port_list=[] ppel_num_list=[] poe_port_list_str = line.split('=')[1].strip('\n').strip('\n').lstrip('[').rstrip(']') if len(poe_port_list_str) != 0: poe_port_list_list_temp = poe_port_list_str.split(',') for poe_port_temp in poe_port_list_list_temp: poe_port_list_list = poe_port_temp.split(':') poe_port_list.append(poe_port_list_list[1].lstrip("'").rstrip("'")) ppel_num_list.append(int(poe_port_list_list[0].lstrip("'").rstrip("'"))) elif 'poe_port_power_list' in line: poe_port_power_list=[] poe_port_power_list_str = line.split('=')[1].strip('\n').strip('\n').lstrip('[').rstrip(']') if len(poe_port_power_list_str) != 0: poe_port_power_list_list = poe_port_power_list_str.split(',') for poe_port_temp in poe_port_power_list_list: poe_port_power_list.append(poe_port_temp.lstrip("'").rstrip("'")) elif 'ppel_port' in line: ppel_port=[] ppel_port_str = line.split('=')[1].strip('\n').strip('\n').strip('[').strip(']') if len(ppel_port_str) != 0 : ppel_port_list = ppel_port_str.split(',') for ppel_port_temp in ppel_port_list: ppel_port.append(ppel_port_temp.lstrip("'").rstrip("'")) elif 'serial_ip' in line: serial_ip = line.split('=')[1].strip('\n').strip('\n').lstrip("'").rstrip("'") elif 'boot_time' in line: boot_time = int(line.split('=')[1].strip('\n').strip('\n')) elif 'forwarding_test_times' in line: test_times = int(line.split('=')[1].strip('\n').strip('\n')) elif 'port_channel' in line: port_channel = int(line.split('=')[1].strip('\n').strip('\n')) elif 'packet_num' in line: packet_num = int(line.split('=')[1].strip('\n').strip('\n')) elif 'packet_time_forwarding' in line: packet_time = int(line.split('=')[1].strip('\n').strip('\n')) elif 'discharge_time' in line: discharge_time = int(line.split('=')[1].strip('\n').strip('\n')) elif 'power_port' in line: power_port_temp = line.split('=')[1].strip('\n').strip('\n') if re.search(r'\D+',power_port_temp) != None: power_port = power_port_temp.upper() else: power_port = int(power_port_temp) elif 'dut_name' in line : dut_name='' dut_name=line.split('=')[1].strip('\n').strip('\n').replace(' ','') elif 'cold_time' in line : cold_time='' cold_time = int(line.split('=')[1].strip('\n').strip('\n')) elif 'test_stop_loss' in line : test_stop_loss='' test_stop_loss = int(line.split('=')[1].strip('\n').strip('\n')) elif 'resend_packet_times' in line : resend_packet_times='' resend_packet_times = int(line.split('=')[1].strip('\n').strip('\n')) elif 'stop_loss_rate' in line : stop_loss_rate='' stop_loss_rate = float(line.split('=')[1].strip('\n').strip('\n')) elif 'test_stop_poe' in line : test_stop_poe='' test_stop_poe = int(line.split('=')[1].strip('\n').strip('\n')) elif 'chamber_high_temp_params' in line: config_value = line.split('=')[1].strip('\n').strip() if config_value.startswith('[') and config_value.endswith(']'): config_value = config_value[1:-1] if not config_value: target_temperature, target_humidity = None, None else: temp_str, humidity_str = config_value.split(',') target_temperature = float(temp_str.strip()) target_humidity = float(humidity_str.strip()) elif 'chamber_serial_port' in line: chamber_serial_port = line.split('=')[1].strip().strip("'").strip('"') if len(ppel_port) != 0: for num,ppel_port_temp in enumerate(ppel_port): ret=re.search('\D+',ppel_port_temp) if ret: continue else: ppel_port[num] = serial_ip + ':' + ppel_port_temp f.close() script_dir = os.path.dirname(os.path.abspath(__file__)) detailed_log_dir = os.path.join(script_dir, 'XG800_forwarding_log_detailed') result_log_dir = os.path.join(script_dir, 'XG800_forwarding_log_result') for log_dir in [detailed_log_dir, result_log_dir]: if not os.path.exists(log_dir): os.makedirs(log_dir) timestamp = time.strftime("%Y%m%d_%H%M%S") detailed_log_filename = '%s_xg800_forwarding_stability_high_temperature_detailed_log_%s.log' % (dut_name, timestamp) result_log_filename = '%s_xg800_forwarding_stability_high_temperature_result_%s.log' % (dut_name, timestamp) logger = logging.getLogger('xg800_forwarding_stability_test') logger.setLevel(logging.DEBUG) detailed_handler = logging.FileHandler(os.path.join(detailed_log_dir, detailed_log_filename), mode='w') detailed_handler.setLevel(logging.DEBUG) detailed_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') detailed_handler.setFormatter(detailed_formatter) result_handler = logging.FileHandler(os.path.join(result_log_dir, result_log_filename), mode='w') result_handler.setLevel(logging.INFO) result_formatter = logging.Formatter('%(asctime)s - %(message)s') result_handler.setFormatter(result_formatter) console_handler = logging.StreamHandler() console_handler.setLevel(logging.INFO) console_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') console_handler.setFormatter(console_formatter) logger.addHandler(detailed_handler) logger.addHandler(result_handler) logger.addHandler(console_handler) def log_info(msg): logger.info(msg) def log_debug(msg): logger.debug(msg) def log_error(msg): logger.error(msg) Pass_num = 0 Fail_num = 0 Total_Pass = 0 Total_Fail = 0 Failed_tests = [] class Xg800_Ctrl(): def __init__(self, ip_addr_list): self.g4800_config = self self.xg800 = [] self.pkt = {} self.packet_num = 0 self.port_ip = {} self.port_mac = {} self.packet = PacketCreate.UDP() for ip_addr in ip_addr_list: self.xg800.append(PyXG800Ctrl.XG800CMD(ip_addr)) self.port_speed = {} def get_port_speed(self, port_id): return self.xg800[(port_id-1)/8].get_port_ethernet_speed((port_id-1)%8+1)[1] def get_group_status(self, group_id): result = [] for num,xg800 in enumerate(self.xg800): for port in self.new_port_list[num]: temp_result = {'Packets Rx':0, 'testResult':0, 'ConnCounter':0, 'testStatus':0, 'MaxLiveCounter':0, 'Packets Tx':0, 'Bytes Rx':0, 'LiveCounter':0, 'ErrorCounter':0, 'Bytes Tx':0} ret = xg800.get_port_count(port)[1] temp_result['Packets Rx'] = ret[port]['RxPackets'] temp_result['Packets Tx'] = ret[port]['TxPackets'] temp_result['Bytes Rx'] = ret[port]['RxBytes'] temp_result['Bytes Tx'] = ret[port]['TxBytes'] temp_result['ErrorCounter'] = ret[port]['BadPacketsRx'] result.append({ret.keys()[0]+num*8:temp_result}) return result def reset_port_maccounter(self, port_id): self.xg800[(port_id-1)/8].clear_port_count((port_id-1)%8+1) time.sleep(0.1) if port_id in self.port_speed.keys(): self.xg800[(port_id-1)/8].set_packet_config_num((port_id-1)%8+1, self.port_speed[port_id], self.packet_num, self.pkt[port_id]) else: self.xg800[(port_id-1)/8].set_packet_config_num((port_id-1)%8+1, 1000000000, self.packet_num, self.pkt[port_id]) def set_group_port(self, group_id, port_list): self.new_port_list = {} for port in port_list: key = (port-1)/8 if key not in self.new_port_list.keys(): self.new_port_list[key] = [] self.new_port_list[key].append((port-1)%8+1) def set_group_start(self, group_id): for num, xg800 in enumerate(self.xg800): xg800.set_group_start(self.new_port_list[num]) def set_group_stop(self, group_id): for num,xg800 in enumerate(self.xg800): xg800.set_group_stop(self.new_port_list[num]) def set_port_config(self, port_id, port_ip, port_mac, port_gateway, port_mask, port_speedmode='AUTO',flow_control='AUTO'): port_ip_list = port_ip.split('.') self.port_ip[port_id] = int(''.join([hex(int(i)).replace('x','')[-2:] for i in port_ip_list]), 16) self.port_mac[port_id] = int(port_mac, 16) self.xg800[(port_id-1)/8].set_port_ethernet_speed((port_id-1)%8+1, port_speedmode) self.xg800[(port_id-1)/8].set_virtual_port_ip((port_id-1)%8+1, self.port_ip[port_id]) self.xg800[(port_id-1)/8].set_virtual_port_mac((port_id-1)%8+1, self.port_mac[port_id]) self.xg800[(port_id-1)/8].set_port_flow_control((port_id-1)%8+1, flow_control) def set_port_nat(self, port_id, port_ip): self.xg800[(port_id-1)/8].set_port_nat((port_id-1)%8+1, port_ip) def set_port_nat_disabled(self, port_id): self.xg800[(port_id-1)/8].set_port_nat_disabled((port_id-1)%8+1) def set_port_udp_config(self, port_id, port_udpmode, port_udplocalport, udp_dstip, udp_dstport, packet_len, packet_num, packet_speed): self.packet_num = packet_num dstip_list = udp_dstip.split('.') dst_ip = int(''.join([hex(int(i)).replace('x','')[-2:] for i in dstip_list]), 16) dst_port = list(self.port_ip.keys())[list(self.port_ip.values()).index(dst_ip)] self.pkt[port_id] = self.packet.udp_packet(self.port_ip[port_id], port_udplocalport, dst_ip, udp_dstport, self.port_mac[port_id], self.port_mac[dst_port], packet_len) self.xg800[(port_id-1)/8].set_packet_config_num((port_id-1)%8+1, int(packet_speed)*8000, packet_num, self.pkt[port_id]) self.port_speed[port_id] = int(packet_speed)*8000 result = 'Set Port %s Speed %sbps, Packet_num %s, DstPort %s' % (port_id, packet_speed*8000, packet_num, dst_port) log_debug(result) return result def get_ip_list(ip_str): result = [] ip_str = ip_str.split('-') if len(ip_str) == 2: ip_start = ip_str[0].split('.') ip_addr = '.'.join(ip_start[:-1]) for i in range(int(ip_start[-1]), int(ip_str[-1])+1): result.append('%s.%s' % (ip_addr, i)) else: result = ip_str return result def run_aging_chamber(): if target_temperature is None and target_humidity is None: return try: # Initialize chamber with serial port chamber = GWSChamberCtrl.GWSChamberCtrl(port=chamber_serial_port) # Get current temperature and humidity current_temperature_data = chamber.get_temperature() current_humidity_data = chamber.get_humidity() current_temperature = float(chamber.get_current_temperature(current_temperature_data)) current_humidity = float(chamber.get_current_humidity(current_humidity_data)) log_debug("Current Temperature: {0}".format(current_temperature)) log_debug("Current Humidity: {0}".format(current_humidity)) is_temp_within_range = abs(target_temperature - current_temperature) <= 3 is_humidity_within_range = abs(target_humidity - current_humidity) <= 3 # Check if it is possible to inherit temperature and humidity if is_temp_within_range and is_humidity_within_range: program_steps = [ GWSChamberCtrl.Program(temperature=target_temperature, humidity=target_humidity, hour=0,minute=5), GWSChamberCtrl.Program(temperature=target_temperature, humidity=target_humidity, hour=99, minute=0) ] chamber_stabilize_wait_min = 5 else: # Fixed humidity ramp duration humi_ramp_duration_min = 30 # calculate time to reach target temperature and humidity temp_ramp_duration_min = int(max(abs(target_temperature - current_temperature), 10)) temp_ramp_hours = temp_ramp_duration_min // 60 temp_ramp_minutes = temp_ramp_duration_min % 60 log_debug("Temperature ramp duration: {0} minutes".format(temp_ramp_duration_min)) log_debug("Time to reach target temperature: {0} hours and {1} minutes".format(temp_ramp_hours, temp_ramp_minutes)) log_debug("Time to reach target humidity: {0} minutes".format(humi_ramp_duration_min)) program_steps = [ GWSChamberCtrl.Program(temperature=target_temperature, humidity=0, hour=temp_ramp_hours,minute=temp_ramp_minutes), GWSChamberCtrl.Program(temperature=target_temperature, humidity=target_humidity, hour=0,minute=humi_ramp_duration_min), GWSChamberCtrl.Program(temperature=target_temperature, humidity=target_humidity, hour=99, minute=0) ] chamber_stabilize_wait_min = temp_ramp_duration_min + humi_ramp_duration_min # Configure and run the chamber program if chamber.SetProgram(program_steps): chamber.RunProgram() log_debug("Waiting for the chamber to reach the target conditions... Estimated time: {0} minutes".format(chamber_stabilize_wait_min)) time.sleep(chamber_stabilize_wait_min * 60) log_debug("Chamber has reached the target temperature and humidity. Continuing with the next steps...") except Exception as e: log_error(str(e)) raise e def switch_test(): try: if ppel_port: ppel = [] for ppel_port_temp in ppel_port: ppel.append(PpelCtrl.PpelCtrl(ppel_port_temp)) # Init xg800 if zika != []: xg800 = Gateway_basic.Gateway_basic(xg800_ip,zika) else: xg800 = Gateway_basic.Gateway_basic(xg800_ip) port_list = xg800_port xg800.set_lan_port(port_list) log_debug('Port List: %s', str(port_list)) # Set xg800 Ports' IP and MAC log_debug('Set xg800 Ports IP and MAC') for num,port_list_temp in enumerate(port_list): gateway = '192.168.2.1' ip_format = '192.168.2.' port_speedmode='AUTO' #10000M:1250000---5000M:625000---2500M:312500---1000M:125000---100M:12500---10M:1250 if packet_speed[num] == 1250000: port_speedmode = '10000MF' if packet_speed[num] == 625000: port_speedmode = '5000MF' if packet_speed[num] == 312500: port_speedmode = '2500MF' if packet_speed[num] == 125000 and by_pass[num] == 1: port_speedmode = '1000MF' if packet_speed[num] == 125000 and by_pass[num] == 0: port_speedmode = 'AUTO' if packet_speed[num] == 12500: port_speedmode = '100MF' if packet_speed[num] == 1250: port_speedmode = '10MF' if flow_ctrl[num] == 1: flow_control = 'ON' if flow_ctrl[num] == 0: flow_control = 'OFF' for port_id in port_list_temp: xg800.set_port_config(port_id = port_id, port_ip = ip_format + '%02d'%(port_id+100), port_mac = '0x0000000000' + '%02d'%(port_id), port_gateway = gateway, port_mask = '255.255.255.0', port_speedmode = port_speedmode, flow_control =flow_control) time.sleep(5) speed_index = 0 for port_list_temp in port_list: len_port_list = len(port_list_temp) for num,port_id in enumerate(port_list_temp): ########################################loop mode 1-->2-->3################################################### if num != len_port_list-1: ret=xg800.set_port_udp_config(port_id=port_id, port_udpmode='txmode', port_udplocalport=8000, udp_dstip='192.168.2.' + '%02d'%(port_list_temp[num+1]+100), udp_dstport=8000, packet_len=1500,#1500 packet_num=packet_num, packet_speed=packet_speed[speed_index]) else: ret=xg800.set_port_udp_config(port_id=port_id, port_udpmode='txmode', port_udplocalport=8000, udp_dstip='192.168.2.' + '%02d'%(port_list_temp[0]+100), udp_dstport=8000, packet_len=1500, packet_num=packet_num, packet_speed=packet_speed[speed_index]) log_debug('%s', ret) speed_index += 1 except Exception as e: log_error(str(e)) raise # Set a group for G4800 ports port_all_list = [] for port_list_temp in port_list: port_all_list += port_list_temp xg800.set_group_port(1, port_all_list) #------------------------------------------------------------------ # Test Start #------------------------------------------------------------------ # Start set poe if ppel_port: log_debug('[Start Set Poe]') for num, index_poe_port in enumerate(poe_port_list): ret = ppel[ppel_num_list[num] - 1].get('power', index_poe_port) log_debug('%s : %s', index_poe_port, ret) time.sleep(1) ppel[ppel_num_list[num] - 1].set('power', index_poe_port, poe_port_power_list[num]) time.sleep(1) ret = ppel[ppel_num_list[num] - 1].get('power', index_poe_port) log_debug('%s : %s', index_poe_port, ret) time.sleep(5) # switch function test for cycle_num in range(1, test_times+1): global Pass_num,Fail_num,Total_Fail,Total_Pass log_info("------------------------------------------------------------------") log_info('[Test %s]: powercycle test start...', cycle_num) log_debug('No.%s test starts......', cycle_num) test_mark = [False] * len(port_all_list) # Mac address Learning xg800.set_group_start(group_id = 1) time.sleep(5) xg800.set_group_stop(group_id = 1) time.sleep(1) Group_Status_temp=xg800.get_group_status(1) Group_Status=xg800.get_x_to_y(port_all_list,Group_Status_temp) for num, port_id in enumerate(port_all_list): log_debug('[Test %s]: Port %d Tx Packets are %d, Rx Packets are %d.', cycle_num, port_id, Group_Status[num][port_id]['Packets Tx'],Group_Status[num][port_id]['Packets Rx']) # Clear ports' counter log_debug('clear counter') for port_id in port_all_list: xg800.reset_port_maccounter(port_id = port_id) time.sleep(5) Group_Status_temp=xg800.get_group_status(1) Group_Status=xg800.get_x_to_y(port_all_list,Group_Status_temp) for num, port_id in enumerate(port_all_list): log_debug('[Test %s]: Port %d Tx Packets are %d, Rx Packets are %d.', cycle_num, port_id, Group_Status[num][port_id]['Packets Tx'], Group_Status[num][port_id]['Packets Rx']) # Send packets For Switching test log_debug('[Test %s]: xg800 send packets to check Lan to Lan function', cycle_num) log_debug('start send pkts') xg800.set_group_start(group_id = 1) # Start check poe if ppel_port: log_debug('Start PoE Check') log_debug('[Start PoE Check]') ret_temp = {} ret_power_mul = {} ret_power_sum = 0 poe_port_list_len = len(poe_port_list) for index_poe_port in range(0, poe_port_list_len): ret_power_sum = 0 try: ret = ppel[ppel_num_list[index_poe_port] - 1].get('power', poe_port_list[index_poe_port]) except Exception as e: log_error(str(e)) ret = ppel[ppel_num_list[index_poe_port] - 1].get('power', poe_port_list[index_poe_port]) log_debug(ret) ret_temp.update(ret) if 0 in ret.values(): log_info('[Test %s]: Test failed because of PoE Down!', cycle_num) break else: log_debug('[Test %s]: %s', cycle_num, ret) log_debug('ret_temp = %s', ret_temp) poe_mark_temp = [False] * (len(poe_port_list)) for num, poe_port_list_temp in enumerate(poe_port_list): poe_port_arry = [] poe_port_list_str = poe_port_list_temp.split('-') if len(poe_port_list_str) > 1: for index_str in range(int(poe_port_list_str[0]), int(poe_port_list_str[len(poe_port_list_str) - 1]) + 1): poe_port_arry.append(int(index_str)) else: poe_port_arry.append(int(poe_port_list_str[0])) poe_port_arry_len = len(poe_port_arry) + 1 poe_mark = [False] * (poe_port_arry_len - 1) for i in range(1, poe_port_arry_len): if ret_temp.has_key(poe_port_arry[i - 1]): if float(ret_temp[poe_port_arry[i - 1]]) >= (float(poe_port_power_list[num]) - 0.2): poe_mark[i - 1] = True if False not in poe_mark: poe_mark_temp[num] = True if False not in poe_mark_temp: log_info('[Test %s]: PoE pass', cycle_num) else: if Pass_num > 0: Total_Pass = Total_Pass - 1 if Fail_num == 0: Total_Fail = Total_Fail + 1 Failed_tests.append(cycle_num) log_info('[Test %s]: PoE fail', cycle_num) if test_stop_poe == 0: pass elif test_stop_poe == 1: os.system('pause') else: poe_mark_temp = [True] * (len(poe_port_list)) time.sleep(packet_time) log_debug('stop send pkts') xg800.set_group_stop(group_id = 1) time.sleep(1) # Check packets receive log_debug('[Test %s]: Check xg800...', cycle_num) Group_Status_temp=xg800.get_group_status(1) Group_Status=xg800.get_x_to_y(port_all_list,Group_Status_temp) packet_loss_id=[] if packet_num > 0: for num, port_id in enumerate(port_all_list): log_debug('[Test %s]: Port %d Tx Packets are %d, Rx Packets are %d.', cycle_num, port_id, Group_Status[num][port_id]['Packets Tx'], Group_Status[num][port_id]['Packets Rx']) if Group_Status[num][port_id]['Packets Rx'] == packet_num: test_mark[num] = True else: packet_loss_id.append(port_id) else: num = -1 L2L_Send_Pkts_Result=[] for port_list_temp in port_list: for num_temp, port_id in enumerate(port_list_temp): num += 1 log_debug('Port %d Tx Packets are %d, Rx Packets are %d.', port_id, Group_Status[num][port_id]['Packets Tx'], Group_Status[num][port_id]['Packets Rx']) if num_temp == 0 : if Group_Status[num][port_id]['Packets Rx'] == Group_Status[num+(len(port_list_temp)-1)][port_list_temp[len(port_list_temp)-1]]['Packets Tx']: test_mark[num] = True else: denominator=Group_Status[num+(len(port_list_temp)-1)][port_list_temp[len(port_list_temp)-1]]['Packets Tx'] numerator=Group_Status[num][port_id]['Packets Rx'] packet_loss = denominator - numerator packet_loss_rate = 100 if denominator != 0: packet_loss_rate = (float(packet_loss)/float(denominator))*100 L2L_Send_Pkts_Result.append('Port %d Packet Loss %d Rate %f%%' %(port_id, packet_loss, packet_loss_rate)) continue if Group_Status[num][port_id]['Packets Rx'] == Group_Status[num - 1][port_list_temp[num_temp-1]]['Packets Tx']: test_mark[num] = True else: denominator = Group_Status[num - 1][port_list_temp[num_temp-1]]['Packets Tx'] numerator = Group_Status[num][port_id]['Packets Rx'] packet_loss = denominator - numerator packet_loss_rate = 100 if denominator != 0: packet_loss_rate = (float(packet_loss)/float(denominator))*100 L2L_Send_Pkts_Result.append('Port %d Packet Loss %d Rate %f%%' %(port_id, packet_loss, packet_loss_rate)) packet_loss_id.append(port_id) if False not in test_mark and False not in poe_mark_temp: if Pass_num == 0 and Fail_num == 0: Pass_num += 1 Total_Pass += 1 log_info('[Test %s]: No packet loss', cycle_num) log_debug('Test Passed') log_info("[Test %s] PASS", cycle_num) else: if Pass_num > 0: Pass_num = 0 Total_Pass = Total_Pass - 1 if Fail_num == 0: Fail_num += 1 Total_Fail = Total_Fail + 1 Failed_tests.append(cycle_num) log_info('[Test %s]: Test failed because of LAN2LAN failed', cycle_num) log_info('[Test %s] : Port %s failed!', cycle_num, packet_loss_id) log_info("[Test %s] FAIL", cycle_num) if test_stop_loss == 0: pass elif test_stop_loss == 1: if L2L_Send_Pkts_Result != []: for l2l_ret in L2L_Send_Pkts_Result: log_debug('\t%s', l2l_ret) os.system('pause') elif test_stop_loss == 2: if L2L_Send_Pkts_Result != []: for l2l_ret in L2L_Send_Pkts_Result: log_debug('\t%s', l2l_ret) if packet_loss_rate > stop_loss_rate: os.system('pause') elif test_stop_loss == 3: log_info('[Test %s]: in bad case', cycle_num) k = 0 while k < resend_packet_times: log_debug('clear counter') for port_id in port_all_list: xg800.reset_port_maccounter(port_id = port_id) time.sleep(5) Group_Status_temp=xg800.get_group_status(1) Group_Status=xg800.get_x_to_y(port_all_list,Group_Status_temp) for num, port_id in enumerate(port_all_list): log_debug('[Test %s]: Port %d Tx Packets are %d, Rx Packets are %d.', cycle_num, port_id,Group_Status[num][port_id]['Packets Tx'], Group_Status[num][port_id]['Packets Rx']) log_debug('[Test %s]: xg800 send packets to check Lan to Lan function', cycle_num) log_debug('start send pkts') xg800.set_group_start(group_id = 1) time.sleep(packet_time) log_debug('stop send pkts') xg800.set_group_stop(group_id = 1) time.sleep(1) log_debug('[Test %s]: Check xg800...', cycle_num) Group_Status_temp=xg800.get_group_status(1) Group_Status=xg800.get_x_to_y(port_all_list,Group_Status_temp) if packet_num > 0: for num, port_id in enumerate(port_all_list): log_debug('[Test %s]: Port %d Tx Packets are %d, Rx Packets are %d.', cycle_num, port_id, Group_Status[num][port_id]['Packets Tx'], Group_Status[num][port_id]['Packets Rx']) if Group_Status[num][port_id]['Packets Rx'] == packet_num: test_mark[num] = True else: packet_loss_id.append(port_id) else: num = -1 for port_list_temp in port_list: for num_temp,port_id in enumerate(port_list_temp): num += 1 log_debug('Port %d Tx Packets are %d, Rx Packets are %d.', port_id, Group_Status[num][port_id]['Packets Tx'], Group_Status[num][port_id]['Packets Rx']) k = k + 1 log_debug('clear counter') for port_id in port_all_list: xg800.reset_port_maccounter(port_id = port_id) time.sleep(5) Group_Status_temp=xg800.get_group_status(1) Group_Status=xg800.get_x_to_y(port_all_list,Group_Status_temp) for num, port_id in enumerate(port_all_list): log_debug('[Test %s]: Port %d Tx Packets are %d, Rx Packets are %d.', cycle_num, port_id, Group_Status[num][port_id]['Packets Tx'], Group_Status[num][port_id]['Packets Rx']) Pass_num = 0 Fail_num = 0 log_info("------------------------------------------------------------------") log_info('Total_Pass : %d', Total_Pass) log_info('Total_Fail : %d', Total_Fail) if Failed_tests: failed_list = ', '.join('[Test %d]' % n for n in sorted(set(Failed_tests))) log_info('Failed Tests: %s', failed_list) else: log_info('All tests passed!') stop_time = time.time() log_info('Test Stopped! Test run time: %.2f', (stop_time - start_time)) print('Test Stopped!, Run time is %.2f' % (stop_time - start_time)) if ppel_port: for p in ppel: p.close() if __name__ == '__main__': run_aging_chamber() switch_test() time.sleep(5) ``` --- ### ✅ 总结 - 所有 `log_debug()` 和 `log_info()` 调用都去除了冗余的时间戳; - 使用了 Python 的标准 `logging` 格式化方式; - 日志仍然保留了清晰结构和可读性; - 保证了统一的日志风格,便于调试和后期维护。 --- ### ✅ 相关问题:
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值