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

Kent Johnson kent_johnson at skillsoft.com
Sat Oct 16 04:25:18 CEST 2004


Eric,

You have a lot of duplicated code between the two loops. One way to avoid 
this is by making a function:
def isConnected():
    connected = False
    o=os.popen("netstat -an")
    for l in o:
        try:
            if l.split()[1].endswith("192.168.0.250:21"):
                connected = True
        except IndexError:

then your main program will look something like this:

connected = isConnected()
while not connected:
   print 'Not connected'
   sleep(1)
   connected = isConnected()

while connected:
   print 'Still there'
   sleep(1)
   connected = isConnected()
print "It's gone"

Kent

At 08:17 PM 10/15/2004 -0400, Eric wrote:
>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")
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list