|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Problems looping
This is the first program I have started to write, and am getting hung up on a few things..
All I'm trying to do is have the system alert the system bell a few times once a connection is made to my computer by a certain IP and port number, and then sound the bell only once or twice once the connection goes away. Then it will have to loop back to the beginning to watch for a connection to be made, and go away and so on till I kill it. I am only at the very first part of this so far. Code:
import os
import time
o=os.popen("netstat -an")
connected = "no"
while connected != "yes":
for l in o:
try:
if l.split()[1].endswith("192.168.0.250:21"):
print "\a\a\a\a\aMatch!"
connected = "yes"
else:
print "Nothing"
except IndexError:
print "Index Exception"
time.sleep(1)
Now the problem is this. It will only find a match if there is a established connection before I start the program. So lets say there is no current ftp connection, and I start the program it seems to be running however once I do make a connection It never will sound the bell.. So It seems to be only going though the for loop once. Thanks for any help. |
|
#2
|
||||
|
||||
|
Hi EJP,
You need to put your code like this: Code:
while ....
o=os.popen("netstat -an")
for l in o....
......
Have fun ![]() BTW it is usually a good idea to avoid single letter variable names like o and l as they look very much like 0O and 1|. grim ![]()
__________________
*** Experimental Python Markup CGI V2 *** |
|
#3
|
||||
|
||||
|
You should also use the boolean values True or False; you're currently using two strings (yes and no) i.e.
Code:
connected = False
while not connected:
...do whatever...
Note: The boolean values True and False can also be expressed as 1 and 0 respectivly. If you're running a version older than Python 2.3 then you will need to use these rather numbers. It might also be handy to know that an empty object (string, list, tuple or dictionary) are also treated as being False. Have fun, Mark. Last edited by netytan : October 6th, 2004 at 08:20 AM. |
|
#4
|
|||
|
|||
|
Thanks for the help.
After putting this... Code:
o=os.popen("netstat -an")
After the while statement got it looping the way I wanted it to. Now I just need to figure out the rest of it out. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Problems looping |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|