trae开发的win10端口占用检测工具
前言
首先,强烈安利字节开发的工具:https://www.trae.com.cn/ 以下代码均由此工具生成。
linux 中可以使用 lsof -i:端口号 查看端口占用进程,并使用kill指令杀死进程,但是对于不熟悉windows命令行的同学,
如果运行代码时,一直提示某个端口被占用,使用netstat -ano|findstr LISTEN 虽然也可以查到端口占用进程,但是taskkill 指令还是挺麻烦的。
下面是用trae开发的基于python wxpython库的端口占用检测界面工具。
正文
- 主程序 port_monitor.py
import wx
import subprocess
import re
class PortMonitorFrame(wx.Frame):
def __init__(self, parent, title):
super(PortMonitorFrame, self).__init__(parent, title=title, size=(600, 400))
self.InitUI()
self.Centre()
self.is_scanning = False
def InitUI(self):
panel = wx.Panel(self)
vbox = wx.BoxSizer(wx.VERTICAL)
hbox1 = wx.BoxSizer(wx.HORIZONTAL)
self.port_input = wx.TextCtrl(panel)
hbox1.Add(wx.StaticText(panel, label='端口号:'), flag=wx.RIGHT, border=8)
hbox1.Add(self.port_input, proportion=1)
hbox2 = wx.BoxSizer(wx.HORIZONTAL)
self.check_btn = wx.Button(panel, label='检测端口占用')
self.kill_btn = wx.Button(panel, label='终止占用进程')
self.check_btn.Bind(wx.EVT_BUTTON, self.on_check_port)
self.kill_btn.Bind(wx.EVT_BUTTON, self.on_kill_process)
hbox2.Add(self.check_btn, flag=wx.RIGHT, border=8)
hbox2.Add(self.kill_btn)
self.result_list = wx.ListCtrl(panel, style=wx.LC_REPORT)
self.result_list.InsertColumn(0, 'PID', width=100)
self.result_list.InsertColumn(1, '程序名称', width=200)
self.result_list.InsertColumn(2, '状态', width=150)
self.result_list.InsertColumn(3, '是否为主程序', width=100)
self.result_list.InsertColumn(4, '程序路径', width=250)
vbox.Add(hbox1, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
vbox.Add(hbox2, flag=wx.EXPAND|wx.ALL, border=10)
vbox.Add(self.result_list, proportion=1, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.BOTTOM, border=10)
panel.SetSizer(vbox)
def on_check_port(self, event):
port = self.port_input.GetValue().strip()
if not port.isdigit():
wx.MessageBox('请输入有效的端口号', '错误', wx.ICON_ERROR)
return
if self.is_scanning:
wx.MessageBox('扫描正在进行中', '提示', wx.ICON_INFORMATION)
return
self.result_list.DeleteAllItems()
self.is_scanning = True
wx.CallAfter(self.check_btn.Enable, False)
def scan_task():
try:
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
output = subprocess.check_output(['netstat', '-ano'], text=True, stderr=subprocess.STDOUT, startupinfo=startupinfo)
pattern = re.compile(r'TCP\s+.*?:{}\s+.*?\s+(\d+)'.format(port))
pids = pattern.findall(output)
if not pids:
wx.CallAfter(wx.MessageBox, '端口未被占用', '提示', wx.ICON_INFORMATION)
else:
for pid in set(pids):
if not self.is_scanning: # 扫描被中断时退出循环
break
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
task_info = subprocess.check_output(['tasklist', '/fi', f'pid eq {pid}'], text=True, stderr=subprocess.STDOUT, startupinfo=startupinfo)
proc_name = re.search(r'(.+?)\s+\d+\s+Console', task_info)
proc_name = proc_name.group(1).strip() if proc_name else '未知程序'
# 解析netstat输出获取PID对应的连接状态
pattern_detail = re.compile(r'TCP\s+.*?:{}\s+.*?\s+(\S+)\s+({})'.format(port, pid))
status_match = pattern_detail.search(output)
is_listening = status_match.group(1) == 'LISTENING' if status_match else False
def update_ui(pid, proc_name, is_listening, proc_path):
index = self.result_list.InsertItem(self.result_list.GetItemCount(), pid)
self.result_list.SetItem(index, 1, proc_name)
self.result_list.SetItem(index, 2, '运行中')
self.result_list.SetItem(index, 3, '是' if is_listening else '')
self.result_list.SetItem(index, 4, proc_path)
if is_listening:
self.result_list.SetItemTextColour(index, wx.RED)
# 获取程序路径(使用wmic命令)
try:
proc_path_output = subprocess.check_output(['wmic', 'process', 'where', f'processid={pid}', 'get', 'executablepath'], text=True, stderr=subprocess.STDOUT, startupinfo=startupinfo)
proc_path_match = re.search(r'ExecutablePath\s+(.+?)\s*$', proc_path_output, re.MULTILINE)
proc_path = proc_path_match.group(1).strip() if proc_path_match else '未知路径'
except:
proc_path = '未知路径'
wx.CallAfter(update_ui, pid, proc_name, is_listening, proc_path)
except subprocess.CalledProcessError as e:
wx.CallAfter(wx.MessageBox, f'执行命令失败:{e.output}', '错误', wx.ICON_ERROR)
finally:
self.is_scanning = False
wx.CallAfter(self.check_btn.Enable, True)
import threading
threading.Thread(target=scan_task, daemon=True).start()
def on_kill_process(self, event):
if self.is_scanning:
wx.MessageBox('扫描未完成,请稍后', '提示', wx.ICON_INFORMATION)
return
selected = self.result_list.GetFirstSelected()
if selected == -1:
wx.MessageBox('请选择要终止的进程', '提示', wx.ICON_INFORMATION)
return
pid = self.result_list.GetItemText(selected)
try:
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
output = subprocess.check_output(['taskkill', '/F', '/PID', pid], text=True, stderr=subprocess.STDOUT, startupinfo=startupinfo)
self.result_list.DeleteItem(selected)
wx.MessageBox('进程终止成功', '提示', wx.ICON_INFORMATION)
except subprocess.CalledProcessError as e:
wx.MessageBox(f'终止进程失败:{e.output}', '错误', wx.ICON_ERROR)
if __name__ == '__main__':
app = wx.App()
frame = PortMonitorFrame(None, '端口监控工具')
frame.Show()
app.MainLoop()
- 编译成二进制脚本
这里使用了 nuitka 工具,将py脚本编译成c++的可执行程序。也可以使用 pyinstaller (pyinstaller 编译成的程序会在用户目录留下缓存文件)
pip install nuitka
批处理文件
build.bat
@echo off
nuitka --standalone --onefile --windows-console-mode=disable port_monitor.py
echo 打包完成,可执行文件在dist目录下
pause
运行后,生成 port_monitor.exe
- 运行
直接python脚本运行
python port_monitor.py
exe运行
port_monitor.exe
运行效果展示:

trae开发的win10端口占用检测工具的更多相关文章
- windows端口占用处理工具
一.描述 笔者在最近使用tomcat时,老是会遇到这种端口占用的问题,便写了这个小的exe,用于解决windows下的端口占用问题. 好吧,其实是我实在记不住CMD下的那几行命令.这玩意的实现比较简单 ...
- windows下端口占用处理工具
一.通用方法 经常,我们在启动应用的时候发现系统需要的端口被别的程序占用,笔者在最近使用tomcat时,老是会遇到这种端口占用的问题,如何知道谁占有了我们需要的端口,很多人都比较头疼,以下是通用方法: ...
- 【windows7】解决IIS 80端口占用问题(亲测)
1.默认你win机器已经安装并启用了80端口 2.现在你要安装并启用apache服务器 3.首先进行80端口占用检测:netstat -aon|findstr 80 4.找到进程号为404的服务名称, ...
- Cocos开发中性能优化工具介绍之Visual Studio内存泄漏检测工具——Visual Leak Detector
那么在Windows下有什么好的内存泄漏检测工具呢?微软提供Visual Studio开发工具本身没有什么太好的内存泄漏检测功能,我们可以使用第三方工具Visual Leak Detector(以下简 ...
- Xenu-web开发死链接检测工具应用
Xenu 是一款深受业界好评,并被广泛使用的死链接检测工具. 时常检测网站并排除死链接,对网站的SEO 非常重要,因为大量死链接存在会降低用户和搜索引擎对网站的信任,web程序开发人员还可通过其找到死 ...
- C#实时检测端口占用情况
在TCP/IP协议中,服务端需要去监听客户端的端口,开始监听,我们需要检测使用的端口是否被占用,获取系统当前使用的所有端口号,用此端口进行匹配即可. 代码如下 internal static Bool ...
- 基于端口的弱口令检测工具--iscan
亲手打造了一款弱口令检测工具,用Python编写,主要可以用于内网渗透.弱口令检测等方面,目前集成了常见端口服务,包含 系统弱口令:ftp.ssh.telnet.ipc$ 数据库弱口令:mssql.m ...
- Metasploit是一款开源的安全漏洞检测工具,
Metasploit是一款开源的安全漏洞检测工具,可以帮助安全和IT专业人士识别安全性问题,验证漏洞的缓解措施,并管理专家驱动的安全性进行评估,适合于需要核实漏洞的安全专家,同时也适合于强大进攻能力的 ...
- Android内存泄露---检测工具篇
内存使用是程序开发无法回避的一个问题.如果我们毫不在意肆意使用,总有一天会为此还账,且痛不欲生...所以应当防患于未然,把内存使用细化到平时的每一行代码中. 内存使用概念较大,本篇先讲对已有app如何 ...
- Linux中系统检测工具top命令
Linux中系统检测工具top命令 本文转自:https://www.cnblogs.com/zhoug2020/p/6336453.html 首先介绍top中一些字段的含义: VIRT:virtua ...
随机推荐
- LVGL 8.3.0常用函数快速使用
LVGL 8.3.0使用快速上手教程(使用篇) 定义页面通用样式style // 创建页面样式 static lv_style_t style; lv_style_init(&style); ...
- MybatisPlus - [04] 分页
limit m,n.PageHelper.MyBatisPlus分页插件 001 || MybatisPlus分页插件 (1)引入maven依赖 <dependency> <grou ...
- implicit和explicit求解器的一点比较
implicit procedure和explicit procedure的比较 abaqus有两个求解器:standard和 explicit求解器.两个求解器在很多方面都有所差异:单元类型/材料行 ...
- sap 管理--企业解决方案 -设备管理
1.什么是sap 管理 2.设备管理管的是什么 3.设备的几种状态 4.设备bom(物料清单) 5.测量点计数器 1.什么是sap 管理 System Applications and Product ...
- docker学习 容器的启动过程
这一节我们来稍微了解下docker原理性的东西1 docker run -i -t ubuntu /bin/bash输入上面这行命令,启动一个ubuntu容器时,到底发生了什么?大致过程可以用下 ...
- 万字长文详解SIFT特征提取
本文对 SIFT 算法进行了详细梳理.SIFT即尺度不变特征变换(Scale-Invariant Feature Transform),是一种用于检测和描述图像局部特征的算法.该算法对图像的尺度和旋转 ...
- BUUCTF---BabyRSA(简单求解n)
题目 p+q : 0x1232fecb92adead91613e7d9ae5e36fe6bb765317d6ed38ad890b4073539a6231a6620584cea5730b5af83a3e ...
- 【集合分组利器】Java通用集合分组方案
Java通用集合分组实现方案详解:从基础到高级实践 在Java开发中,对集合中的元素按照特定属性进行分组是一项常见而重要的操作.本文将全面介绍Java中实现集合分组的多种方案,从基础实现到高级用法,并 ...
- 【Docker】容器数据卷
Docker容器数据卷 第一次听说这个名字,我一直以为是数据卷(juǎn),后来查看官方英文文档的"volume"这个单词的时候,我才反应过来,这是容器数据卷(juàn),书卷的卷 ...
- 2020年devops的7个发展趋势
2020年devops的7个发展趋势 2019年对DevOps从业者来说是激动人心的一年,DevOps继续快速增长.大多数组织都在执行或评估他们的DevOps策略.那么,到2020年,DevOps.基 ...