最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于Python編寫簡單的網(wǎng)絡(luò)測試工具

 更新時間:2025年02月05日 10:01:42   作者:mosquito_lover1  
這篇文章主要為大家詳細(xì)介紹了如何基于Python編寫一個簡單的網(wǎng)絡(luò)測試工具,可以測試網(wǎng)絡(luò)的下載速度,上傳速度和延遲,感興趣的可以了解下

工具介紹

這是一個功能完整的網(wǎng)絡(luò)測速工具,可以測試網(wǎng)絡(luò)的下載速度、上傳速度和延遲。

功能特點

1. 速度測試

  • 下載速度測試
  • 上傳速度測試
  • Ping延遲測試
  • 自動選擇最佳服務(wù)器

2. 實時顯示

  • 進(jìn)度條顯示測試進(jìn)度
  • 實時顯示測試狀態(tài)
  • 清晰的數(shù)據(jù)展示

3. 歷史記錄

  • 保存測試歷史
  • 顯示最近6次測試結(jié)果
  • 支持導(dǎo)出歷史記錄

使用要求

Python 3.6+

需要安裝的庫:

python -m pip install speedtest-cli

使用方法

1. 安裝依賴:

  • 首先安裝必要的庫
  • 確保網(wǎng)絡(luò)連接正常

2. 開始測速:

  • 點擊"開始測速"按鈕
  • 等待測試完成(約1-2分鐘)
  • 查看測試結(jié)果

3. 歷史記錄:

  • 自動保存每次測試結(jié)果
  • 查看最近的測試歷史
  • 可導(dǎo)出完整歷史記錄

完整代碼

import tkinter as tk
from tkinter import ttk, messagebox
try:
    import speedtest
except ImportError:
    messagebox.showerror("錯誤", "請先安裝 speedtest-cli:\npip install speedtest-cli")
    raise
import threading
import time
from datetime import datetime
import json
import os
from pathlib import Path
 
class NetworkSpeedTest:
    def __init__(self):
        self.window = tk.Tk()
        self.window.title("網(wǎng)絡(luò)測速工具")
        self.window.geometry("600x500")
        
        # 創(chuàng)建主框架
        self.main_frame = ttk.Frame(self.window, padding="10")
        self.main_frame.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
        
        # 測速結(jié)果顯示
        self.setup_display()
        
        # 控制按鈕
        self.setup_controls()
        
        # 歷史記錄
        self.setup_history()
        
        # 初始化speedtest
        self.st = None
        self.testing = False
        self.history_file = Path.home() / '.speedtest_history.json'
        self.load_history()
        
    def setup_display(self):
        # 當(dāng)前速度顯示
        display_frame = ttk.LabelFrame(self.main_frame, text="測速結(jié)果", padding="10")
        display_frame.grid(row=0, column=0, sticky=(tk.W, tk.E), pady=10)
        
        # 下載速度
        ttk.Label(display_frame, text="下載速度:").grid(row=0, column=0, pady=5)
        self.download_speed = ttk.Label(display_frame, text="-- Mbps")
        self.download_speed.grid(row=0, column=1, padx=20)
        
        # 上傳速度
        ttk.Label(display_frame, text="上傳速度:").grid(row=1, column=0, pady=5)
        self.upload_speed = ttk.Label(display_frame, text="-- Mbps")
        self.upload_speed.grid(row=1, column=1, padx=20)
        
        # Ping值
        ttk.Label(display_frame, text="Ping延遲:").grid(row=2, column=0, pady=5)
        self.ping = ttk.Label(display_frame, text="-- ms")
        self.ping.grid(row=2, column=1, padx=20)
        
        # 服務(wù)器信息
        ttk.Label(display_frame, text="測速服務(wù)器:").grid(row=3, column=0, pady=5)
        self.server_info = ttk.Label(display_frame, text="--")
        self.server_info.grid(row=3, column=1, padx=20)
        
        # 進(jìn)度條
        self.progress = ttk.Progressbar(display_frame, length=300, mode='determinate')
        self.progress.grid(row=4, column=0, columnspan=2, pady=10)
        
        # 狀態(tài)標(biāo)簽
        self.status = ttk.Label(display_frame, text="就緒")
        self.status.grid(row=5, column=0, columnspan=2)
        
    def setup_controls(self):
        control_frame = ttk.Frame(self.main_frame)
        control_frame.grid(row=1, column=0, pady=10)
        
        self.start_button = ttk.Button(control_frame, text="開始測速", command=self.start_test)
        self.start_button.grid(row=0, column=0, padx=5)
        
        ttk.Button(control_frame, text="導(dǎo)出歷史", command=self.export_history).grid(row=0, column=1, padx=5)
        
    def setup_history(self):
        history_frame = ttk.LabelFrame(self.main_frame, text="歷史記錄", padding="10")
        history_frame.grid(row=2, column=0, sticky=(tk.W, tk.E), pady=10)
        
        # 創(chuàng)建表格
        columns = ('time', 'download', 'upload', 'ping')
        self.history_tree = ttk.Treeview(history_frame, columns=columns, height=6)
        
        self.history_tree.heading('time', text='時間')
        self.history_tree.heading('download', text='下載(Mbps)')
        self.history_tree.heading('upload', text='上傳(Mbps)')
        self.history_tree.heading('ping', text='Ping(ms)')
        
        self.history_tree.column('#0', width=0, stretch=tk.NO)
        self.history_tree.column('time', width=150)
        self.history_tree.column('download', width=100)
        self.history_tree.column('upload', width=100)
        self.history_tree.column('ping', width=100)
        
        self.history_tree.grid(row=0, column=0)
        
    def load_history(self):
        if self.history_file.exists():
            try:
                with open(self.history_file, 'r') as f:
                    self.history = json.load(f)
                self.update_history_display()
            except:
                self.history = []
        else:
            self.history = []
            
    def save_history(self):
        with open(self.history_file, 'w') as f:
            json.dump(self.history, f)
            
    def update_history_display(self):
        for item in self.history_tree.get_children():
            self.history_tree.delete(item)
            
        for record in self.history[-6:]:  # 只顯示最近6條記錄
            self.history_tree.insert('', 0, values=(
                record['time'],
                f"{record['download']:.1f}",
                f"{record['upload']:.1f}",
                f"{record['ping']:.0f}"
            ))
            
    def start_test(self):
        if self.testing:
            return
            
        self.testing = True
        self.start_button['state'] = 'disabled'
        self.progress['value'] = 0
        self.status['text'] = "正在初始化..."
        
        # 在新線程中運行測速
        threading.Thread(target=self.run_speedtest, daemon=True).start()
        
    def run_speedtest(self):
        try:
            # 初始化
            self.status['text'] = "正在連接到測速服務(wù)器..."
            self.st = speedtest.Speedtest()
            self.progress['value'] = 20
            
            # 選擇服務(wù)器
            self.status['text'] = "正在選擇最佳服務(wù)器..."
            server = self.st.get_best_server()
            self.server_info['text'] = f"{server['sponsor']} ({server['name']})"
            self.progress['value'] = 40
            
            # 測試下載速度
            self.status['text'] = "正在測試下載速度..."
            download_speed = self.st.download() / 1_000_000  # 轉(zhuǎn)換為Mbps
            self.download_speed['text'] = f"{download_speed:.1f} Mbps"
            self.progress['value'] = 60
            
            # 測試上傳速度
            self.status['text'] = "正在測試上傳速度..."
            upload_speed = self.st.upload() / 1_000_000  # 轉(zhuǎn)換為Mbps
            self.upload_speed['text'] = f"{upload_speed:.1f} Mbps"
            self.progress['value'] = 80
            
            # 獲取ping值
            ping_time = server['latency']
            self.ping['text'] = f"{ping_time:.0f} ms"
            self.progress['value'] = 100
            
            # 保存結(jié)果
            self.history.append({
                'time': datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
                'download': download_speed,
                'upload': upload_speed,
                'ping': ping_time
            })
            self.save_history()
            self.update_history_display()
            
            self.status['text'] = "測速完成"
            
        except Exception as e:
            messagebox.showerror("錯誤", f"測速過程中出錯:{str(e)}")
            self.status['text'] = "測速失敗"
            
        finally:
            self.testing = False
            self.start_button['state'] = 'normal'
            
    def export_history(self):
        if not self.history:
            messagebox.showinfo("提示", "沒有歷史記錄可供導(dǎo)出")
            return
            
        file_path = tk.filedialog.asksaveasfilename(
            defaultextension=".csv",
            filetypes=[("CSV files", "*.csv")],
            initialfile="speedtest_history.csv"
        )
        
        if file_path:
            try:
                with open(file_path, 'w', encoding='utf-8') as f:
                    f.write("時間,下載速度(Mbps),上傳速度(Mbps),Ping延遲(ms)\n")
                    for record in self.history:
                        f.write(f"{record['time']},{record['download']:.1f},"
                               f"{record['upload']:.1f},{record['ping']:.0f}\n")
                messagebox.showinfo("成功", "歷史記錄已導(dǎo)出")
            except Exception as e:
                messagebox.showerror("錯誤", f"導(dǎo)出過程中出錯:{str(e)}")
                
    def run(self):
        self.window.mainloop()
 
if __name__ == "__main__":
    app = NetworkSpeedTest()
    app.run() 

效果圖

以上就是基于Python編寫簡單的網(wǎng)絡(luò)測試工具的詳細(xì)內(nèi)容,更多關(guān)于Python網(wǎng)絡(luò)測試的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • python中opencv支持向量機的實現(xiàn)

    python中opencv支持向量機的實現(xiàn)

    本文主要介紹了python中opencv支持向量機的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Django ModelForm組件原理及用法詳解

    Django ModelForm組件原理及用法詳解

    這篇文章主要介紹了Django ModelForm組件原理及用法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-10-10
  • Python實現(xiàn)多條件篩選Excel數(shù)據(jù)并批量繪制直方圖

    Python實現(xiàn)多條件篩選Excel數(shù)據(jù)并批量繪制直方圖

    這篇文章主要為大家介紹了如何Python對Excel數(shù)據(jù)進(jìn)行多條件篩選和去除并批量繪制直方圖,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下
    2023-09-09
  • python實現(xiàn)圖片,視頻人臉識別(dlib版)

    python實現(xiàn)圖片,視頻人臉識別(dlib版)

    這篇文章主要介紹了python實現(xiàn)圖像,視頻人臉識別(dlib版)的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
    2020-11-11
  • python Scrapy爬蟲框架的使用

    python Scrapy爬蟲框架的使用

    這篇文章主要介紹了python Scrapy爬蟲框架的使用,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2021-01-01
  • Python input()函數(shù)案例教程

    Python input()函數(shù)案例教程

    在 Python 中,input() 函數(shù)用于獲取用于的輸入,并給出提示。input() 函數(shù),總是返回 string 類型,因此,我們可以使用 input() 函數(shù),獲取用戶輸入的任何數(shù)據(jù)類型 ,這篇文章主要介紹了Python input()函數(shù)案例詳解,需要的朋友可以參考下
    2023-01-01
  • Python數(shù)據(jù)可視化之環(huán)形圖

    Python數(shù)據(jù)可視化之環(huán)形圖

    這篇文章主要介紹了Python數(shù)據(jù)可視化之環(huán)形圖,主要使用兩種不同的方式來可視化環(huán)形圖,并均給出了完整的代碼示例。需要的朋友可以參考一下,希望對你的工作和學(xué)習(xí)有所幫助
    2022-01-01
  • Python辦公自動化解決world文件批量轉(zhuǎn)換

    Python辦公自動化解決world文件批量轉(zhuǎn)換

    本文分享如何用 Python 來讀取 Word、寫入 Word、將 Word 轉(zhuǎn)換為 pdf。學(xué)會之后,如果遇到大量 Word 文件需要處理的時候,就不慌了
    2021-09-09
  • Python中with的作用和使用解讀

    Python中with的作用和使用解讀

    Python的with語句通過上下文管理器協(xié)議(__enter__和__exit__方法),簡化資源管理,確保文件、數(shù)據(jù)庫等資源在使用后自動釋放,避免泄露,支持自定義實現(xiàn),提升代碼簡潔性與可讀性
    2025-07-07
  • Python中is與==判斷的區(qū)別

    Python中is與==判斷的區(qū)別

    在python中,is檢查兩個對象是否是同一個對象,而==檢查他們是否相等.這樣說起來很簡單,我們通過具體的實例來分析吧
    2017-03-03

最新評論

广丰县| 阳原县| 新兴县| 任丘市| 高平市| 绥芬河市| 吉安市| 横峰县| 高平市| 仁寿县| 北宁市| 青州市| 扎赉特旗| 泸西县| 万宁市| 满洲里市| 崇阳县| 衢州市| 五峰| 江达县| 井冈山市| 龙陵县| 山西省| 威海市| 沙湾县| 霞浦县| 慈溪市| 南皮县| 隆化县| 延川县| 阜新市| 若羌县| 饶平县| 苍南县| 阿拉善右旗| 六安市| 临泉县| 稷山县| 台中县| 闽侯县| 桐梓县|