From 32fcd149392e0be1841c3a9b831e8859613355e1 Mon Sep 17 00:00:00 2001 From: ZhaoSQ <1044239768@qq.com> Date: Mon, 15 Nov 2021 14:09:38 +0800 Subject: [PATCH] Increase access to administrator permissions 1.Use cmd file to double-click windows host.cmd to run the program; 2.Adding the code of python to obtain administrator rights can realize the operation of the cmd window program without administrative rights. --- host_python/windows_host.cmd | 5 +++++ host_python/windows_host.py | 38 ++++++++++++++++++++++++------------ 2 files changed, 31 insertions(+), 12 deletions(-) create mode 100644 host_python/windows_host.cmd diff --git a/host_python/windows_host.cmd b/host_python/windows_host.cmd new file mode 100644 index 0000000..b447fdd --- /dev/null +++ b/host_python/windows_host.cmd @@ -0,0 +1,5 @@ +@echo off +%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit +cd /d "%~dp0" + +start python windows_host.py diff --git a/host_python/windows_host.py b/host_python/windows_host.py index c1eb7ee..85318b5 100644 --- a/host_python/windows_host.py +++ b/host_python/windows_host.py @@ -8,12 +8,13 @@ # Make sure 'OpenHardwareMonitorLib.dll' is in the same folder as this Python script! # # Change your COM port to match that of your COM port! - +from __future__ import print_function import serial import os import time import clr import psutil +import ctypes, sys updateTime = 4 #number of seconds between each update @@ -28,7 +29,7 @@ def sendData(temp, rpm, gpu, free_disk, free_mem, procs): try: - connection = serial.Serial('COM16') # Change this to match your COM port! + connection = serial.Serial('COM35') # Change this to match your COM port! data = temp + ',' + rpm + ',' + str(free_mem) + ',' + str(free_disk) + ',' + gpu + ',' + str(procs) + '/' connection.write(data.encode()) print("Data written", data.encode()) @@ -76,14 +77,27 @@ def parse_sensor(sensor): HardwareHandle = initialize_openhardwaremonitor() -while(1): - fetch_stats(HardwareHandle) - obj_Disk = psutil.disk_usage('c:\\') # Drive letter with double \\ - free_disk = int(obj_Disk.free / (1024.0 ** 3)) - free_mem = (int((psutil.virtual_memory().total - psutil.virtual_memory().used)/ (1024 * 1024))) - proc_counter = 0 - for proc in psutil.process_iter(): - proc_counter += 1 - sendData(cpu_temp, rpm, gpu_temp, free_disk, free_mem, proc_counter) - time.sleep(updateTime) +def is_admin(): + try: + return ctypes.windll.shell32.IsUserAnAdmin() + except: + return False + +if is_admin(): + # 将要运行的代码加到这里 + while(1): + fetch_stats(HardwareHandle) + obj_Disk = psutil.disk_usage('c:\\') # Drive letter with double \\ + free_disk = int(obj_Disk.free / (1024.0 ** 3)) + free_mem = (int((psutil.virtual_memory().total - psutil.virtual_memory().used)/ (1024 * 1024))) + proc_counter = 0 + for proc in psutil.process_iter(): + proc_counter += 1 + sendData(cpu_temp, rpm, gpu_temp, free_disk, free_mem, proc_counter) + time.sleep(updateTime) +else: + if sys.version_info[0] == 3: + ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1) + else:#in python2.x + ctypes.windll.shell32.ShellExecuteW(None, u"runas", unicode(sys.executable), unicode(__file__), None, 1)