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()
相关推荐
ycjunhua几秒前
终极入门:uv —— 超快 Python 包 / 环境管理工具(Windows 完整版)
windows·python·uv
2401_88360025几秒前
SQL视图名称冲突如何避免_建立规范化的命名空间与管理
jvm·数据库·python
JAVA学习通2 分钟前
AI Agent 工具调用机制深度解析与 Spring Boot 工程集成实战(2026版)
java·人工智能·spring boot·python·spring
亿牛云爬虫专家3 分钟前
解决 Python 爬虫代理 407 错误:基于 urllib3 更新与爬虫代理的实战指南-2
爬虫·python·爬虫代理·authentication·urllib3·407·base64 编码
m0_640309303 分钟前
CSS中如何让浮动元素撑开父容器_深度解析清除浮动
jvm·数据库·python
2301_816660214 分钟前
Golang bufio怎么读取用户输入_Golang标准输入读取教程【详解】
jvm·数据库·python
WJ.Polar5 分钟前
Ansible任务控制
linux·运维·网络·python·ansible
泰迪智能科技016 分钟前
图书教材推荐|Python网络爬虫技术(第2版)(微课版)
开发语言·爬虫·python
解救女汉子8 分钟前
Golang如何创建和删除目录_Golang目录操作教程【完整】
jvm·数据库·python
hj28625110 分钟前
网络基础知识day03
网络·智能路由器