Python實現美化版端口進程管理工具
更新時間:2025年03月04日 08:49:18 作者:探客白澤
這篇文章主要為大家詳細介紹了如何使用Python實現一個美化版的端口進程管理工具,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下
1. 簡介
一個基于端口管理和進程管理的GUI工具,它可以顯示當前系統上所有開放的端口信息,并且允許用戶對選中的進程進行操作(如結束進程、定位進程文件夾路徑、復制相關信息到剪貼板等)。
詳細信息可參考原文信息:基于Python編寫端口進程管理工具
2. 運行效果
3. 相關源碼
import sys import psutil import os import pyperclip from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QTableWidget, QTableWidgetItem, QMenu, QAction, QComboBox, QLineEdit, QPushButton, QMessageBox, QHeaderView from PyQt5.QtCore import Qt from PyQt5.QtGui import QIcon # 獲取本機所有開放的端口及對應的進程信息 def get_open_ports(): open_ports = [] for conn in psutil.net_connections(kind='inet'): if conn.status != 'LISTEN': continue pid = conn.pid if conn.type == 1: # TCP協議 protocol = 'TCP' elif conn.type == 2: # UDP協議 protocol = 'UDP' else: protocol = 'N/A' try: process = psutil.Process(pid) process_name = process.name() exe_path = process.exe() except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): process_name = "N/A" exe_path = "N/A" open_ports.append({ 'Port': conn.laddr.port, 'PID': pid, 'Process Name': process_name, 'Protocol': protocol, 'Path': exe_path }) return open_ports # 根據端口號查詢對應進程的信息 def search_by_port(port): open_ports = get_open_ports() for port_info in open_ports: if port_info['Port'] == port: return port_info return None # 根據進程名稱查詢對應的端口信息 def search_by_process_name(name): open_ports = get_open_ports() result = [] for port_info in open_ports: if name.lower() in port_info['Process Name'].lower(): result.append(port_info) return result # 根據PID查詢對應的端口信息 def search_by_pid(pid): open_ports = get_open_ports() for port_info in open_ports: if port_info['PID'] == pid: return port_info return None # 結束進程 def kill_process(pid): try: process = psutil.Process(pid) process.terminate() # 發(fā)送 terminate 信號 process.wait() # 等待進程結束 return True except (psutil.NoSuchProcess, psutil.AccessDenied): return False # 定位進程文件夾路徑 def open_process_folder(exe_path): if exe_path and os.path.exists(exe_path): folder_path = os.path.dirname(exe_path) os.startfile(folder_path) # 打開文件夾 return True return False # 復制到剪貼板的功能 def copy_to_clipboard(text): pyperclip.copy(text) # 使用 pyperclip 庫復制文本 class PortProcessManager(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("端口進程管理工具") self.setWindowIcon(QIcon(os.path.join(os.getcwd(), 'icon.ico'))) self.setGeometry(100, 100, 900, 600) #self.setWindowOpacity(0.8) # 設置窗口透明度為0.8 # 主窗口布局 self.main_widget = QWidget() self.setCentralWidget(self.main_widget) layout = QVBoxLayout(self.main_widget) # 搜索框布局 search_layout = QHBoxLayout() self.search_type_combo = QComboBox() self.search_type_combo.addItems(["端口號", "進程名稱", "PID"]) self.search_type_combo.setCurrentIndex(0) search_layout.addWidget(self.search_type_combo) self.search_input = QLineEdit() search_layout.addWidget(self.search_input) self.search_button = QPushButton("查 詢") self.search_button.clicked.connect(self.search) search_layout.addWidget(self.search_button) self.refresh_button = QPushButton("刷新列表") self.refresh_button.clicked.connect(self.refresh_list) search_layout.addWidget(self.refresh_button) layout.addLayout(search_layout) # 表格展示 self.table = QTableWidget() self.table.setColumnCount(5) self.table.setHorizontalHeaderLabels(["PID", "協議", "端口", "進程名稱", "相關路徑"]) self.table.setEditTriggers(QTableWidget.NoEditTriggers) # 禁止編輯 self.table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) # 列寬自適應 layout.addWidget(self.table) # 右鍵菜單 self.table.setContextMenuPolicy(Qt.CustomContextMenu) self.table.customContextMenuRequested.connect(self.open_right_menu) # 設置樣式 self.set_styles() # 初始化數據 self.refresh_list() def set_styles(self): # 設置字體為 "Segoe UI" style = """ * { font-family: "Segoe UI"; } QPushButton { background-color: #2dd8e1; color: white; border: 1px solid #00a7c3; padding: 5px; } QHeaderView::section { background-color: #2dd8e1; color: white; padding: 5px; } """ self.setStyleSheet(style) def refresh_list(self): self.table.setRowCount(0) open_ports = get_open_ports() if not open_ports: QMessageBox.information(self, "沒有找到端口", "沒有開放的端口或無法獲取端口信息。") return for port_info in open_ports: row_position = self.table.rowCount() self.table.insertRow(row_position) self.table.setItem(row_position, 0, QTableWidgetItem(str(port_info['PID']))) self.table.setItem(row_position, 1, QTableWidgetItem(port_info['Protocol'])) self.table.setItem(row_position, 2, QTableWidgetItem(str(port_info['Port']))) self.table.setItem(row_position, 3, QTableWidgetItem(port_info['Process Name'])) self.table.setItem(row_position, 4, QTableWidgetItem(port_info['Path'])) def search(self): search_value = self.search_input.text() search_type = self.search_type_combo.currentText() if not search_value: QMessageBox.warning(self, "輸入錯誤", "請輸入查詢內容!") return self.table.setRowCount(0) if search_type == "端口號": try: port = int(search_value) port_info = search_by_port(port) if port_info: self.add_row(port_info) else: QMessageBox.information(self, "未找到", f"未找到端口 {port} 對應的進程。") except ValueError: QMessageBox.warning(self, "輸入錯誤", "請輸入有效的端口號。") elif search_type == "進程名稱": result = search_by_process_name(search_value) if result: for port_info in result: self.add_row(port_info) else: QMessageBox.information(self, "未找到", f"未找到進程名稱包含 {search_value} 的記錄。") elif search_type == "PID": try: pid = int(search_value) port_info = search_by_pid(pid) if port_info: self.add_row(port_info) else: QMessageBox.information(self, "未找到", f"未找到PID {pid} 對應的進程。") except ValueError: QMessageBox.warning(self, "輸入錯誤", "請輸入有效的PID。") def add_row(self, port_info): row_position = self.table.rowCount() self.table.insertRow(row_position) self.table.setItem(row_position, 0, QTableWidgetItem(str(port_info['PID']))) self.table.setItem(row_position, 1, QTableWidgetItem(port_info['Protocol'])) self.table.setItem(row_position, 2, QTableWidgetItem(str(port_info['Port']))) self.table.setItem(row_position, 3, QTableWidgetItem(port_info['Process Name'])) self.table.setItem(row_position, 4, QTableWidgetItem(port_info['Path'])) def open_right_menu(self, pos): selected_item = self.table.itemAt(pos) if selected_item: row = selected_item.row() pid = self.table.item(row, 0).text() port = self.table.item(row, 2).text() process_name = self.table.item(row, 3).text() exe_path = self.table.item(row, 4).text() menu = QMenu(self) kill_action = QAction("結束進程", self) kill_action.triggered.connect(lambda: self.kill_process(int(pid))) menu.addAction(kill_action) folder_action = QAction("定位進程文件夾路徑", self) folder_action.triggered.connect(lambda: self.open_folder(exe_path)) menu.addAction(folder_action) copy_pid_action = QAction("復制PID", self) copy_pid_action.triggered.connect(lambda: copy_to_clipboard(pid)) menu.addAction(copy_pid_action) copy_port_action = QAction("復制端口號", self) copy_port_action.triggered.connect(lambda: copy_to_clipboard(port)) menu.addAction(copy_port_action) copy_process_action = QAction("復制進程名稱", self) copy_process_action.triggered.connect(lambda: copy_to_clipboard(process_name)) menu.addAction(copy_process_action) copy_path_action = QAction("復制相關路徑", self) copy_path_action.triggered.connect(lambda: copy_to_clipboard(exe_path)) menu.addAction(copy_path_action) menu.exec_(self.table.viewport().mapToGlobal(pos)) def kill_process(self, pid): if kill_process(pid): QMessageBox.information(self, "結束進程", f"進程 (PID: {pid}) 已被成功結束。") else: QMessageBox.warning(self, "錯誤", "無法結束該進程,可能沒有權限。") def open_folder(self, exe_path): if open_process_folder(exe_path): QMessageBox.information(self, "打開文件夾", "已成功定位進程文件夾。") else: QMessageBox.warning(self, "錯誤", "無法找到進程文件路徑。") def keyPressEvent(self, event): if event.key() == Qt.Key_Return: # 檢測到回車鍵 self.search() # 執(zhí)行查詢操作 else: super().keyPressEvent(event) if __name__ == "__main__": app = QApplication(sys.argv) window = PortProcessManager() window.show() sys.exit(app.exec_())
到此這篇關于Python實現美化版端口進程管理工具的文章就介紹到這了,更多相關Python端口進程管理內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python中使用zip函數出現<zip object at 0x02A9E418>錯誤的原因
這篇文章主要介紹了python中使用zip函數出現<zip object at 0x02A9E418>錯誤的原因分析及解決方法,需要的朋友可以參考下2018-09-09Django通過dwebsocket實現websocket的例子
今天小編就為大家分享一篇Django通過dwebsocket實現websocket的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11