I hacked together my first script to learn threading. I'm seeing some weird output.
This script will PING approximately 1,000 routers. I added a few print commands for debugging so I can see if it runs faster than a single threaded script. It blazes through the routers, however I'm seeing multiple prints (PINGs) for the same device. What is causing this?
Also, I can get fc to write to the file.
#!/usr/bin/env python
#Let us profile code which uses threads
import os, re, string, thread, threading, time
from time import strftime
#from threading import Thread
class PingThread(threading.Thread):
def run(self):
pingaf = os.popen('ping -n 1 -w 3 ' + rtr)
pingas = string.join(pingaf.readlines())
if ms.search(pingas):
print (re.sub('\n','',rtr)) + " responded." #for debugging
else:
pingaf = os.popen('ping -n 2 -w 3 ' + rtr)
pingas = string.join(pingaf.readlines())
if ms.search(pingas):
print (re.sub('\n','',rtr)) + " responded." # for debugging
else:
fc=fc+1
output.write(re.sub('\n','',rtr) + " did not respond.\n")
fc = 0 # failure counter
ms = re.compile("Reply from")
rpath = (r"c:\temp\py\network_ping_routers.txt")
if os.path.exists(r"c:\temp\py\network_ping_again.txt"):
rpath = (r"c:\temp\py\network_ping_again.txt")
wpath = (r"c:\temp\py\network_ping.out")
#os.system(r"c:\tnd\bin\cawto.exe -cat NetNet -n l17aesm1 forward blue held Weekly ISDN testing has started -" + strftime(" %H:%M:%S %x") + "\n")
output = open(wpath, "a")
output.write("\n" + "\n" + "Network PING test started -" + strftime(" %H:%M:%S %x") + "\n")
output.flush()
for rtr in file(rpath):
PingThread().start()
print fc # for debugging
#output.write(fc + " failures found.\n")
output.write("\n" + "\n" + "Network PING test completed -" + strftime(" %H:%M:%S %x") + "\n")
output.close()
_______________________________________________ Tutor maillist - [email protected] http://mail.python.org/mailman/listinfo/tutor
