要实现这个系统,可以使用以下步骤:

  1. 在网页上创建两个按钮A和B,并使用JavaScript为按钮添加点击事件。
  2. 当按钮A被点击时,使用Ajax请求将命令“iperf3 -s -p 1111”发送到后台。
  3. 后台接收到请求后,使用子进程启动该命令,并将命令的输出结果实时发送回前端。
  4. 前端接收到命令输出结果后,将结果显示在网页上。
  5. 当按钮B被点击时,再次使用Ajax请求将停止命令的请求发送到后台。
  6. 后台接收到停止命令的请求后,终止正在运行的命令进程。

下面是一个使用Python Flask框架实现该系统的示例代码:

# app.py

import subprocess
from flask import Flask, render_template, request, jsonify

app = Flask(__name__)
process = None

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/start', methods=['POST'])
def start():
    global process
    if process:
        return jsonify({'message': 'Command already running.'})

    process = subprocess.Popen(['iperf3', '-s', '-p', '1111'], stdout=subprocess.PIPE, universal_newlines=True)

    def stream():
        while process.poll() is None:
            output = process.stdout.readline().strip()
            yield output + '\n'

    return app.response_class(stream(), mimetype='text/plain')

@app.route('/stop', methods=['POST'])
def stop():
    global process
    if process:
        process.terminate()
        process = None
        return jsonify({'message': 'Command stopped.'})
    else:
        return jsonify({'message': 'No command running.'})

if __name__ == '__main__':
    app.run()
<!-- index.html -->

<!DOCTYPE html>
<html>
<head>
    <title>System</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
    <button id="start">Start</button>
    <button id="stop">Stop</button>
    <pre id="output"></pre>
    <script>
        $(document).ready(function() {
            var output = $('#output');

            $('#start').click(function() {
                $.ajax({
                    url: '/start',
                    type: 'POST',
                    success: function(data) {
                        output.html('');
                        var source = new EventSource('/stream');
                        source.onmessage = function(event) {
                            output.append(event.data);
                        }
                    },
                    error: function(xhr, status, error) {
                        console.error(error);
                    }
                });
            });

            $('#stop').click(function() {
                $.ajax({
                    url: '/stop',
                    type: 'POST',
                    success: function(data) {
                        output.html('');
                    },
                    error: function(xhr, status, error) {
                        console.error(error);
                    }
                });
            });
        });
    </script>
</body>
</html>

在这个示例中,我们使用Flask框架创建了一个简单的Web应用。当按钮A被点击时,发送一个POST请求到/start路由,该路由启动iperf3命令并将输出结果以流的形式发送回前端。当按钮B被点击时,发送一个POST请求到/stop路由,该路由终止正在运行的命令进程。输出结果以文本的形式显示在网页上的<pre>标签中。

请注意,为了能够实时接收命令输出结果,我们使用了Server-Sent Events(SSE)技术,通过使用EventSource对象从/stream路由接收命令输出的流

请用实现一个系统该系统有一个网页作为输入和输出网页上有2个按钮点击A按钮启动一个进程运行如下命令iperf3 -s -p 1111并把输出结果实时显示到网页中。点击按钮B时停止这个命令。

原文地址: http://www.cveoy.top/t/topic/iRKt 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录