程序在尝试搜索端口时崩溃

时间:2016-06-19 13:03:51

标签: python multithreading user-interface

我正在尝试使用GUI制作线程端口扫描程序。当我测试我的脚本时,它崩溃了。我不知道我做错了什么,这就是为什么我在这里写作,我需要你的帮助来解决它。 Ps:我还没有完成它所以请不要查看不存在的异常,未完成的细节等。提前感谢。 这是我的代码:

from tkinter import *
import socket
import time
import threading
from queue import Queue

class Application(Frame):
    def __init__(self, master):
        super(Application, self).__init__(master)
        self.grid()
        self.create_widgets()

def create_widgets(self):
    Label(self, text = "Fill required fields.").grid(row=0, column=0, sticky=W)
    Label(self, text = "Check ports from:").grid(row=1,column=0,sticky = W)
    Label(self, text = "Check ports to:").grid(row=2,column=0,sticky = W)
    Label(self, text="Site:").grid(row=3,column=0,sticky=W)
    Label(self, text="Results:").grid(row=5,column=0,sticky=W)
    self.range_from_entry=Entry(self)
    self.range_from_entry.grid(row=1,column=1)

    self.range_to_entry=Entry(self)
    self.range_to_entry.grid(row=2,column=1)

    self.site_entry=Entry(self)
    self.site_entry.grid(row=3,column=1)
    self.bttn1=Button(self, text = "Search",command = self.port_scanner).grid(column=1, row=4, sticky=E)
    self.results_box=Text(self, width=20, height=10).grid(row=6,columnspan=1)

def port_scanner(self):
    fr0m=self.range_from_entry.get()
    t0=self.range_to_entry.get()
    target=self.site_entry.get()
    print_lock = threading.Lock()
    def portscan(port):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            con = s.connect((target,port))
            with print_lock:
                str(port)
                message = 'port',port,'is open!'
                self.results_box.insert(0.0, message)
                con.close()
        except:
            pass
    def threader():
        while True:
            worker = q.get()
            portscan(worker)
            q.task_done()

    q = Queue()

    for x in range(30):
        t = threading.Thread(target=threader)
        t.deamon = True
        t.start()

    for worker in range(int(fr0m),int(t0)):
        q.put(worker)

        q.join()


root=Tk()
root.geometry("300x300")
root.title("Port Scanner")

app = Application(root)

root.mainloop()

0 个答案:

没有答案