MonitorTCP/UDP端口活动而无需结合

问题描述 投票:0回答:1
这只是Python中的基本实现。如果指定端口上没有活动10秒钟,则会暂停该过程。我不确定它是否恢复过程。就像隐含的是,Windows不会将数据包发送到暂停的过程中,因此没有网络活动可以唤醒该过程。一个过程是在暂停状态下的资源组。无论如何...

记住使用python -m pip install psutil或sudo apt install python3 -psutil的设备安装psutil.
network-programming tcp udp
1个回答
0
投票
import os import signal import psutil import platform import time def check_connections(port): """Check if there are active connections on the given port.""" for conn in psutil.net_connections(kind='inet'): if conn.laddr.port == port and conn.status == 'ESTABLISHED': return True # Active connections exist return False # No active connections def pause_process(pid): """Pause the process.""" if platform.system() == "Windows": psutil.Process(pid).suspend() else: os.kill(pid, signal.SIGSTOP) print(f"Process {pid} paused.") def resume_process(pid): """Resume the process.""" if platform.system() == "Windows": psutil.Process(pid).resume() else: os.kill(pid, signal.SIGCONT) print(f"Process {pid} resumed.") def monitor_process(pid, port, delay=10): """Monitor the process and pause it after a period of inactivity.""" inactive_since = None while True: if check_connections(port): if inactive_since is not None: print("Active connection detected, resuming process.") inactive_since = None resume_process(pid) else: if inactive_since is None: inactive_since = time.time() elif time.time() - inactive_since >= delay: print(f"No connections for {delay} seconds, pausing process.") pause_process(pid) inactive_since = None # Reset the timer after pausing time.sleep(1) # Check every second if __name__ == "__main__": pid = int(input("Enter a process ID to watch: ")) port = int(input("Enter a port to watch: ")) monitor_process(pid, port)

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.