import platform import hashlib import time def get_computer_name_id(): """使用计算机名生成ID(方案三)""" try: computer_name = platform.node() if computer_name: print(f"\n计算机名: {computer_name}") hardware_id = hashlib.md5(computer_name.encode()).hexdigest() return hardware_id return None except Exception as e: print(f"获取计算机名失败: {str(e)}") return None def test_stability(): """测试ID的稳定性""" print("开始测试计算机名生成ID的稳定性...") print("将进行10次测试,每次间隔1秒") # 存储每次生成的ID computer_ids = [] for i in range(10): print(f"\n第 {i+1} 次测试:") # 测试计算机名方案 comp_id = get_computer_name_id() if comp_id: print(f"使用计算机名生成的ID: {comp_id}") computer_ids.append(comp_id) time.sleep(1) # 分析结果 print("\n测试结果分析:") if computer_ids: unique_ids = set(computer_ids) print(f"\n计算机名方案:") print(f"生成次数: {len(computer_ids)}") print(f"唯一ID数: {len(unique_ids)}") print("是否稳定: " + ("是" if len(unique_ids) == 1 else "否")) if len(unique_ids) > 1: print("出现的不同ID:") for idx, id in enumerate(unique_ids): print(f"{idx+1}. {id}") else: print("\n计算机名方案: 获取失败") if __name__ == "__main__": test_stability()