|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
||||
|
||||
|
Trouble with int values
Hi. I have a problem in my script.
the problem is that when I type some IP in the Text2, just read one number. example: When I type : 127.0.0.1 80, just read the 7 I would like that read the last 2 (80) number. what could be the problem? Thanks Code:
b = BannerGrabber.BannerGrabber(self.components.Text2.text[1])
for i in self.components.Text2.text[2:]:
if b.connect(int(i)):
try:
self.components.Text1.text = "Port %s: %s" % (i,b.getBanner())
b.close()
except Exception,e:
print e
else:
self.components.Text1.text = mess + "Port %s: (closed)" % i
|
|
#2
|
||||
|
||||
|
Ok you lost me again, could you attach your whole program, it would make it easier since what you posted doesn't show much at all
![]() Mark. Last edited by netytan : December 18th, 2003 at 05:36 PM. |
|
#3
|
||||
|
||||
|
Thanks for your reply. Attached the whole code.
|
|
#4
|
||||
|
||||
|
Ah ok this is kinda crazy
, and i dont have pyCard which makes this very hard. In any case your variable self.components.Text2.text[1] isnt defined as far as i can see in your code, also using a slice like that on a string (which i guess text is) you get a single letter ![]() On the plus side i cant see anything wrong with your BannerGrabber() class. So we can narrow this down to your Scan file. I cant also see Scan.rscr.py beign used anywhere, so is this being used by something in pyCard? Mark. |
|
#5
|
||||
|
||||
|
Yeah Scan.rscr.py is used with PythonCard to setup your textboxes or checkboxes and all kind of objects. I changed for Tkinter maybe you can help with the new attached file.
Last edited by Gerardoj : December 18th, 2003 at 07:57 PM. |
|
#6
|
|||
|
|||
|
Re: Trouble with int values
Quote:
Most likely the problem is Text2.text is a string; you're trying to access the port number, I assume, by trying to access it as the second element of some array (the IP address being the first). But a string is not the same as an array -- to break up the string into an array, you can use split: Code:
>>> addr = "127.50.80.90 54"
>>> addr
'127.50.80.90 54'
>>> ip, port = addr.split(' ')
>>> ip
'127.50.80.90'
>>> port
'54'
If you don't need the IP address, and just the port, you can do it all in one line: Code:
>>> addr.split(' ')[1]
'54'
So I think you'd want: Code:
for i in self.components.Text2.text.split(' ')[1]:
Since that's only going to iterate over one element, that single port, you might want to remove the for loop altogether. Cheers, - tps Last edited by theperfectsoup : December 23rd, 2003 at 12:56 PM. |
|
#7
|
|||
|
|||
|
Just some quick pseudo-code.
I think your looking for something being processed a bit more in your for loop before you get to your slice. I'm guessing that your grabbing a bunch of results from a scan and processing those (if not see soups idea). Something more like this: #massage the results from the scan into #a nice array first for i in scan_rslts_array: if b.connect(int(i[-2:]): #count two back from end of slice for port try: self.components.Text1.text = "Port %s: %s" % (i[-2:]b.getBanner()) b.close() except Exception.e: print e else: self.components.Text1.text = mess + "Port %s: (closed)" % i[-2:] Ugly with the slices. Prolly should put a my_slice = i[-2:] near the top to clean it up a little. -rag |
|
#8
|
||||
|
||||
|
Thanks all for your help. Still have a problem, I changed my code and for i in self.components.Text2.text.split(' ')[2]:
IndexError: list index out of range shows up. When I change for self.components.Text2.text.split(' ')[1]:. runs fine, but just read the last integer What could be the problem?. Code:
b = BannerGrabber.BannerGrabber(self.components.Text2.text[1])
for i in self.components.Text2.text.split(' ')[2]:
if b.connect(int(i[-2:])):
try:
self.components.Text1.text = "Port %s: %s" % (i,b.getBanner())
b.close()
except Exception,e:
print e
else:
self.components.Text1.text = mess + "Port %s: (closed)" % i
|
|
#9
|
||||
|
||||
|
Have you tried
print self.components.Text2.text.split(' ') print self.components.Text2.text.split(' ')[2] to see what you get? Grim Happy bug hunting holidays ![]() |
|
#10
|
|||
|
|||
|
(Lengthy post. Sorry)
Your index in your array is off. Array indexes (indices? Too early...) start at 0. So if you fire up IDLE: Code:
ip_port = '172.0.0.1 80'
ip_array = ip_port.split(' ') # Array is now '172.0.0.1' and '80'
print ip_array[0] #Should print '172.0.0.1'
print ip_array[1] #Prints '80'
Now before I go on, what exactly are you trying to accomplish in your for loop? It seems like your not working with an array of IP and port numbers, instead just working on one single one. Code:
#for loop unrolled a little for clarity
Text2_array = self.components.Text2.text.value.split(' ')
#Text2_array[0] is now '172.0.0.1'
#Text2_array[1] is now '80'
#Text2_array[2] is nothing and will throw an out of bounds exception
for i in Text2_array[1] #This loops through the values in ONLY element [1]
#IE the first time through i= '8', the second i= '0' and then falls through.
#rest of loop
OK, assuming that Text2.text is going to be equal to something like: 172.0.0.1 80 172.0.0.2 80 172.0.0.3 80 Then this code will work: Code:
for i in self.components.Text2.text.split('\n'):
ip, port = i.split(' ')
b = BannerGrabber.BannerGrabber(ip)
if b.connect(int(port)):
try:
self.components.Text1.text = "Port %s: %s" % (port,,b.getBanner())
b.close()
else:
self.components.Text1.text = mess + "Port %s: (closed)" % port
BTW try to give your variables names other then their default. I know on small projects like this it doesn't matter much, but on big ones you'll save DAYS. -rag |
|
#11
|
||||
|
||||
|
Hey Thanks a lot. Now is working fine, and thanks for the suggestion about variables.
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Trouble with int values |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|