<div class="gmail_quote">On 23 June 2010 13:29, Nethirlon . <span dir="ltr"><<a href="mailto:nethirlon@gmail.com">nethirlon@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Hello everyone,<br>
<br>
I'm new at programming with python and have a question about how I can<br>
solve my problem the correct way. Please forgive my grammar, English<br>
is not my primary language.<br>
<br>
I'm looking for a way to repeat my function every 30 seconds.<br>
<br>
As an example I have written a ping function. But I would like this<br>
function to repeat itself every 30 seconds, without stopping until I<br>
give it a STOP command (if such a thing exists.)<br>
<br>
Code:<br>
import os, sys<br>
<br>
def check(host):<br>
try:<br>
output = os.popen('ping -ns 1 %s' % host).read()<br>
alive = output.find('Reply from')<br>
print alive<br>
if alive is -1:<br>
print '%s \t\t DOWN ' % host<br>
else:<br>
print '%s \t\t OK' % host<br>
except OSError, e:<br>
print e<br>
sys.exit()<br>
<br>
check('<a href="http://www.google.com" target="_blank">www.google.com</a>')<br>
<br>
Let me know if anything is unclear or if there are other<br>
recommendations about doing some parts different.<br>
<br>
Kind regards,<br>
<font color="#888888">Nethirlon</font><br></blockquote><div><font color="#888888"><br></font>You should probably take a look at the time module <a href="http://docs.python.org/library/time.html">http://docs.python.org/library/time.html</a> for waiting (maybe time.sleep)<br>
<br>If you want to call a function an unspecified number of times you probably want a while loop:<br><br>while True:<br> pass<br><br>loops infinitely unless you quit the program though, to leave the loop you will need to use "break", you will probably want an if statement and change the value of some variable when you want to stop calling the function and break out of the loop.<br>
<br>HTH,<br>Adam.<br></div></div>