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()
相关推荐
用户83562907805116 小时前
Python 实现 PDF 文件加密与解密方法
后端·python
用户83562907805116 小时前
使用 Python 冻结与拆分 Excel 窗格教程
后端·python
你好潘先生1 天前
别再记命令了,用 yeero do 说句人话就能跑脚本,而且不烧 token
服务器·python·命令行
Agent_大师1 天前
WebSocket 行情重连成功,K线缺口不会自动消失
python
荣码1 天前
LLM结构化输出:让AI返回JSON而不是废话,我踩了4个坑
java·python
copyer_xyf1 天前
FastAPI 如何连接 MySQL
后端·python
apocelipes2 天前
常用编程语言和库的正则表达式性能对比
c语言·c++·python·性能优化·golang·开发工具和环境
用户8356290780512 天前
使用 Python 在 PDF 中创建与管理书签
后端·python
MeixianAgent2 天前
Python 回测数据入口怎么验?历史 K 线入库前先做 5 个检查
后端·python
咕白m6252 天前
用 Python 实现一键批量查找与替换 Excel 数据
后端·python