微前端架构如何改变企业的开发模式与效率提升
1305
2022-09-05
Python3获取本地硬件信息(Windows)
import wmi, pynvml, re, socket‘’‘从小白到放弃。。。。 学习过程中的练手脚本,希望能得到大家的意见试了各种办法,显卡的厂商信息死活获取不到,然后就放弃再优化了,不过做个简单硬件配置参数统计应该还是可以的,这里只是初步的获取硬件信息方法,自己再加一个输出到Excel或者数据库都可以这里用到了wmi相当于windows的wmic命令;pynvml用来获取显卡信息;re用来做字符串匹配提取;socket用来获取IP地址等四个库待考虑项: 需要自己再加兼容多cpu、网卡、显卡的方法代码,否则会漏统计硬件 需要自己再优化去移动硬盘等设备的方法,否则连u盘等都会统计出来 IP地址获取也得再改进一下,注意我这里是通过socket连接网络管理设备telnet口来获取的自身IP地址 ’‘’w = wmi.WMI()class Get_info(): #获取cpu和计算机名 def get_cpu_os_info(self): dict = {} for info in w.Win32_Processor(): #获取cpu详细信息(没有做双cpu兼容) dict['cpu_info'] = info.Name #获取计算机名 dict['hostname'] = info.SystemName return dict #获取用户名 def get_username(self): #获取当前登录用户名 user = w.Win32_ComputerSystem()[0] return {"UserName":user.PrimaryOwnerName} #获取主板信息 def get_board_info(self): dict = {} #获取主板信息 for board in w.Win32_BaseBoard(): dict['board_Manufacturer'] = board.Manufacturer dict['board_Product'] = board.Product boards = [i for i in dict.values()] return {"Board":boards[0] + ' ' + boards[1]} #获取硬盘信息(去除usb移动设备兼容性测试并不完善) def get_disk_info(self): Caption_lists = [] size_lists = [] for i in w.Win32_DiskDrive(): if i.size != None and "USB" not in i.Caption: Caption = i.Caption size = str(int(i.Size) // (1000 * 1000 * 1000)) Caption_lists.append(Caption) size_lists.append(size) result_size = [] count = len(Caption_lists) for i in range(0, count): b = Caption_lists[i] + ' ' + size_lists[i] + 'GB' result_size.append(b) return result_size #获取内存信息 def get_memory(self): BankLabel_list = [] Manufacturer_list = [] Capacity_list = [] for i in w.Win32_PhysicalMemory(): #插槽 BankLabel = i.BankLabel #品牌 Manufacturer = i.Manufacturer #容量 Capacity = int(i.Capacity) // (1024 * 1024 * 1024) BankLabel_list.append(BankLabel) Manufacturer_list.append(Manufacturer) Capacity_list.append(Capacity) result_size = {} count = len(BankLabel_list) for i in range(0, count): result_size['port %s' % str(int(i) + 1)] = BankLabel_list[i] result_size['brand %s' % str(int(i) + 1)] = Manufacturer_list[i] result_size['size %s' % str(int(i) + 1)] = Capacity_list[i] return result_size #获取IP地址(没有做多网卡兼容,通过socket连接路由器取本机IP) def get_ip(self): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: s.connect(("192.168.0.254", 23)) except: return {"无网卡/驱动/地址"} ip = s.getsockname()[0] s.close() return {'IP':ip} #获取显卡信息(没有做多显卡及无驱动兼容) def get_gpu_info(self): try: pynvml.nvmlInit() except: return "无显卡/驱动" deviceCount = pynvml.nvmlDeviceGetCount() for i in range(deviceCount): handle = pynvml.nvmlDeviceGetHandleByIndex(i) gpu_name = pynvml.nvmlDeviceGetName(handle) matchObj = re.compile(r"'(.*?)'", re.S) result = matchObj.findall(str(gpu_name)) pynvml.nvmlShutdown() return {"GPU":result} #获取MAC地址(没有做多网卡兼容) def Getphysicaladdress(self): import uuid node = uuid.getnode() macHex = uuid.UUID(int=node).hex[-12:] mac = [] for i in range(len(macHex))[::2]: mac.append(macHex[i:i + 2]) mac = ':'.join(mac) return {'MAC:':mac}#测试输出方法test = Get_info()print(test.get_cpu_os_info())print(test.get_username())print(test.get_board_info())print(test.get_disk_info())print(test.get_memory())print(test.get_gpu_info())print(test.get_ip())print(test.Getphysicaladdress())
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~