【问题标题】:Print same output as python console in flask rendered HTML [duplicate]在烧瓶渲染的 HTML 中打印与 python 控制台相同的输出 [重复]
【发布时间】:2020-09-10 17:54:55
【问题描述】:

我有一个 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 控制台相同的输出,这是我使用烧瓶时所拥有的:

@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)

【问题讨论】:

  • 把它放在<pre>标签里?
  • 可以贴html代码吗?

标签: python html flask ssh paramiko


【解决方案1】:

非常感谢!!我在 result.html 中缺少 <pre> 标签

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    • 2020-06-03
    • 1970-01-01
    • 2011-02-23
    • 2020-12-16
    相关资源
    最近更新 更多