[Tutor] Help debuging a small program
Kent Johnson
kent37 at tds.net
Mon Feb 21 19:21:35 CET 2005
Mark Kels wrote:
> Hi list !
> Here is a small port scanner I made to practice sockets and GUI
> programming ( WARNING: the program crash when scan button is clicked):
How far does it get? How do you know?
I would put some debug print statements in. Also you should call sk.close() in the else clause of
scan(). Finally, I don't think you will see any output in the result window until get_info()
returns; you have to give some time to mainloop() for it to be able to trigger the updates.
>
> #----------Imports----------
> from Tkinter import *
> import socket
> #----------The result GUI window function---------
> def result():
> global result_t #I made result_t global so I will be able to
> insert data into it from the scan function
> r_root=Toplevel(root)
> result_t=Text(r_root)
> result_t.pack()
>
> #----------Command Functions----------
> # + ---Function to get the info from the gui and start the scan---
> def get_info():
> result()
> host=host_e.get()
> start_port=int(start_port_e.get())
> end_port=int(end_port_e.get())
> scan(host,start_port,end_port)
>
> # + ---The Scan---
> def scan(host,start_port,end_port):
> sk=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> while start_port<=end_port:
> try:
> sk.connect((host,start_port))
> except:
> result_t.insert(END,"Port",start_port,"is CLOSED on",host,"\n")
> else:
> result_t.insert(END,"Port",start_port,"is OPENED on",host,"\n")
> start_port=start_port+1
>
> #----------The main GUI window----------
> root=Tk()
> Label(root,text="Host: ").grid(row=1,column=1)
> host_e=Entry(root)
> host_e.grid(row=1,column=2)
> Label(root,text="Start port: ").grid(row=2,column=1)
> start_port_e=Entry(root)
> start_port_e.grid(row=2,column=2)
> Label(root,text="End port: ").grid(row=3,column=1)
> end_port_e=Entry(root)
> end_port_e.grid(row=3,column=2)
> Button(root,text="Scan",command=get_info).grid(row=5,column=1)
>
> root.mainloop()
>
> Why does the program crash (without any errors :-/ ) when I click the
> Scan button ??
>
More information about the Tutor
mailing list