Flask iPerf3 Server with Real-time Output Streaming and Automatic Index.html Generation
{ "title": "Flask iPerf3 Server with Real-time Output Streaming and Automatic Index.html Generation", "description": "This Python Flask application acts as an iPerf3 server, providing real-time output streaming for network performance testing. It also includes a feature to automatically generate an index.html file for easier access.", "keywords": "flask, iperf3, server, streaming, real-time, output, network, performance, testing, index.html, automatic, generation", "content": "# app.py\n\nimport subprocess\nfrom flask import Flask, render_template, request, jsonify, Response, stream_with_context\nfrom flask import render_template_string\n\napp = Flask(name, template_folder='D:\temp\dddd\')\nprocess = None\noutput_list = []\nstream_list = []\n\n@app.route('/')\ndef index():\n return render_template('index.html')\n\n@app.route('/start', methods=['POST'])\ndef start():\n global process, output_list, stream_list\n if process:\n return jsonify({'message': 'Command already running.'})\n\n process = subprocess.Popen(['iperf3', '-s', '-p', '1111'], stdout=subprocess.PIPE, universal_newlines=True)\n\n def stream():\n while process and process.poll() is None:\n output = process.stdout.readline().strip()\n stream_list.append(output)\n yield output + '\n'\n return Response(stream_with_context(stream()), mimetype='text/plain')\n\n@app.route('/stream')\ndef stream():\n global stream_list\n if stream_list:\n def generate():\n while stream_list:\n output = stream_list.pop(0)\n yield output + '\n'\n return Response(generate(), mimetype='text/event-stream')\n else:\n def empty_stream():\n yield 'data: \n\n'\n return Response(empty_stream(), mimetype='text/event-stream')\n\n@app.route('/stop', methods=['POST'])\ndef stop():\n global process, output_list, stream_list\n if process:\n process.terminate()\n process = None\n output_list = []\n stream_list = []\n return jsonify({'message': 'Command stopped.'})\n else:\n return jsonify({'message': 'No command running.'})\n\n@app.route('/create_index')\ndef create_index():\n template = '''\n \n \n
\nWelcome to the Index Page!
\n \n \n '''\n return render_template_string(template)\n\nif name == 'main':\n app.run(debug=True)\n
原文地址: https://www.cveoy.top/t/topic/qx8n 著作权归作者所有。请勿转载和采集!