這個問題的目標是:
目標是開發一個監視遠端伺服器上日誌檔案的系統,類似於 Unix 命令 tail -f。日誌檔案不斷新增資料。系統應包括:
一個伺服器應用程序,用於追蹤同一伺服器上指定日誌檔案的持續變更。該應用程式應該能夠將新添加的數據即時傳輸給客戶端。
基於 Web 的客戶端介面,可透過 URL(例如 http://localhost/log)訪問,旨在動態顯示日誌檔案更新,而不需要使用者重新載入頁面。最初,在造訪該頁面時,使用者應該會看到日誌檔案中的最新 10 行。
也處理了以下場景:伺服器必須主動向客戶端推送更新,以確保最小延遲,以實現盡可能接近即時的更新。
鑑於日誌檔案可能非常大(可能有幾 GB),您需要製定一種策略來有效取得最後 10 行而不處理整個檔案。
伺服器應僅將檔案的新新增內容傳輸給客戶端,而不是重新傳送整個檔案。
伺服器支援來自多個客戶端的並發連接而不降低效能至關重要。
客戶端的網頁應立即加載,而不是在初始請求後停留在加載狀態,並且不應該需要重新加載來顯示新的更新。
我創建了一個 Flask 應用程序,它具有簡單的 UI,可顯示最後 10 個訊息。
我使用了flask-socketio來形成連接,也使用了處理文件的一些基本概念,如fileObj.seek()、fileObj.tell()等
from flask import Flask, render_template from flask_socketio import SocketIO, emit from threading import Lock app = Flask(__name__) socketio = SocketIO(app) thread = None thread_lock = Lock() LOG_FILE_PATH = "./static/client.txt" last_position = 0 position_lock = Lock() @app.route('/') def index(): return render_template('index.html') @socketio.on('connect') def test_connect(): global thread with thread_lock: if thread is None: print("started execution in background!") thread = socketio.start_background_task(target=monitor_log_file) def monitor_log_file(): global last_position while True: try: with open(LOG_FILE_PATH, 'rb') as f: f.seek(0, 2) file_size = f.tell() if last_position != file_size: buffer_size = 1024 if file_size <pre class="brush:php;toolbar:false"> <meta charset="UTF-8"> <title>Basics</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.5.4/socket.io.js"></script> <h1 id="User-Updated-Files-Display-it-over-here">User Updated Files Display it over here:</h1> <div id="output"></div> <script> var socket = io("http://127.0.0.1:5000"); socket.on('connect', function() { console.log('Connected to the server'); }); socket.on('disconnect', function() { console.log('Client disconnected'); }); socket.on('log_updates', function(data) { console.log("data", data); var div = document.getElementById('output'); var lines = data.content.split('\n'); div.innerHTML = ''; lines.forEach(function(line) { var p = document.createElement('p'); p.textContent = line; div.appendChild(p); }); }); </script>
還在 Flask 應用程式的 static 資料夾下建立一個 client.log 檔案。
如果我做錯了什麼,請隨時糾正我。有任何更正請在下面評論!
以上是開發一個監視位於遠端伺服器上的記錄檔的系統,類似 Unix 指令 tail -f。的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Tomergelistsinpython,YouCanusethe操作員,estextMethod,ListComprehension,Oritertools

在Python3中,可以通過多種方法連接兩個列表:1)使用 運算符,適用於小列表,但對大列表效率低;2)使用extend方法,適用於大列表,內存效率高,但會修改原列表;3)使用*運算符,適用於合併多個列表,不修改原列表;4)使用itertools.chain,適用於大數據集,內存效率高。

使用join()方法是Python中從列表連接字符串最有效的方法。 1)使用join()方法高效且易讀。 2)循環使用 運算符對大列表效率低。 3)列表推導式與join()結合適用於需要轉換的場景。 4)reduce()方法適用於其他類型歸約,但對字符串連接效率低。完整句子結束。

pythonexecutionistheprocessoftransformingpypythoncodeintoExecutablestructions.1)InternterPreterReadSthecode,ConvertingTingitIntObyTecode,whepythonvirtualmachine(pvm)theglobalinterpreterpreterpreterpreterlock(gil)the thepythonvirtualmachine(pvm)

Python的關鍵特性包括:1.語法簡潔易懂,適合初學者;2.動態類型系統,提高開發速度;3.豐富的標準庫,支持多種任務;4.強大的社區和生態系統,提供廣泛支持;5.解釋性,適合腳本和快速原型開發;6.多範式支持,適用於各種編程風格。

Python是解釋型語言,但也包含編譯過程。 1)Python代碼先編譯成字節碼。 2)字節碼由Python虛擬機解釋執行。 3)這種混合機制使Python既靈活又高效,但執行速度不如完全編譯型語言。

UseeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.forloopsareIdealForkNownsences,而WhileLeleLeleLeleLeleLoopSituationSituationsItuationsItuationSuationSituationswithUndEtermentersitations。

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐個偏置,零indexingissues,andnestedloopineflinefficiencies


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SublimeText3漢化版
中文版,非常好用

Dreamweaver Mac版
視覺化網頁開發工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!