from flask import Flask, jsonify, render_template_string import psutil import time import subprocess app = Flask(__name__) # Template with JS to fetch data every second HTML_TEMPLATE = """
CPU Temp: Loading... °C
Network: ↑ 0 KB/s | ↓ 0 KB/s
""" @app.route('/') def index(): return render_template_string(HTML_TEMPLATE) @app.route('/stats') def stats(): cpu = psutil.cpu_percent(interval=0.5) # Network stats net1 = psutil.net_io_counters() time.sleep(0.5) net2 = psutil.net_io_counters() up_speed = round((net2.bytes_sent - net1.bytes_sent) / 1024, 1) down_speed = round((net2.bytes_recv - net1.bytes_recv) / 1024, 1) # Get temperature using sensors try: sensors_output = subprocess.check_output(['sensors']).decode() temp_line = next(line for line in sensors_output.splitlines() if 'Package id 0' in line or 'Tctl' in line) temp = ''.join([c for c in temp_line if c.isdigit() or c == '.'])[:5] except Exception: temp = 'N/A' return jsonify({ 'cpu': cpu, 'temp': temp, 'net_up': up_speed, 'net_down': down_speed }) if __name__ == '__main__': app.run(host='0.0.0.0', port=5001)