|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
||||
|
||||
|
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
|
||||
|
||||
|
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. |
|
#3
|
||||
|
||||
|
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()
|
|
#4
|
||||
|
||||
|
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. |
|
#5
|
||||
|
||||
|
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. |
|
#6
|
||||
|
||||
|
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. |
|
#7
|
||||
|
||||
|
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
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Adding code to GUI |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|