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
import os
import Queue
import time
from time import sleep
def showResponse(args):
"""Pretty prints passed tuple to stdout"""
"""Pretty prints passed tuple to stdout"""
ip, stdoutLi, threadName = args
print '%s \t\t\t\t\t\n' % (ip)
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
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)
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)
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()
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
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()
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()
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 = 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
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
-- http://mail.python.org/mailman/listinfo/python-list