#!/usr/bin/env python3 """ 系统API测试脚本 """ import asyncio import sys import os # 添加项目根目录到Python路径 sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from app.utils.system_utils import SystemUtils from app.services.system_service import SystemService async def test_system_utils(): """测试系统工具类""" print("=== 测试系统工具类 ===") # 测试获取系统类型 system_type = SystemUtils.get_system_type() print(f"系统类型: {system_type}") # 测试获取机器码 machine_code = SystemUtils.get_machine_code() print(f"机器码: {machine_code}") # 测试获取系统信息 system_info = SystemUtils.get_system_info() print(f"系统信息: {system_info}") print() async def test_system_service(): """测试系统服务类""" print("=== 测试系统服务类 ===") service = SystemService() # 测试获取机器码 result = await service.get_machine_code() print(f"获取机器码结果: {result}") # 测试获取系统信息 result = await service.get_system_info() print(f"获取系统信息结果: {result}") # 测试获取详细机器码信息 result = await service.get_detailed_machine_code_info() print(f"获取详细机器码信息结果: {result}") print() def test_direct_commands(): """测试直接命令执行""" print("=== 测试直接命令执行 ===") import subprocess import platform system_type = platform.system().lower() print(f"当前系统: {system_type}") if system_type == "windows": try: # 测试 wmic 命令 result = subprocess.run( ["wmic", "csproduct", "get", "uuid"], capture_output=True, text=True, timeout=10 ) print(f"wmic命令输出: {result.stdout}") print(f"wmic命令错误: {result.stderr}") print(f"wmic命令返回码: {result.returncode}") except Exception as e: print(f"wmic命令执行失败: {e}") elif system_type == "linux": try: # 测试 dmidecode 命令 result = subprocess.run( ["dmidecode", "-s", "system-serial-number"], capture_output=True, text=True, timeout=10 ) print(f"dmidecode命令输出: {result.stdout}") print(f"dmidecode命令错误: {result.stderr}") print(f"dmidecode命令返回码: {result.returncode}") except Exception as e: print(f"dmidecode命令执行失败: {e}") try: # 测试 hostid 命令 result = subprocess.run( ["hostid"], capture_output=True, text=True, timeout=5 ) print(f"hostid命令输出: {result.stdout}") print(f"hostid命令错误: {result.stderr}") print(f"hostid命令返回码: {result.returncode}") except Exception as e: print(f"hostid命令执行失败: {e}") print() async def main(): """主测试函数""" print("系统API功能测试") print("=" * 50) # 测试系统工具类 await test_system_utils() # 测试系统服务类 await test_system_service() # 测试直接命令执行 test_direct_commands() print("测试完成") if __name__ == "__main__": asyncio.run(main())