Python批量测试IP端口GUI程序(Tkinter)

一、实现样式

批量IP与端口中间用","分割,点击Telnet进行测试,前提是你电脑安装了telnet客户端,Clear按钮用来清空文本框。

二、核心点

1、使用Tkinter来制作桌面GUI页面

2、使用telnetlib模块测试telnet端口

三、困难点

1、测试结果陆续输出到文本框里面去 ,最开始使用重定向将print打印结果输出到文本框,当时结果会一下子全出现在页面上,如果数据量大就会导致程序一直无响应,半天结果不会出来,用户体验感异常不好,这里采用text.insert()将结果输出到文本框,使用延迟sleep,文本框更新的方式将数据一条一条的输出到页面上。

python 复制代码
 output_text.insert(INSERT,f"{host} {port}端口开放 \n")
 time.sleep(0.1)
 output_text.update()

2、文本框键盘无法输入 ,最开始想到是文本框直接 disabled,只读,但是这样的话text.insert()就无法将结果输出到文本框里面去,这里采用禁止键盘输入

python 复制代码
# 键盘无法输入
output_text.bind("<Key>",lambda event:"break")

四、完整代码

python 复制代码
from tkinter import *
import telnetlib
import time

root = Tk()
root.title("telnet批量测试")
# root.iconbitmap("telnet.ico")
root.geometry("800x540")
root.resizable(False, False)
# 保存端口不通的ip:ports
notopenport = []


def telnet(host, port):
    # redirect_stdout_to_tkinter(output_text)
    try:
        #  timeout单位s
        telnetlib.Telnet(host=host, port=port, timeout=1)
        output_text.insert(INSERT,f"{host} {port}端口开放 \n")
        time.sleep(0.1)
        output_text.update()
        output_text.focus_force()
        # print(f"{host} {port}  端口开放")       
    except:
        output_text.insert(INSERT,f"{host} {port}端口未开放 \n")
        notopenport.append(f"{host}:{port}")
        time.sleep(0.1)
        output_text.update()
        output_text.focus_force()
# 测试端口
def for_port():
    hosts = ipInput.get().split(',')
    port_list = portsInput.get().split(',')
    # print('需要测试的端口:')
    # print(hosts)
    output_text.insert(INSERT,'----------开始进行端口测试--------- \n')  
    for host in hosts:
        for port in port_list:
            telnet(host, port)
    output_text.insert(END,'------------端口测试完成----------- \n') 
    output_text.insert(END,'不通的ip地址与端口为: \n')
    output_text.insert(END,notopenport )
      

def clear():
    output_text.delete('1.0', END)

# 布局框架
fr = Frame(root).pack(anchor = 'nw')
# ip输入框
ipLab = Label(fr,text='IP Address:').place(x=30,y=30)
ipInput = Entry(fr,width=70)
ipInput.place(x=110,y=32)
# 端口输入框
portsLab = Label(fr,text='Ports:').place(x=30,y=70)
portsInput = Entry(fr,width=70)
portsInput.place(x=110,y=72)
# 按钮
telnetButton = Button(fr,text='Telnet',command=for_port).place(x=110,y=128)
clearButton = Button(fr,text='Clear',command=clear).place(x=170,y=128)

fr1 = Frame(root,width=72,height=200,bd=4).place(x=110,y=160)
output_text = Text(fr1,width=72)
output_text.place(x=110,y=160)
# 键盘无法输入
output_text.bind("<Key>",lambda event:"break")
# 滑块绑定
scroll = Scrollbar(fr1)
scroll.pack(side=RIGHT,fill=Y)
# 两个控件关联
scroll.config(command=output_text.yview)
output_text.config(yscrollcommand=scroll.set)

# redirect_stdout_to_tkinter(output_text)

mainloop()
相关推荐
暖馒34 分钟前
Modbus应用层协议的深度剖析
网络·网络协议·c#·wpf·智能硬件
喵手1 小时前
Python爬虫实战:旅游数据采集实战 - 携程&去哪儿酒店机票价格监控完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集结果csv导出·旅游数据采集·携程/去哪儿酒店机票价格监控
2501_944934731 小时前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
helloworldandy1 小时前
使用Pandas进行数据分析:从数据清洗到可视化
jvm·数据库·python
yunfuuwqi2 小时前
OpenClaw✅真·喂饭级教程:2026年OpenClaw(原Moltbot)一键部署+接入飞书最佳实践
运维·服务器·网络·人工智能·飞书·京东云
迎仔2 小时前
C-算力中心网络隔离实施方法:怎么搞?
运维·网络
代码游侠2 小时前
C语言核心概念复习——网络协议与TCP/IP
linux·运维·服务器·网络·算法
肖永威3 小时前
macOS环境安装/卸载python实践笔记
笔记·python·macos
TechWJ3 小时前
PyPTO编程范式深度解读:让NPU开发像写Python一样简单
开发语言·python·cann·pypto
枷锁—sha3 小时前
【SRC】SQL注入WAF 绕过应对策略(二)
网络·数据库·python·sql·安全·网络安全