Toplevel Widget和Portscanner的问题

时间:2010-11-07 03:28:59

标签: python multithreading widget tkinter

def PortScanWin():

win2 = Toplevel()
win2.title("PortScan")
win2.wm_maxsize(width='190',height='370')
win2.wm_minsize(width='190',height='370')

def go():
    global app
    result.delete(1.0,END)
    app=scan()
    app.start() 

def stop():
    app.flag='stop'

def clear():
    host_e.delete(0,END)
    start_port_e.delete(0,END)
    end_port_e.delete(0,END)
    result.delete(1.0,END)

class scan(threading.Thread):
    def _init_(self):
        threading.thread._init_(self)
    def run(self):
        self.host=host_e.get() 
        self.start_port=int(start_port_e.get())
        self.end_port=int(end_port_e.get())
        self.open_counter=0
        self.flag='scan'       
        start.config(text="Stop",command=stop)
        win2.update()
        result.insert(END,"Scanning "+str(self.host)+"...\n\n")
        win2.update()
        while self.start_port<=self.end_port and self.flag=='scan':
            self.sk=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.sk.settimeout(0.01) 
            try:
                self.sk.connect((self.host,self.start_port))
            except:
                pass 
            else:
                result.insert(END,str(self.start_port)+"\n")
                win2.update()
                self.open_counter=self.open_counter+1
                self.sk.close()
            self.start_port=self.start_port+1
        if self.flag=='scan':    
            result.insert(END,"\nDone !!\nFound "+str(self.open_counter)+" opened ports")
            win2.update()
            start.config(text="Scan",command=go)
            win2.update()
        elif self.flag=='stop':
            result.insert(END,"\n Scan stopped.")
            start.config(text="Scan",command=go)
            win2.update()

Label(win2,text="Host: ").grid(row=1,column=1,sticky="w")
host_e=Entry(win2)
host_e.grid(row=1,column=2,sticky="WE")
Label(win2,text="Start port: ").grid(row=2,column=1,sticky="w")
start_port_e=Entry(win2)
start_port_e.grid(row=2,column=2,sticky="WE")
Label(win2,text="End port: ").grid(row=3,column=1,sticky="w")
end_port_e=Entry(win2)
end_port_e.grid(row=3,column=2,sticky="WE")
start=Button(win2,text="Scan",command=go)
start.grid(row=5,columnspan=3,sticky="WE")
clear=Button(win2,text="Clear",command=clear)
clear.grid(row=6,columnspan=3,sticky="WE")
result=Text(win2,width=20,height=20)
result.grid(row=7,columnspan=3,sticky="WENS")

portscanner正在子窗口中运行,但有些如何不起作用,当我点击“扫描”时,我得到“正在扫描......”并且它什么也没做,任何帮助都将受到高度赞赏,谢谢。

2 个答案:

答案 0 :(得分:0)

这不是你问题的真正答案,但你可能想看看Scapy。 Scapy允许您构建自己的数据包并从网络接口读取原始数据。两者在编写端口扫描程序时显然非常有用。因此,根据您的需要,您可能希望用Scapy中的一个替换您的端口扫描器。

有关详情,请访问:http://www.secdev.org/projects/scapy/

答案 1 :(得分:0)

学会相信你得到的反馈。在您的情况下,代码正在打印“正在扫描...无”。所以,当(推测)应该是其他东西时,显然某些东西是“无”。弄清楚为什么self.host为无,你可能会解决你的问题