首页 > 代码库 > python flask初体验(一)linux命令发射器

python flask初体验(一)linux命令发射器

<html>
<head>
    <link rel="stylesheet" type="text/css" href=http://www.mamicode.com/"{{ url_for(‘static‘, filename=‘bootstrap.css‘) }}" />
    <script type=text/javascript src=http://www.mamicode.com/"{{ url_for(‘static‘, filename=‘jquery.min.js‘) }}"></script>
    <!--Post请求-->
    <script>
        $(function () {
            function submit_form(e) {
                var data = {
                    name: $("#name").val(),
                    {#                      ‘age‘: $("#age").val(),#}
                }
                $.ajax({
                    type: POST,
                    url: {{url_for("test_post")}},
                    data: data,
                    dataType: text,//希望服务器返回json格式的数据
                    success: function (data) {
                        alert(JSON.stringify(data));
                        var newdata = data;
                        var table = $("#ListTable");
                        var body = table.find(tbody:eq(0));
                        body.empty();
                        var tr;
                        var jieguo = newdata.split("\\n");
                        for(i in jieguo){
                            tr = $("<tr id=‘aaaaaaaa_" + i + "‘ ></tr>");
                            tr.append($("<td>" + jieguo[i]+ "</td>"));
                            table.append(tr);
                        }

                    }
                });
            }

            $(#button).bind(click, submit_form);
        });
    </script>
</head>
<body>
<h1>梦想linux命令发射器</h1>
<span style="color: red;font-size: 24px;">请输入Linux命令:</span><input name="name" id="name" style="widh:110px;height:30px;" type="text"/>
<button id="button" class="btn btn-info">发送按钮</button>
<table id="ListTable">
    <tbody></tbody>
</table>
<h3>Hello: <span id="name" style="border-bottom: darkred solid 5px;">{{ title+\n }} result</span></h3>
</body>

以上是前台代码

# -*- coding: utf-8 -*-
from flask import Flask, jsonify, request, render_template
import sys
import paramiko

app = Flask(__name__)

@app.route(/)
def hello_world():
    return render_template(index.html, title="this is")

@app.route(/test_post/,methods=[POST,GET])
def test_post():
    name=request.form.get(name)
    hostname = ip
    command = str(name)
    port = 22
    username = "username"
    password = "pwd"
    s = paramiko.SSHClient()
    s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    s.connect(hostname, port, username, password)
    stdin, stdout, sterr = s.exec_command(command)
    z = stdout.read()
    s.close()
    return jsonify({result: z})
if __name__ == "__main__":
    app.run(
        port=7777,
        debug=True
    )

以上是后台代码

我的感受:通过本次学习我了解到了,flask路由,jinja2,还有模板的使用,代码需要日日练习,天天积累。我会坚持分享

python flask初体验(一)linux命令发射器