Saturday, February 20, 2010

MultiHeaded python - Threading in python

Hi guys , as a student of multicore programming , I was just curious about threading facilities in higher level programming languages like python . So just revoking on the idea , I did find a good small ping application , using python.

Here is the source code : try it and make the changes to suite your local network.
I have made a comment , which shall help you .

original source : 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 1:
        line = pingaling.readline()
        if not line: break
        igot = re.findall(testit.lifeline,line)
        if igot:
           self.status = int(igot[0])

testit.lifeline = re.compile(r"(\d) received")
report = ("No response","Partial Response","Alive")

print time.ctime()

pinglist = []

for host in range(1,4): # Change the IP range here
   ip = "192.168.1."+str(host)
   current = testit(ip)
   pinglist.append(current)
   current.start()

for pingle in pinglist:
   pingle.join()
   print "Status from ",pingle.ip,"is",report[pingle.status]

print time.ctime()

 Posted by :
  Jayanthi GM (Course Instructor for Java , PESIT )
  Prashanth Raghu ( Student Assistant )

No comments:

Post a Comment