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


    一、实现样式

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

    二、核心点

    1、使用Tkinter来制作桌面GUI页面
    2、使用telnetlib模块测试telnet端口

    三、困难点

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

     output_text.insert(INSERT,f"{host} {port}端口开放 \n")
     time.sleep(0.1)
     output_text.update()
    
    • 1
    • 2
    • 3

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

    # 键盘无法输入
    output_text.bind("",lambda event:"break")
    
    • 1
    • 2

    四、完整代码

    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("",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()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
  • 相关阅读:
    【从零开始学微服务】05.微服务的优势和不足
    数据结构之空间复杂度、顺序表
    书单|1024程序员狂欢节充能书单!
    文件与IO
    php利用微信公众号发送模板消息
    Vs Code npm install 报错解决方法
    em与rem的区别
    leetcode 1586 二叉搜索树迭代器 II 与 173. 二叉搜索树迭代器
    【Python3 高级篇】5. subprocess 子进程管理,取代 os.popen()/os.system()
    4.查询用户的累计消费金额及VIP等级
  • 原文地址:https://blog.csdn.net/weixin_43205774/article/details/133790717