python flask初體驗(一)linux命令發射器

來源:互聯網
上載者:User

標籤:connect   order   請求   返回   java   stdin   btn   pass   boot   

<html><head>    <link rel="stylesheet" type="text/css" href="{{ url_for(‘static‘, filename=‘bootstrap.css‘) }}" />    <script type=text/javascript src="{{ 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_templateimport sysimport paramikoapp = 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命令發射器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.