基于Python編寫一個(gè)IP地址存活檢查器
代碼
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} 時(shí)出錯(cuò): {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") # 運(yùn)行主循環(huán) root.mainloop()
安裝pyinstaller
制作exe
同級目錄dist下會生成程序 ,運(yùn)行效果如下
到此這篇關(guān)于基于Python編寫一個(gè)IP地址存活檢查器的文章就介紹到這了,更多相關(guān)Python IP地址存活檢查器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Django中modelform組件實(shí)例用法總結(jié)
在本篇文章里小編給大家整理的是關(guān)于Django中modelform組件實(shí)例用法內(nèi)容,有興趣的朋友們可以跟著學(xué)習(xí)下。2020-02-02python-tornado的接口用swagger進(jìn)行包裝的實(shí)例
今天小編就為大家分享一篇python-tornado的接口用swagger進(jìn)行包裝的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08Python函數(shù)式編程實(shí)現(xiàn)登錄注冊功能
這篇文章主要為大家詳細(xì)介紹了Python函數(shù)式編程實(shí)現(xiàn)登錄注冊功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02Matplotlib繪制混淆矩陣的實(shí)現(xiàn)
對于機(jī)器學(xué)習(xí)多分類模型來說,其評價(jià)指標(biāo)除了精度之外,常用的還有混淆矩陣和分類報(bào)告,下面來展示一下如何繪制混淆矩陣,這在論文中經(jīng)常會用到。感興趣的可以了解一下2021-05-05使用python下載大型文件顯示進(jìn)度條和下載時(shí)間的操作代碼
大家都知道下載大型文件時(shí)存在一個(gè)問題,那就是內(nèi)存使用量迅速上升,可能會造成電腦卡死,所以我們需要換一個(gè)方式進(jìn)行下載,這篇文章主要介紹了使用python下載大型文件的方法顯示進(jìn)度條和下載時(shí)間,需要的朋友可以參考下2022-11-11Python回調(diào)函數(shù)用法實(shí)例詳解
這篇文章主要介紹了Python回調(diào)函數(shù)用法,以實(shí)例形式較為詳細(xì)的分析了Python回調(diào)函數(shù)的定義、功能及相關(guān)使用技巧,需要的朋友可以參考下2015-07-07