#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    2
    Rep Power
    0

    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. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    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
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula

  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    2
    Rep Power
    0
    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.

IMN logo majestic logo threadwatch logo seochat tools logo