SKY外语计算机学习
标题:
python 多线程ping
[打印本页]
作者:
rogan
时间:
2012-10-4 19:10
标题:
python 多线程ping
doping.py
import os
import commands
import time
import mytimer
from Tkinter import *
class Application(Frame):
def doPing(self):
cmd = "ping "
cmd = cmd + self.v.get()
pintime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
print cmd, " please waite",pintime
usr = os.popen(cmd,'r').read()
usr= str(usr)
usr = usr.replace('\r\n','\n')
usr = pintime +"\n"+usr + self.v.get() + "\n-------------------------------\n"
print usr
self.area.insert(1.0,usr)
def multiping(self):
thread1 = mytimer.mytimer(1,1,'www.baidu.com',self.area)
thread2= mytimer.mytimer(2,2,'192.168.0.1',self.area)
thread3 = mytimer.mytimer(3,3,'localhost',self.area)
thread1.start()
thread2.start()
thread3.start()
def clr(self):
self.area.delete(1.0,END)
def createWidgets(self):
self.area=Text(self,height=30,width=50)
self.area.grid(row=2,column=0,columnspan=5)
self.btn_quit = Button(self, text="Quit", fg="red", command=self.quit)
self.btn_quit.grid(row=0,column=0)
self.btn_clr = Button(self,text="清除",command=self.clr )
self.btn_clr.grid(row=0,column=2)
self.btn_ping = Button(self, text="ping", command=self.doPing)
self.btn_ping.grid(row=0,column=1,columnspan=2)
self.btn_ping = Button(self, text="multiping", command=self.multiping)
self.btn_ping.grid(row=0,column=3,columnspan=2)
MODES = [
("localhost", "localhost"),
("route", "192.168.0.1"),
("baidu", "www.baidu.com"),
("qq", "www.qq.com"),
]
self.v = StringVar()
self.v.set("localhost") # initialize
i = 0
for text, mode in MODES:
self.b = Radiobutton(self, text=text,
variable=self.v, value=mode)
self.b.grid(row=1,column=i)
i = i+1
def __init__(self,master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()
root = Tk()
root.geometry("410x550+0+0")
app = Application(master=root)
app.mainloop()
root.destroy()
复制代码
mytimer.py
import threading
import time
import os
class mytimer(threading.Thread):
def __init__(self, num, interval,web,area):
threading.Thread.__init__(self)
self.thread_num = num
self.interval = interval
self.thread_stop = False
self.web = web
self.rs = ""
self.area = area
def run(self):
#while not self.thread_stop:
#print 'Thread Object(%d),Time:%s\n' % (self.thread_num,time.ctime())
#time.sleep(self.interval)
cmd="ping "+self.web
#os.system(cmd)
rs = os.popen(cmd).read()
self.rs =rs
#print rs
def __del__(self):
print self.rs
self.area.insert(1.0,self.rs)
def stop(self):
self.thread_stop = True
def test():
thread1 = mytimer(1,1,'www.baidu.com')
thread2= mytimer(2,2,'192.168.0.1')
thread3 = mytimer(3,3,'localhost')
thread1.start()
thread2.start()
thread3.start()
##time.sleep(10)
thread1.stop()
thread2.stop()
thread3.stop()
return
if __name__ == '__main__':
test()
复制代码
作者:
沈申丹一
时间:
2012-10-25 12:34
提示:
作者被禁止或删除 内容自动屏蔽
欢迎光临 SKY外语计算机学习 (http://join.skywj.com/)
Powered by Discuz! X2.5