Python制作一個(gè)系統(tǒng)信息采集工具
圖樣
原碼
# 系統(tǒng)信息采集工具 # 2024-12-16 # 作者:Hoye # 版本:1.0 # 功能:采集系統(tǒng)信息并保存到文件 # 使用方法: # 1. 運(yùn)行程序 # 2. 點(diǎn)擊“采集系統(tǒng)信息”按鈕 # 3. 等待信息采集完成 # 4. 選擇保存位置 # 5. 點(diǎn)擊“保存信息”按鈕 import win32print import socket import subprocess import win32com.shell.shell as shell import win32com.shell.shellcon as shellcon import wmi from datetime import date import tkinter as tk from tkinter import ttk, messagebox, scrolledtext, filedialog from threading import Thread import os class SystemInfoApp: def __init__(self, root): self.root = root self.root.title("藍(lán)動(dòng)力電腦-系統(tǒng)信息采集工具") self.root.geometry("800x600") # 設(shè)置整體樣式 style = ttk.Style() style.configure('TButton', padding=5) style.configure('TLabel', padding=5) self.create_widgets() def create_widgets(self): # 創(chuàng)建主框架 main_frame = ttk.Frame(self.root, padding="10") main_frame.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S)) # 創(chuàng)建按鈕框架 button_frame = ttk.Frame(main_frame) button_frame.grid(row=0, column=0, pady=5, sticky=tk.W) # 采集按鈕 self.collect_btn = ttk.Button( button_frame, text="采集系統(tǒng)信息", command=self.start_collection ) self.collect_btn.grid(row=0, column=0, padx=5) # 保存按鈕 self.save_btn = ttk.Button( button_frame, text="保存信息", command=self.save_info ) self.save_btn.grid(row=0, column=1, padx=5) # 進(jìn)度條 self.progress = ttk.Progressbar( main_frame, orient=tk.HORIZONTAL, length=300, mode='determinate' ) self.progress.grid(row=1, column=0, pady=5, sticky=tk.EW) # 信息顯示區(qū)域 self.info_text = scrolledtext.ScrolledText( main_frame, width=80, height=30, wrap=tk.WORD ) self.info_text.grid(row=2, column=0, pady=5, sticky=(tk.W, tk.E, tk.N, tk.S)) # 狀態(tài)標(biāo)簽 self.status_label = ttk.Label(main_frame, text="就緒") self.status_label.grid(row=3, column=0, pady=5, sticky=tk.W) # 設(shè)置列和行的權(quán)重 self.root.columnconfigure(0, weight=1) self.root.rowconfigure(0, weight=1) main_frame.columnconfigure(0, weight=1) main_frame.rowconfigure(2, weight=1) def get_basic_info(self): """獲取基本系統(tǒng)信息""" info = [] self.update_status("正在獲取基本系統(tǒng)信息...") computer_name = socket.gethostname() try: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(("8.8.8.8", 80)) ip_address = s.getsockname()[0] s.close() except Exception as e: ip_address = "無(wú)法獲取IP地址" info.extend([ "=== 基本系統(tǒng)信息 ===", f"計(jì)算機(jī)名: {computer_name}", f"IP 地址: {ip_address}", "" ]) return info def get_printer_info(self): """獲取打印機(jī)信息""" self.update_status("正在獲取打印機(jī)信息...") try: printers = win32print.EnumPrinters(win32print.PRINTER_ENUM_LOCAL, None, 1) info = ["=== 打印機(jī)信息 ==="] info.extend([f"打印機(jī)名稱: {printer[2]}" for printer in printers]) info.append("") return info except Exception: return ["=== 打印機(jī)信息 ===", "無(wú)法獲取打印機(jī)信息", ""] def get_hardware_info(self): """獲取硬件信息""" self.update_status("正在獲取硬件信息...") try: c = wmi.WMI() info = [] # 計(jì)算機(jī)系統(tǒng)信息 info.append("=== 計(jì)算機(jī)系統(tǒng)信息 ===") for computer in c.Win32_ComputerSystem(): info.extend([ f"計(jì)算機(jī)名稱: {computer.Name}", f"制造商: {computer.Manufacturer}", f"型號(hào): {computer.Model}", "" ]) # CPU信息 info.append("=== CPU信息 ===") for processor in c.Win32_Processor(): info.extend([ f"CPU型號(hào): {processor.Name}", f"CPU ID: {processor.ProcessorId}", f"CPU核心數(shù): {processor.NumberOfCores}", f"CPU線程數(shù): {processor.NumberOfLogicalProcessors}", "" ]) # 硬盤(pán)信息 info.append("=== 硬盤(pán)信息 ===") for disk in c.Win32_DiskDrive(): size_gb = round(int(disk.Size or 0) / (1024**3), 2) info.extend([ f"硬盤(pán)型號(hào): {disk.Model}", f"硬盤(pán)序列號(hào): {disk.SerialNumber}", f"硬盤(pán)容量: {size_gb}GB", "" ]) return info except Exception as e: return ["=== 硬件信息 ===", f"獲取硬件信息出錯(cuò): {str(e)}", ""] def get_folder_paths(self): """獲取重要文件夾路徑""" self.update_status("正在獲取系統(tǒng)文件夾路徑...") try: desktop = shell.SHGetFolderPath(0, shellcon.CSIDL_DESKTOP, 0, 0) documents = shell.SHGetFolderPath(0, shellcon.CSIDL_PERSONAL, 0, 0) info = [ "=== 系統(tǒng)文件夾路徑 ===", f"桌面位置: {desktop}", f"我的文檔位置: {documents}", "" ] return info except Exception as e: return ["=== 系統(tǒng)文件夾路徑 ===", "無(wú)法獲取文件夾路徑", str(e), ""] def get_network_shares(self): """獲取網(wǎng)絡(luò)共享信息""" self.update_status("正在獲取網(wǎng)絡(luò)共享信息...") try: output = subprocess.check_output("net share", shell=True, text=True) info = [ "=== 網(wǎng)絡(luò)共享信息 ===", output, "" ] return info except subprocess.CalledProcessError as e: return ["=== 網(wǎng)絡(luò)共享信息 ===", f"獲取網(wǎng)絡(luò)共享信息出錯(cuò): {str(e)}", ""] def collect_info(self): """收集所有系統(tǒng)信息""" self.progress["value"] = 0 self.info_text.delete(1.0, tk.END) all_info = [] # 獲取基本信息 all_info.extend(self.get_basic_info()) self.progress["value"] = 20 # 獲取文件夾路徑 all_info.extend(self.get_folder_paths()) self.progress["value"] = 35 # 獲取打印機(jī)信息 all_info.extend(self.get_printer_info()) self.progress["value"] = 50 # 獲取硬件信息 all_info.extend(self.get_hardware_info()) self.progress["value"] = 75 # 獲取網(wǎng)絡(luò)共享信息 all_info.extend(self.get_network_shares()) self.progress["value"] = 100 # 顯示信息 self.info_text.insert(tk.END, "\n".join(all_info)) self.update_status("信息采集完成") def start_collection(self): """在新線程中啟動(dòng)信息收集""" self.collect_btn["state"] = "disabled" Thread(target=self.collect_info, daemon=True).start() self.collect_btn["state"] = "normal" def save_info(self): """保存信息到文件""" if not self.info_text.get(1.0, tk.END).strip(): messagebox.showwarning("警告", "沒(méi)有可保存的信息,請(qǐng)先采集系統(tǒng)信息") return try: date_str = date.today().strftime("%Y-%m-%d") computer_name = socket.gethostname() # 默認(rèn)文件名 default_filename = f"{computer_name}_info_{date_str}.txt" # 創(chuàng)建保存選項(xiàng)按鈕框架 save_window = tk.Toplevel(self.root) save_window.title("選擇保存位置") save_window.geometry("300x200") save_window.transient(self.root) # 設(shè)置為主窗口的子窗口 def save_to_network(): try: network_path = f"\\\\bpc\\temp_g\\{default_filename}" with open(network_path, "w", encoding="utf-8") as f: f.write(self.info_text.get(1.0, tk.END)) messagebox.showinfo("成功", f"信息已保存到網(wǎng)絡(luò)位置:\n{network_path}") save_window.destroy() except Exception as e: messagebox.showerror("錯(cuò)誤", f"保存到網(wǎng)絡(luò)位置失敗:\n{str(e)}") def save_to_local(): initial_dir = os.path.expanduser("~\\Documents") # 默認(rèn)打開(kāi)我的文檔 filename = filedialog.asksaveasfilename( parent=save_window, initialdir=initial_dir, initialfile=default_filename, defaultextension=".txt", filetypes=[("文本文件", "*.txt"), ("所有文件", "*.*")] ) if filename: try: with open(filename, "w", encoding="utf-8") as f: f.write(self.info_text.get(1.0, tk.END)) messagebox.showinfo("成功", f"信息已保存到:\n{filename}") save_window.destroy() except Exception as e: messagebox.showerror("錯(cuò)誤", f"保存文件時(shí)出錯(cuò):\n{str(e)}") def save_to_d_drive(): try: # 確保D盤(pán)存在 if not os.path.exists("D:\\"): messagebox.showerror("錯(cuò)誤", "未找到D盤(pán)") return # 在D盤(pán)根目錄創(chuàng)建文件 d_drive_path = f"D:\\{default_filename}" with open(d_drive_path, "w", encoding="utf-8") as f: f.write(self.info_text.get(1.0, tk.END)) messagebox.showinfo("成功", f"信息已保存到:\n{d_drive_path}") save_window.destroy() except Exception as e: messagebox.showerror("錯(cuò)誤", f"保存到D盤(pán)失?。篭n{str(e)}") # 創(chuàng)建保存選項(xiàng)按鈕 ttk.Label( save_window, text="請(qǐng)選擇保存位置:", padding=10 ).pack() ttk.Button( save_window, text="保存到網(wǎng)絡(luò)位置(\\\\bpc\\temp_g)", command=save_to_network, padding=5 ).pack(pady=5, padx=10, fill=tk.X) ttk.Button( save_window, text="保存到D盤(pán)", command=save_to_d_drive, padding=5 ).pack(pady=5, padx=10, fill=tk.X) ttk.Button( save_window, text="保存到本地位置", command=save_to_local, padding=5 ).pack(pady=5, padx=10, fill=tk.X) # 居中顯示窗口 save_window.update_idletasks() width = save_window.winfo_width() height = save_window.winfo_height() x = (save_window.winfo_screenwidth() // 2) - (width // 2) y = (save_window.winfo_screenheight() // 2) - (height // 2) save_window.geometry(f'{width}x{height}+{x}+{y}') # 模態(tài)對(duì)話框 save_window.grab_set() save_window.focus_set() except Exception as e: messagebox.showerror("錯(cuò)誤", f"創(chuàng)建保存對(duì)話框時(shí)出錯(cuò):\n{str(e)}") def update_status(self, message): """更新?tīng)顟B(tài)欄消息""" self.status_label["text"] = message self.root.update() def main(): root = tk.Tk() app = SystemInfoApp(root) root.mainloop() if __name__ == "__main__": main()
打包 exe
pip install pyinstaller
version_info.txt
# UTF-8 # # For more details about fixed file info 'ffi' see: # http://msdn.microsoft.com/en-us/library/ms646997.aspx VSVersionInfo( ffi=FixedFileInfo( # filevers和prodvers應(yīng)該始終是包含4個(gè)項(xiàng)的元組 filevers=(1, 0, 0, 0), prodvers=(1, 0, 0, 0), # 包含一個(gè)位掩碼,指定文件的有效信息 mask=0x3f, # 包含一個(gè)位掩碼,指定布爾屬性的文件 flags=0x0, # 操作系統(tǒng) OS=0x40004, # 文件類型 fileType=0x1, # 文件子類型 subtype=0x0, # 創(chuàng)建日期 date=(0, 0) ), kids=[ StringFileInfo( [ StringTable( '080404b0', [StringStruct('CompanyName', '藍(lán)動(dòng)力電腦'), StringStruct('FileDescription', '系統(tǒng)信息采集工具'), StringStruct('FileVersion', '1.0.0'), StringStruct('InternalName', 'SystemInfoCollector'), StringStruct('LegalCopyright', 'Copyright (C) 2024 Hoye'), StringStruct('OriginalFilename', 'SystemInfoCollector.exe'), StringStruct('ProductName', '系統(tǒng)信息采集工具'), StringStruct('ProductVersion', '1.0.0')]) ]), VarFileInfo([VarStruct('Translation', [2052, 1200])]) ] )
build.py
import PyInstaller.__main__ import os # 確保當(dāng)前工作目錄正確 current_dir = os.path.dirname(os.path.abspath(__file__)) os.chdir(current_dir) PyInstaller.__main__.run([ '7_系統(tǒng)信息采集工具.py', '--name=SystemInfoCollector', '--windowed', # '--icon=app.ico', # 如果您有圖標(biāo)文件的話 '--version-file=version_info.txt', '--clean', '--noconfirm', '--uac-admin', # 添加管理員權(quán)限 '--onefile', # 打包成單一文件 f'--workpath={os.path.join(current_dir, "build")}', f'--distpath={os.path.join(current_dir, "dist")}', f'--specpath={current_dir}' ])
python build.py
到此這篇關(guān)于Python制作一個(gè)系統(tǒng)信息采集工具的文章就介紹到這了,更多相關(guān)Python系統(tǒng)信息采集內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pytorch下使用LSTM神經(jīng)網(wǎng)絡(luò)寫(xiě)詩(shī)實(shí)例
今天小編就為大家分享一篇pytorch下使用LSTM神經(jīng)網(wǎng)絡(luò)寫(xiě)詩(shī)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01對(duì)python中l(wèi)ist的五種查找方法說(shuō)明
這篇文章主要介紹了對(duì)python中l(wèi)ist的五種查找方法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07使用python opencv對(duì)畸變圖像進(jìn)行矯正的實(shí)現(xiàn)
本文主要介紹了使用python opencv對(duì)畸變圖像進(jìn)行矯正的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05python 實(shí)現(xiàn)查詢Neo4j多節(jié)點(diǎn)的多層關(guān)系
今天小編就為大家分享一篇python 實(shí)現(xiàn)查詢Neo4j多節(jié)點(diǎn)的多層關(guān)系,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12Python通過(guò)調(diào)用mysql存儲(chǔ)過(guò)程實(shí)現(xiàn)更新數(shù)據(jù)功能示例
這篇文章主要介紹了Python通過(guò)調(diào)用mysql存儲(chǔ)過(guò)程實(shí)現(xiàn)更新數(shù)據(jù)功能,結(jié)合實(shí)例形式分析了Python調(diào)用mysql存儲(chǔ)過(guò)程實(shí)現(xiàn)更新數(shù)據(jù)的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2018-04-04Python中Thop庫(kù)的基本用例和參數(shù)說(shuō)明
這篇文章主要給大家介紹了關(guān)于Python中Thop庫(kù)的基本用例和參數(shù)說(shuō)明的相關(guān)資料,THOP是PyTorch非常實(shí)用的一個(gè)第三方庫(kù),可以統(tǒng)計(jì)模型的 FLOPs 和參數(shù)量,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05