multiping + while loop + excel

Fransiska M Argasetya acquiesce11 at yahoo.com.au
Sun Sep 17 09:56:08 EDT 2006


dear everyone
   
  please help me with my coding dilemma o_O
   
  i have a python code that can ping several sites as shown below:
   
  =====================================================
  =====================================================
   
  import threading
import os
import Queue
import time
from time import sleep
   
  def showResponse(args):
    """Pretty prints passed tuple to stdout"""
      ip, stdoutLi, threadName = args
    print '%s \t\t\t\t\t\n' % (ip)
      for line in stdoutLi:
        line = line.strip()
        if not line: continue
        print '\t' + line
    print '-'*72
  class Pinger(threading.Thread):
      def __init__(self, host, queue):
        threading.Thread.__init__(self)
        self.__host = host
        self.__queue = queue
        self.setDaemon(1)
      def run(self):
        pingCmd = "ping -n 1 -w 1000 " + self.__host
        childStdout = os.popen(pingCmd)
        result = (self.__host, childStdout.readlines(), self.getName())
        childStdout.close()
        self.__queue.put(result)
  
if __name__ == '__main__':
    
    hostLi = ['yahoo.co.uk','google.com','wikipedia.org'] # read from the list
      q = Queue.Queue()
      startA = time.time()
    for host in hostLi:
        """Create and start all necessary threads, passing results
        back via thread safe Queue instance.
        """
      
        Pinger(host,q).start()
      i = len(hostLi) # counts the length of hostLi
        
    while i > 0:
        showResponse(q.get())
        i -= 1

  =====================================================
  =====================================================
   
  i would like to develop it more with the following requirements:
   
  1). i want to make the code to read the list of websites from another file (be it a .txt or .xls)
  2). i want the results of the pings to be displayed in another file, where i can manipulate those figures later on for research purposes
  3). if number (2) is achieved, i want the code to run every 60 minutes so i can make a statistic with those values, i.e. pinging to site A has less delay during the day compare to during night time or something like that
   
  i have several ideas of my own but they dont seem to work:
  1). hostLi = open("ipaddress.txt")  # ipaddress.txt is the list of websites
     for line in hostLi.readlines():
          print line
     hostLi.close()
    
       OR
   
       f = open("ipaddress.txt")
     while 1:
          line = f.readline()
          if not line: break
               hostLi(line)
     f.close()
  2). myoutfile = showResponse(q.get()) # write this result to the file
     theoutfile = open ("myout.txt","w") # open the file to write (w)
     for item in myoutfile:  # write out the contents
          theoutfile.write (item)
       theoutfile.close()
  3). while i > 0:
        showResponse(q.get())
        i -= 1
    while 1:
        time.sleep(3600)  # 60 minutes = 3600 seconds
   
  thank you very much for anyone who can help me out with these issues
   
  really appreciate it a lot!

 		
---------------------------------
On Yahoo!7 
  Answers: 25 million answers and counting. Learn something new today
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20060917/21382e65/attachment.html>


More information about the Python-list mailing list