python 实现局域网探测在线主机
python 3实现局域网探测在线主机,代码如下:
1 # -*- coding: GBK -*- 2 from tkinter import * 3 import subprocess 4 import threading 5 import time 6 import re 7 8 9 def btn_click(): 10 text.insert(INSERT, ("IP地址 状态!\n")) 11 i = 0 12 j = 1 13 thread_id = [] 14 ip = int(ipText.get()) 15 v = ('192.168.10.%d' % (x) for x in range(0, ip + 1)) 16 for it in v: 17 loop = str('%s' % it) 18 thread_id.append(0) 19 thread_id[i] = Ping(loop, int(j / 20)) 20 thread_id[i].start() 21 # thread_id[i].join() 22 i = i + 1 23 j = j + 10 24 25 26 class Ping(threading.Thread): 27 def __init__(self, str_ip, sleep_time): 28 threading.Thread.__init__(self) 29 self.str_ip = str_ip 30 self.sleep_time = sleep_time 31 32 def run(self): 33 time.sleep(self.sleep_time) 34 ftp_ret = subprocess.Popen('ping %s -n 3' % self.str_ip, stdin=subprocess.PIPE, stdout=subprocess.PIPE, 35 shell=True) 36 ret = ftp_ret.stdout.read() 37 str_ret = ret.decode("gbk") 38 ret_s = re.search("TTL", str_ret) 39 if ret_s: 40 text.insert(INSERT, ("%s 在线!\n" % self.str_ip)) 41 # text.update() 42 else: 43 text.insert(INSERT, ("%s 下线!\n" % self.str_ip)) 44 45 46 root = Tk() 47 root.title('局域网Ping程序') 48 root.geometry('600x400') 49 main_frame = Frame(root) 50 text_frame = Frame(main_frame) 51 ip_frame = Frame(main_frame) 52 botton_frame = Frame(ip_frame) 53 54 l1 = Label(ip_frame, text='IP地址:192.168.10 从 0 到') 55 l2 = Label(ip_frame, text=' 线程20 ') 56 ipText = Entry(ip_frame) 57 58 l1.pack(side='left') 59 ipText.pack(side='left') 60 ipText['width'] = 5 61 l2.pack(side='left') 62 b = Button(ip_frame, text='开始', command=btn_click) 63 b['width'] = 4 64 b['height'] = 1 65 b.pack(side='left') 66 67 text = Text(text_frame, width=30, height=25) 68 text.pack() 69 main_frame.pack() 70 ip_frame.pack(side='top', pady='5') 71 text_frame.pack() 72 root.mainloop()
看效果图:

作者: 忙 碌 使 我 忘 了 伤 痛
出处: https://www.cnblogs.com/Hello-python/>
关于作者:专注python、linux、运维、IT,请多多赐教!
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接 如有问题, 可邮件(1198987828@qq.com)咨询.

浙公网安备 33010602011771号