设为首页收藏本站

SKY外语、计算机论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 4998|回复: 6
打印 上一主题 下一主题

[代码分享] python 多线程ping

[复制链接]

65

主题

3

好友

739

积分

超级版主

Rank: 8Rank: 8

自我介绍
新年第一天据说有雨,全民齐赏日出的计划恐要泡汤。”宋仁宗拍着包拯的肩,“朕决定把你悬挂在城门上。”“但微臣额上的不是太阳是月亮啊!”“没事,挂久一点就会升级成太阳
生肖
星座
狮子座
性别

最佳新人 活跃会员 热心会员 推广达人 宣传达人 灌水之王 突出贡献 优秀版主 论坛元老

跳转到指定楼层
楼主
发表于 2012-10-4 19:10:36 |只看该作者 |倒序浏览
doping.py

  1. import os
  2. import commands
  3. import time
  4. import mytimer
  5. from Tkinter import *

  6. class Application(Frame):
  7.     def doPing(self):
  8.         cmd = "ping "
  9.         cmd = cmd + self.v.get()
  10.         pintime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
  11.         print cmd, " please waite",pintime
  12.         usr = os.popen(cmd,'r').read()
  13.         usr= str(usr)
  14.         usr = usr.replace('\r\n','\n')
  15.         usr = pintime +"\n"+usr + self.v.get() + "\n-------------------------------\n"
  16.         print usr
  17.         self.area.insert(1.0,usr)

  18.     def multiping(self):
  19.         thread1 = mytimer.mytimer(1,1,'www.baidu.com',self.area)
  20.         thread2= mytimer.mytimer(2,2,'192.168.0.1',self.area)
  21.         thread3 = mytimer.mytimer(3,3,'localhost',self.area)
  22.         thread1.start()
  23.         thread2.start()
  24.         thread3.start()

  25.     def clr(self):
  26.         self.area.delete(1.0,END)
  27.     def createWidgets(self):
  28.         self.area=Text(self,height=30,width=50)

  29.         self.area.grid(row=2,column=0,columnspan=5)
  30.         self.btn_quit = Button(self, text="Quit", fg="red", command=self.quit)
  31.         self.btn_quit.grid(row=0,column=0)

  32.         self.btn_clr = Button(self,text="清除",command=self.clr )
  33.         self.btn_clr.grid(row=0,column=2)
  34.         self.btn_ping = Button(self, text="ping", command=self.doPing)
  35.         self.btn_ping.grid(row=0,column=1,columnspan=2)
  36.         self.btn_ping = Button(self, text="multiping", command=self.multiping)
  37.         self.btn_ping.grid(row=0,column=3,columnspan=2)

  38.         MODES = [
  39.         ("localhost", "localhost"),
  40.         ("route", "192.168.0.1"),
  41.         ("baidu", "www.baidu.com"),
  42.         ("qq", "www.qq.com"),
  43.         ]

  44.         self.v = StringVar()
  45.         self.v.set("localhost") # initialize
  46.         i = 0

  47.         for text, mode in MODES:
  48.             self.b = Radiobutton(self, text=text,
  49.                             variable=self.v, value=mode)
  50.             self.b.grid(row=1,column=i)
  51.             i = i+1

  52.     def __init__(self,master=None):
  53.         Frame.__init__(self, master)

  54.         self.pack()
  55.         self.createWidgets()


  56. root = Tk()
  57. root.geometry("410x550+0+0")
  58. app = Application(master=root)

  59. app.mainloop()
  60. root.destroy()


复制代码
mytimer.py

  1. import threading
  2. import time
  3. import os
  4. class mytimer(threading.Thread):
  5.     def __init__(self, num, interval,web,area):
  6.         threading.Thread.__init__(self)
  7.         self.thread_num = num
  8.         self.interval = interval
  9.         self.thread_stop = False
  10.         self.web = web
  11.         self.rs = ""
  12.         self.area = area

  13.     def run(self):
  14.         #while not self.thread_stop:
  15.             #print 'Thread Object(%d),Time:%s\n' % (self.thread_num,time.ctime())
  16.             #time.sleep(self.interval)
  17.         cmd="ping "+self.web
  18.         #os.system(cmd)
  19.         rs = os.popen(cmd).read()
  20.         self.rs =rs
  21.         #print rs

  22.     def __del__(self):
  23.         print self.rs
  24.         self.area.insert(1.0,self.rs)
  25.     def stop(self):
  26.         self.thread_stop = True


  27. def test():
  28.     thread1 = mytimer(1,1,'www.baidu.com')
  29.     thread2= mytimer(2,2,'192.168.0.1')
  30.     thread3 = mytimer(3,3,'localhost')

  31.     thread1.start()
  32.     thread2.start()
  33.     thread3.start()
  34.     ##time.sleep(10)
  35.     thread1.stop()
  36.     thread2.stop()
  37.     thread3.stop()

  38.     return

  39. if __name__ == '__main__':
  40.     test()

复制代码
分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
分享淘帖0 收藏收藏0 评分评分

0

主题

0

好友

141

积分

禁止发言

性别
保密
沙发
发表于 2012-10-25 12:34:24 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 评分 举报

无效楼层,该帖已经被删除
无效楼层,该帖已经被删除
5#
无效楼层,该帖已经被删除
6#
无效楼层,该帖已经被删除
您需要登录后才可以回帖 登录 | 立即注册


手机版|SKY外语计算机学习 ( 粤ICP备12031577 )    

GMT+8, 2024-5-2 11:23 , Processed in 0.134465 second(s), 30 queries .

回顶部