可用的网络接口: \Device\NPF{45C931DA-C83C-4AF1-A32E-8515B49A4F09}…
- User
可用的网络接口: \Device\NPF_{45C931DA-C83C-4AF1-A32E-8515B49A4F09} \Device\NPF_{86FCE07C-A06F-416D-B784-D10CB0B53ECD} \Device\NPF_{00A2F2C8-0041-4869-88A9-9E172A1755F8} \Device\NPF_{6FA7FBD5-16B6-4A80-BB20-4B96652FFEE0} \Device\NPF_{0B61559C-493D-458C-A155-CE87F09A88E0} \Device\NPF_{2A954A09-58E5-43EE-96CE-583728C5E994} \Device\NPF_{17AFF98A-37EA-417D-B479-15B682D2CD73} \Device\NPF_Loopback \Device\NPF_{14B9E7E3-6A72-40F2-AA6B-37061CA55397} \Device\NPF_{22D68102-BAA8-4B48-B0A6-3BA1C0FA7229} 选择的接口: \Device\NPF_{45C931DA-C83C-4AF1-A32E-8515B49A4F09} 初始化数据包捕获时出错: There is no current event loop in thread 'Dummy-1'. 选择的接口: \Device\NPF_{0B61559C-493D-458C-A155-CE87F09A88E0} 初始化数据包捕获时出错: There is no current event loop in thread 'Dummy-2'. 选择的接口: \Device\NPF_{0B61559C-493D-458C-A155-CE87F09A88E0} 初始化数据包捕获时出错: There is no current event loop in thread 'Dummy-3'. 。。。。。。。。。 我下面的代码运行后提示上面的错误,怎么办
import sys import psutil import pyshark from PySide6.QtWidgets import ( QApplication, QMainWindow, QVBoxLayout, QWidget, QTableWidget, QTableWidgetItem, QHeaderView, QComboBox, QHBoxLayout, QLabel, QLineEdit, QMessageBox ) from PySide6.QtCore import Qt, QThread, Signal, QTimer, QSize from PySide6.QtGui import QIcon, QPixmap from collections import defaultdict import win32gui import win32ui import win32con from PIL import Image import threading import os import ctypes from scapy.all import get_if_list def make_transparent(image): image = image.convert("RGBA") datas = image.getdata() new_data = [] for item in datas: if item[0] == 0 and item[1] == 0 and item[2] == 0: new_data.append((255, 255, 255, 0)) else: new_data.append(item) image.putdata(new_data) return image
class NetworkMonitorThread(QThread): update_signal = Signal(list)
def __init__(self, interface, parent=None): super().__init__(parent) self.running = True self.process_net_io = defaultdict(lambda: {'bytes_recv': 0, 'bytes_sent': 0, 'name': ''}) self.lock = threading.Lock() self.capture = None self.interface = interface def run(self): if not self.interface: print("未选择网络接口,无法进行数据包捕获。") return try: self.capture = pyshark.LiveCapture(interface=self.interface, bpf_filter='tcp or udp') print(f"开始在接口 {self.interface} 上捕获数据包...") for packet in self.capture.sniff_continuously(): if not self.running: self.capture.close() break try: if 'IP' not in packet: continue src_ip = packet.ip.src dst_ip = packet.ip.dst protocol = packet.transport_layer src_port = packet[packet.transport_layer].srcport dst_port = packet[packet.transport_layer].dstport packet_length = int(packet.length) # 查找与源 IP:port 相关的进程 src_pid = self.get_pid(src_ip, src_port, protocol) if src_pid: with self.lock: self.process_net_io[src_pid]['bytes_sent'] += packet_length # 查找与目标 IP:port 相关的进程 dst_pid = self.get_pid(dst_ip, dst_port, protocol) if dst_pid: with self.lock: self.process_net_io[dst_pid]['bytes_recv'] += packet_length except Exception as e: print(f"处理数据包时出错: {e}") except Exception as e: print(f"初始化数据包捕获时出错: {e}") return # 捕获结束后更新 UI self.update_ui() def get_pid(self, ip, port, protocol): try: for proc in psutil.process_iter(['pid', 'name', 'connections']): for conn in proc.info['connections']: if conn.laddr and conn.laddr.ip == ip and conn.laddr.port == int(port): return proc.info['pid'] return None except (psutil.NoSuchProcess, psutil.AccessDenied): return None def update_ui(self): with self.lock: data = [] for pid, io in self.process_net_io.items(): if io['bytes_recv'] > 0 or io['bytes_sent'] > 0: try: proc = psutil.Process(pid) name = proc.name() data.append(( name, io['bytes_recv'] / 1024, # KB io['bytes_sent'] / 1024, # KB io['bytes_recv'] / (1024 * 1024), # MB io['bytes_sent'] / (1024 * 1024) # MB )) except (psutil.NoSuchProcess, psutil.AccessDenied): continue # 重置统计 self.process_net_io = defaultdict(lambda: {'bytes_recv': 0, 'bytes_sent': 0, 'name': ''}) self.update_signal.emit(data) def stop(self): self.running = False if self.capture: self.capture.close() - Assistant