Difference between revisions of "ThreadedPing.py"
(One intermediate revision by the same user not shown) | |||
Line 41: | Line 41: | ||
for pingle in pinglist: | for pingle in pinglist: | ||
pingle.join() | pingle.join() | ||
+ | |||
+ | for pingle in pinglist: | ||
print "Status from ",pingle.ip,"is",report[pingle.status] | print "Status from ",pingle.ip,"is",report[pingle.status] | ||
</source> | </source> | ||
+ | [[Category:CSC352]][[Category:Python]][[Category:Threads]] |
Latest revision as of 18:36, 4 February 2010
--D. Thiebaut 16:09, 24 January 2010 (UTC)
#
# http://www.wellho.net/solutions/python-python-threads-a-first-example.html
import os
import re
import time
import sys
from threading import Thread
class testit( Thread ):
def __init__ ( self, ip ):
Thread.__init__(self)
self.ip = ip
self.status = -1
def run( self ):
pingaling = os.popen( "ping -q -c2 " + self.ip, "r" )
while True:
line = pingaling.readline()
if not line: break
igot = re.findall(testit.lifeline, line)
if igot:
self.status = int( igot[0] )
#--- static members for the class ---
testit.lifeline = re.compile(r"(\d) received")
report = ( "No response", "Partial Response", "Alive")
print time.ctime()
pinglist = []
for host in range(1,50):
ip = "131.229.72."+str(host)
current = testit(ip)
pinglist.append(current)
current.start()
for pingle in pinglist:
pingle.join()
for pingle in pinglist:
print "Status from ",pingle.ip,"is",report[pingle.status]