参考wmi的用法:https://blog.csdn.net/u014101410/article/details/104159072
简道云API文档说明:https://hc.jiandaoyun.com/open/10993
如果你也是用在简道云上,只需要修改 模块id 表单id appkey 三个参数即可
import winreg
import wmi
import requests
import json
import urllib3
urllib3.disable_warnings()
data_dict = {}
name = input("使用者姓名:")
department = input("所属部门:")
data_dict["name"] = name
data_dict["department"] = department
c = wmi.WMI()
print("正在获取电脑CPU信息……")
for cpu in c.Win32_Processor():
# print("您的CPU名称为:" + cpu.Name)
# cpu.Name = cpu.Name
# print(type(cpu.Name))
data_dict["cpu"] = cpu.Name
print("正在获取电脑硬盘信息……")
disk_space = 0 # 初始化硬盘大小参数为0
for physical_disk in c.Win32_DiskDrive(): # for 获取多块硬盘
disk_size = round(int(physical_disk.Size) / (1024 * 1024 * 1024), 2)
disk_str = physical_disk.Caption+'*' + str(disk_size)+'G'
data_dict.setdefault("disk", []).append(disk_str)
disk_space = disk_space + disk_size
merge_disk = ','.join(data_dict["disk"]) # str_night = ''.join(list_good_night)把列表转换成字符串
data_dict["disk"] = merge_disk
data_dict["disk_space"] = disk_space
print("正在获取电脑内存信息……")
try:
memory_space = 0
for mem in c.Win32_PhysicalMemory(): # 获取多个内存
mem_size = int(mem.capacity)/1024/1024/1024
# print(mem.Manufacturer, mem.Speed, mem_size, mem.PartNumber)
# print(mem)
mem_one = mem.Manufacturer.strip()+"*" + str(mem.Speed)+'MHZ'+'*' + str(mem_size)+'*' + mem.PartNumber.strip()
data_dict.setdefault("memory", []).append(mem_one)
memory_space = mem_size + memory_space
merge_memory = ','.join(data_dict["memory"])
data_dict["memory"] = merge_memory
data_dict["memory_space"] = memory_space
except Exception as e:
data_dict["memory"] = input("内存获取权限不足,请手动填写:")
print("正在获取电脑显卡信息……")
for video in c.Win32_VideoController():
# print(video.Name)
data_dict["video_card"] = video.name
print("正在获取电脑网卡信息……")
for ip_mac in c.Win32_NetworkAdapterConfiguration(): # 获取多个ip
# print(ip_mac.IPAddress,ip_mac.MACAddress)
if ip_mac.IPAddress == None:
pass
else:
data_dict.setdefault("ip_address", []).append(ip_mac.IPAddress)
data_dict.setdefault("mac_address", []).append(ip_mac.MACAddress)
merge_list_ip = []
for tup_ip in data_dict["ip_address"]:# ip 元组转换为字符串
list_ip = list(tup_ip)
merge_list_ip = merge_list_ip + list_ip
data_dict["ip_address"] = ','.join(merge_list_ip)
merge_mac = ''
# print(data_dict["mac_address"])
for str_mac in data_dict["mac_address"]:
merge_mac = merge_mac + str_mac + ','
data_dict["mac_address"] = merge_mac
print("正在获取电脑主板信息……")
for main_board in c.Win32_BaseBoard():
# print(main_board.Manufacturer,main_board.Name,main_board.Product,main_board.SerialNumber)
main_board_merge = main_board.Manufacturer + main_board.Name + main_board.Product + ','+ main_board.SerialNumber
data_dict["main_board"] = main_board_merge
print("正在获取电脑显示器信息……")
try:
PATH = "SYSTEM\\ControlSet001\\Enum\\" # 用于获取多个显示器信息,win10没问题,win7获取不到
for display in c.Win32_PnPEntity():
print(display)
if display.PNPClass == "Monitor":
subPath = display.PNPDeviceID
infoPath = PATH + subPath + "\\Device Parameters"
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, infoPath)
# 屏幕信息按照一定的规则保存(EDID)
value = winreg.QueryValueEx(key, "EDID")[0]
winreg.CloseKey(key)
# 屏幕实际尺寸
width, height = value[21], value[22]
# 推荐屏幕分辨率
widthResolution = value[56] + (value[58] >> 4) * 256
heightResolution = value[59] + (value[61] >> 4) * 256
# 屏幕像素密度(Pixels Per Inch)
widthDensity = widthResolution / (width / 2.54)
heightDensity = heightResolution / (height / 2.54)
# print("屏幕宽度:", width, " (厘米)")
# print("屏幕高度:", height, " (厘米)")
# print("水平分辩率: ", widthResolution, " (像素)")
# print("垂直分辩率: ", heightResolution, " (像素)")
# 保留小数点固定位数的两种方法
# print("水平像素密度: ", round(widthDensity, 2), " (PPI)")
# print("垂直像素密度: ", "%2.f" % heightDensity, " (PPI)")
# print(display)
arrange_display = display.PNPDeviceID.replace("DISPLAY", "")
# print(arrange_test)
data_dict.setdefault("display", []).append(arrange_display)
data_dict.setdefault("display_size", []).append('宽' + str(width) + 'cm' + '*' + '高' + str(height) + 'cm')
data_dict.setdefault("display_resolution", []).append(str(widthResolution) + '*' + str(heightResolution))
# print(data_dict)
merge_display = ','.join(data_dict["display"])
merge_display_size = ','.join(data_dict["display_size"])
merge_display_resolution = ','.join(data_dict["display_resolution"])
data_dict["display"] = merge_display
data_dict["display_size"] = merge_display_size
data_dict["display_resolution"] = merge_display_resolution
except Exception as e:
data_dict["display"] = input("无法获取显示器型号,请手动填写:")
data_dict["display_size"] = input("无法获取显示器尺寸,请手动填写:")
data_dict["display_resolution"] = input("无法获取显示器分辨率,请手动填写:")
print("正在获取电脑系统版本……")
try:
for system_edition in c.Win32_OperatingSystem():
data_dict["hostname"] = system_edition.CSName
data_dict["system_edition"] = system_edition.Caption + system_edition.Version
# data_dict["system_version"] = system_edition.Version
data_dict["system_bit"] = system_edition.OSArchitecture
data_dict["system_user"] = system_edition.RegisteredUser
except Exception as s:
print("系统版本获取不到")
#print("开始上传到简道云……")
# print(data_dict)
jdy_url = "https://api.jiandaoyun.com/api/v2/app/0000000/entry/00000000/data_create"
# 两排00000000 代表 模块id 和 表单id,简道云后台获取
headers = {'Content-Type': 'application/json;charset=utf-8',
'Authorization': 'Bearer ' + '0000000'} # 这一排00000代表appkey,简道云后台获取
# data_computer 是按照简道云的数据格式 组装出来的数据
data_computer = {
'data': {
"name": {"value": data_dict['name']},
"department": {"value": data_dict['department']},
"cpu": {"value": data_dict['cpu']},
"disk": {"value": data_dict['disk']},
"memory": {"value": data_dict['memory']},
"video_card": {"value": data_dict['video_card']},
"ip_address": {"value": data_dict['ip_address']},
"mac_address": {"value": data_dict['mac_address']},
"main_board": {"value": data_dict['main_board']},
"display": {"value": data_dict['display']},
"display_size": {"value": data_dict['display_size']},
"display_resolution": {"value": data_dict['display_resolution']},
"hostname": {"value": data_dict['hostname']},
"system_edition": {"value": data_dict['system_edition']},
"system_bit": {"value": data_dict['system_bit']},
"system_user": {"value": data_dict['system_user']},
"memory_space": {"value": data_dict['memory_space']},
"disk_space": {"value": data_dict['disk_space']}
}
}
# print(data_computer) # 打印上传数据
pause = input("回车后上传")
up_data = requests.post(jdy_url, json.dumps(data_computer), headers=headers, verify=False)
# 自己测试的话,直接注释上一行,打印出来data_computer
print(up_data) # 显示上传信息
pause = input("回车后退出")