[Tutor] A way to restart a script after it runs?

Eric eric at digitalert.net
Fri Oct 22 10:00:26 CEST 2004


I ended up just putting in a while loop at the start of the script that 
will always be "true", and then indented everything else below it. I 
kinda figured that would be how I would have to do it, but either way it 
works just fine.

And without further adu here is my first completed Python project that I 
didn't copy out of a book.. .


import os
import time

z = 2

while z ==2:

    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")





Nothing fancy, but a good learning experience none the less.




More information about the Tutor mailing list