#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Somewhere over the Rainbow
    Posts
    128
    Rep Power
    11

    Adding code to GUI


    Hi, I would like to know why doesnt works my below code?, dont show any error message! Thanks.

    Code:
           def port(self):
        	   
            try:
    	   t = tkSimpleDialog.askstring('DATA',               
                                         ENTER IP or HOST:', 
                                         parent=self.TextBox)  
               host = t[1]
               maxport = int(t[2]) + 1
    
               for port in xrange(1, maxport):
                   s = socket(AF_INET, SOCK_STREAM)
                   try:
                      s.connect((host, port))
                      self.TextBox.insert(Tkinter.END,"Port %d is open" % port)
                   except error:
                       continue
        	except IndexError:
             self.TextBox.insert(Tkinter.END,  "Usage: %s <ip address> <max port>" % t[0])
         	except ValueError:
             self.TextBox.insert(Tkinter.END, "Port must be a valid integer")
         	except Exception, e:
             print e
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    I'd love to tell you but it seems like we've only got a small bit of your actual program here, could you attach the whole scrript?

    The reason your not getting an error message would be because you wrapped your code up inside try-except blocks which are kinda like walls errors can't pass though.

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Somewhere over the Rainbow
    Posts
    128
    Rep Power
    11
    Thanks for your reply, here is my whole script

    Code:
    import Tkinter
    import tkFileDialog
    import tkSimpleDialog
    import string
    from socket import *
    
    
    TEXT_FILE_TYPES=[("PRO Scanner Files","scn"),("All files","*")] 
    
    class Scanner:
        def __init__(self):        
            self.FileName=None
            self.CreateWidgets()
    
        def CreateWidgets(self):
            self.root=Tkinter.Tk()
            self.root.title("New file")
            MainFrame=Tkinter.Frame(self.root)        
     
            MenuFrame=Tkinter.Frame(self.root)
            MenuFrame.pack(side=Tkinter.TOP,fill=Tkinter.X)
            FileMenuButton=Tkinter.Menubutton(MenuFrame,
                text="File",underline=0)
            FileMenuButton.pack(side=Tkinter.LEFT,anchor=Tkinter.W)
            FileMenu=Tkinter.Menu(FileMenuButton,tearoff=0)
            FileMenu.add_command(label="New",underline=0,
                command=self.port)        
            FileMenuButton["menu"]=FileMenu        
            # Create the main text field:
            self.TextBox=Tkinter.Text(MainFrame,background="white",foreground="black")
            self.TextBox.pack(fill=Tkinter.BOTH,expand=Tkinter.YES)
            # Pack the top-level widget:
            MainFrame.pack(fill=Tkinter.BOTH,expand=Tkinter.YES)
    
        def port(self):
        	   
            try:
    	   t = tkSimpleDialog.askstring('INGRESO DE DATOS',               
                                         'Ingrese la IP o HOST:', 
                                         parent=self.TextBox)  
               host = t[1]
               maxport = int(t[2]) + 1
    
               for port in xrange(1, maxport):
                   s = socket(AF_INET, SOCK_STREAM)
                   try:
                      s.connect((host, port))
                      self.TextBox.insert(Tkinter.END,"Port %d is open" % port)
                   except error:
                       continue
        	except IndexError:
             self.TextBox.insert(Tkinter.END,  "Usage: %s <ip address> <max port>" % t[0])
         	except ValueError:
             self.TextBox.insert(Tkinter.END, "Port must be a valid integer")
         	except Exception, e:
             print e
    
        def Run(self):
            self.root.mainloop()
    
    if (__name__=="__main__"):
        Scanner().Run()
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    When i run your script it works fine, at least up untill i enter an IP (my localhost) then it freezes.. so i think we can narror it down too somewhere around your s.connect() line.

    Unfortunatly im not really a Tk expert; too be sure i'd try removing the socket and see if it works then, at least then we know where to start!

    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    In port() I guess you need to split the response sting t first and index from 0 not one.

    You could consider a regular expression check to confirm the valid format of the user input.
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Oh, while i think of it, you should move 's = socket(AF_INET, SOCK_STREAM)' to outside your loop! you def' don't want to create a new socket instance each time!

    mark.
    programming language development: www.netytan.com Hula

  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Somewhere over the Rainbow
    Posts
    128
    Rep Power
    11
    I changed it without Tkinter, and now is working fine. How can I add the results into the Textbox in the Tkinter code?

    Thanks a lot.


    Code:
    #!/usr/bin/env python
    
    from socket import *
    import sys
    
    try:
        host = sys.argv[1]
        maxport = int(sys.argv[2]) + 1
    
        for port in xrange(1, maxport):
            s = socket(AF_INET, SOCK_STREAM)
            try:
                s.connect((host, port))
                print "Port %d is open" % port
            except error:
                continue
    except IndexError:
        print "Usage: %s <ip address> <max port>" % sys.argv[0]
    except ValueError:
        print "Port must be a valid integer"
    except Exception, e:
        print e

IMN logo majestic logo threadwatch logo seochat tools logo