网速上传下载流量监测工具尝试

闲来无事,看到加速器的网速上传下载变动,试着自己做个小工具

先是命令指示符版的:

复制代码
@echo off
chcp 65001 >nul
REM 切换为UTF-8编码
:loop

for /f "skip=2 tokens=2 delims=," %%a in ('typeperf "\Network Interface(802.11n USB Wireless LAN Card)\Bytes Received/sec" -sc 1') do (
set "recv=%%~a"
goto :gotRecv
)
:gotRecv
REM 获取下载速率(skip=2:跳过前两行,tokens=2:提取第二个字段,delims=,:指定逗号为字段分隔符)

for /f "skip=2 tokens=2 delims=," %%b in ('typeperf "\Network Interface(802.11n USB Wireless LAN Card)\Bytes Sent/sec" -sc 1') do (
set "sent=%%~b"
goto :gotSent
)
:gotSent
REM 获取上传速率

for /f "delims=." %%i in ("%recv%") do set recv_int=%%i
set /a recvKB=recv_int/1024
for /f "delims=." %%i in ("%sent%") do set sent_int=%%i
set /a sentKB=sent_int/1024
REM 转换为KB/s格式

echo recv↓:%recvKB% KB/s sent↑:%sentKB% KB/s

timeout /t 1 /nobreak >nul
REM 等待1秒
goto loop

效果:

(在自己电脑上操作时把'802.11n USB Wireless LAN Card'换成'*',如果电脑上有多个网卡,建议换成正在使用流量的那个网卡,这里我有有限和无线网卡,一开始搞的时候全用'*'导致流量一直为0,也算一个教训..)

这个黑框框版本有点不友好,并且只能获得整数位KB/s,我又弄了个python版的

python 复制代码
from tkinter import *
import tkinter as tk
import psutil
import time

def main():

    sentLast=psutil.net_io_counters().bytes_sent#获取上传流量
    recvLast=psutil.net_io_counters().bytes_recv#获取下载流量
    lastTime=time.time()

    def updateNetwork():
        nonlocal sentLast,recvLast,lastTime
        sentNow=psutil.net_io_counters().bytes_sent
        recvNow=psutil.net_io_counters().bytes_recv
        nowTime=time.time()
        ##获取新数据
        deltaTime=nowTime-lastTime#获取时间差
        if deltaTime>0:
            up=(sentNow-sentLast)/1024/deltaTime
            down=(recvNow-recvLast)/1024/deltaTime
            upDetail.config(text='{0:.2f}KB/s'.format(up))
            downDetail.config(text='{0:.2f}KB/s'.format(down))
            ##更新当次网速
            sentLast=sentNow
            recvLast=recvNow
            lastTime=nowTime
            ##更新上一次记录
        root.after(1000,updateNetwork)
        
            
    root = tk.Tk()
    root.title("网速监测")
    root.geometry('300x200')

    upLabel = Label(root,text='↑')
    upDetail = Label(root,text='0.00KB/s')

    downLabel = Label(root,text='↓')
    downDetail = Label(root,text='0.00KB/s')

    upLabel.place(x=20,y=20)
    upDetail.place(x=100,y=20)
    downLabel.place(x=20,y=100)
    downDetail.place(x=100,y=100)

    updateNetwork()#开始刷新
        
    root.mainloop()
 
if __name__ == "__main__":
    main()

这下舒服了

tip:我机子上两个程序都能正常运行,如有bug,请各位大佬多多指教!

相关推荐
如竟没有火炬9 分钟前
最大矩阵——单调栈
数据结构·python·线性代数·算法·leetcode·矩阵
Multipath71211 分钟前
无人区不掉线:多链路聚合路由,为环塔拉力赛筑起“空中通讯走廊”
网络·5g·安全·无人机·实时音视频
阳区欠18 分钟前
【LangChain】LLM基础介绍
开发语言·python·langchain
Cosolar19 分钟前
保姆级 CrewAI 教程:从零构建多智能体协作系统
人工智能·python·架构
GDAL34 分钟前
使用 uv 管理 Python 版本
python·uv·版本
真实的菜35 分钟前
Redis 从入门到精通(十二):典型业务场景实战 —— 排行榜、限流器、秒杀系统、Session 共享
数据库·redis·python
cup111 小时前
[开源] Meta Assistant / 告别命令行,我为一堆 Python 脚本做了一个 Windows 任务栏的“家”
windows·python·工具·nuitka·脚本运行
小小编程路2 小时前
Python 还有容器类型互转、进制转换、字符编码转换
开发语言·windows·python
上海云盾-小余2 小时前
接口高频恶意刷取怎么防?网关限流搭配 WAF 联合防护方案
网络·安全
Samooyou2 小时前
RAG项目案例--02在线检索&过滤流水线
人工智能·python·ai·全文检索·检索