在HTML中打印与python控制台相同的输出

2024-04-23 12:04:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python脚本,可以打印来自网络设备的控制台输出,它工作得很好,我得到的输出如下:Python Console Output

现在,我尝试使用Flask在web服务器上运行它,它工作得很好,只是呈现的HTML模板中的输出是一个字符串,如下所示:

display mac-address 44FB-5ABC-9788
--------------------------------------------------------------------------- 
MAC Address VLAN/VSI/BD Learned-From Type 
------------------------------------------------------------------------------
44fb-5abc-9788 1579/-/- Eth-Trunk1 amic
------------------------------------------------------------------------------ 
Total items displayed = 1 

我想要的是HTML页面中的输出与python控制台中的输出相同,以下是我对flask的了解:

@app.route('/result',methods = ['POST', 'GET']) 
def result():    
    if request.method == 'POST':
        hw_input = str(request.form.get("hw"))
        commandinput = str(request.form.get("command"))
        user = 'user'
        passv = 'pass'
        passg = 'password'
        user_prompt = '.*name:'
        pass_prompt =  '.*assword:'
        hw_prompt = '.*>'
        hw_connect = 'telnet ' + hw_input

        ssh_client = paramiko.SSHClient() 
        ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh_client.connect(hostname='host', username=user, password=passv)

        interact = SSHClientInteraction(ssh_client, timeout=10, display=False)
        interact.send(hw_connect)
        interact.expect(user_prompt)
        interact.send(user)
        interact.expect(pass_prompt)
        interact.send(passg)
        interact.expect(hw_prompt)
        interact.send(commandinput)
        interact.expect(hw_prompt)
        output = interact.current_output_clean
        return render_template("result.html",result = output)

Tags: clientsendoutputrequesthtmlconnectdisplaypass