[Tutor] Repeat function until...
Sithembewena Lloyd Dube
zebra05 at gmail.com
Wed Jun 23 16:29:44 CEST 2010
My two cents' worth added below. Seems to do what you want. You probably
want to call sys.wait(x) after printing an error, so it can be read before
exiting?
import os, sys, time
def check(host):
try:
output = os.popen('ping -ns 1 %s' % host).read()
alive = output.find('Reply from')
print alive
if alive is -1:
print '%s \t\t DOWN ' % host
else:
print '%s \t\t OK' % host
except OSError, e:
print e
sys.exit()
while alive != -1:
try:
time.sleep(4)
check(host)
except KeyboardInterrupt, k:
sys.exit()
def get_command():
prompt = input('Start now? Use 1 to start or 0 to exit.\n')
return prompt
if __name__ == '__main__':
cmd = get_command()
if cmd == 1:
check('localhost')
elif cmd == 0:
sys.exit()
else:
get_command()
On Wed, Jun 23, 2010 at 2:29 PM, Nethirlon . <nethirlon at gmail.com> wrote:
> Hello everyone,
>
> I'm new at programming with python and have a question about how I can
> solve my problem the correct way. Please forgive my grammar, English
> is not my primary language.
>
> I'm looking for a way to repeat my function every 30 seconds.
>
> As an example I have written a ping function. But I would like this
> function to repeat itself every 30 seconds, without stopping until I
> give it a STOP command (if such a thing exists.)
>
> Code:
> import os, sys
>
> def check(host):
> try:
> output = os.popen('ping -ns 1 %s' % host).read()
> alive = output.find('Reply from')
> print alive
> if alive is -1:
> print '%s \t\t DOWN ' % host
> else:
> print '%s \t\t OK' % host
> except OSError, e:
> print e
> sys.exit()
>
> check('www.google.com')
>
> Let me know if anything is unclear or if there are other
> recommendations about doing some parts different.
>
> Kind regards,
> Nethirlon
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
--
Regards,
Sithembewena Lloyd Dube
http://www.lloyddube.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100623/81452145/attachment.html>
More information about the Tutor
mailing list