[Tutor] How to watch a changing list, and wait for a certain value to leave?

Eric eric at digitalert.net
Sat Oct 16 02:17:56 CEST 2004


Eric wrote:

> I got some help here before with this program I have started to write, 
> and I am hung up on something again. Anyway I'm trying to write a 
> simple program that will watch for a connection to be made by a 
> certain host, sound the system bell when the connection is made, and 
> then when the connection is closed have the system bell sound again. 
> Then loop back to the beginning and start watching again. Here is the 
> only working part I have so far...
>
>
> import os
> import time
>
> connected = False
>
> while not connected:
>    o=os.popen("netstat -an")
>    for l in o:
>        try:
>            if l.split()[1].endswith("192.168.0.250:21"):
>                print "\a\a\a\a\aMatch!"
>                connected = True
>            else:
>                print "Nothing"          except IndexError:
>            print "Index Exception"
>    time.sleep(1)
>
>
> That works just like I want it to however now I need to figure out a 
> way to know when the host disconnects. I have tried to use count() 
> function in conjunction with the less than operator to wait till 
> "192.168.0.250:21" is <1 , but can't seem to get it right. Can anyone 
> give me a hint, but not write the entire thing for me, or maybe give 
> me some insite on a better way to do this?
>
> Thanks
>


I got it working..


import os
import time

connected = False

while not connected:
    o=os.popen("netstat -an")
    for l in o:
        try:
            if l.split()[1].endswith("192.168.0.250:21"):
                print "\a\a\a\a\aMatch!"
                connected = True
            else:
                print "Nothing"   
        except IndexError:
            print "Index Exception"
    time.sleep(1)


amount=0
while connected:
    o=os.popen("netstat -an")
    for l in o:
        try:
            if l.split()[1].endswith("192.168.0.250:21"):
                print "Still There"
                connected = True
                amount +=1
                print amount
            else:
                print "Nothing"
        except IndexError:
            print "Index Exception"
    time.sleep(1)
    if amount == 1:
        amount -=1
    else:
        print "It's Gone"
        connected = False
             


print "\a\a"
raw_input("Press Enter to close")


More information about the Tutor mailing list