基于Python編寫一個IP地址存活檢查器
更新時間:2024年11月05日 08:45:39 作者:蝸牛其實也很努力
這篇文章主要為大家詳細(xì)介紹了如何基于Python編寫一個IP地址存活檢查器,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
代碼
import tkinter as tk import subprocess import threading import ipaddress from concurrent.futures import ThreadPoolExecutor import os def ping_ip(ip): try: ping_command = ["ping", "-c", "1", str(ip)] if os.name != 'nt' else ["ping", "-n", "1", str(ip)] output = subprocess.run(ping_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) if output.returncode == 0: return f"{ip} 被占用\n", "occupied" else: return f"{ip} 未被占用\n", "free" except Exception as e: return f"檢查 {ip} 時出錯: {str(e)}\n", None def update_result(result, tag): result_text.config(state=tk.NORMAL) result_text.insert(tk.END, result, tag) result_text.config(state=tk.DISABLED) def check_ip_range(cidr): try: network = ipaddress.ip_network(cidr) except ValueError as e: update_result(f"無效的 CIDR: {str(e)}\n", None) return with ThreadPoolExecutor(max_workers=20) as executor: future_to_ip = {executor.submit(ping_ip, ip): ip for ip in network.hosts()} for future in future_to_ip: result, tag = future.result() if tag == "occupied": update_result(result, "occupied") elif tag == "free": update_result(result, "free") else: update_result(result, None) def on_check_button_click(): cidr = cidr_entry.get() threading.Thread(target=check_ip_range, args=(cidr,)).start() # 創(chuàng)建主窗口 root = tk.Tk() root.title("IP 地址存活檢查器") # 輸入框 tk.Label(root, text="Example:192.168.1.0/24):").pack(pady=5) cidr_entry = tk.Entry(root, width=20) cidr_entry.pack(pady=5) # 檢查按鈕 check_button = tk.Button(root, text="Check", command=on_check_button_click) check_button.pack(pady=10) # 結(jié)果文本框 result_text = tk.Text(root, width=50, height=20, state=tk.DISABLED) result_text.pack(pady=10) # 設(shè)置文本標(biāo)簽的顏色 result_text.tag_config("occupied", foreground="red") result_text.tag_config("free", foreground="green") # 運行主循環(huán) root.mainloop()
安裝pyinstaller
制作exe
同級目錄dist下會生成程序 ,運行效果如下
到此這篇關(guān)于基于Python編寫一個IP地址存活檢查器的文章就介紹到這了,更多相關(guān)Python IP地址存活檢查器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python-tornado的接口用swagger進(jìn)行包裝的實例
今天小編就為大家分享一篇python-tornado的接口用swagger進(jìn)行包裝的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08Python函數(shù)式編程實現(xiàn)登錄注冊功能
這篇文章主要為大家詳細(xì)介紹了Python函數(shù)式編程實現(xiàn)登錄注冊功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02使用python下載大型文件顯示進(jìn)度條和下載時間的操作代碼
大家都知道下載大型文件時存在一個問題,那就是內(nèi)存使用量迅速上升,可能會造成電腦卡死,所以我們需要換一個方式進(jìn)行下載,這篇文章主要介紹了使用python下載大型文件的方法顯示進(jìn)度條和下載時間,需要的朋友可以參考下2022-11-11