1

我有一个场景,我想使用套接字来包装我的 API 调用,而不能直接在浏览器中访问 api 并获取数据。

索引.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Report</title>
  <meta charset="utf-8">
  <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
  <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.21/css/dataTables.bootstrap.min.css"/>
  <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/buttons/1.6.2/css/buttons.dataTables.min.css"/>
  <script src="https://code.jquery.com/jquery-3.5.1.js"></script>

  <script type="text/javascript" src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"></script>
  <script src="https://cdn.datatables.net/1.10.21/js/dataTables.bootstrap.min.js"></script>

  <script src="https://cdn.datatables.net/buttons/1.6.2/js/dataTables.buttons.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js"></script>
  <script src="https://cdn.datatables.net/buttons/1.6.2/js/buttons.html5.min.js"></script>

</head>
<body style="background-color:mintcream;">
<div ><span style="font-family: sans-serif;font-size: x-large;al:center;">reportyu</span> <br> <br>

<table id="example" class="table table-striped table-bordered" style="width:100%;">
        <thead>
            <tr>
                <th>Lame</th>
                <th>IAr</th>
                <th>Hee</th>
                <th>ED</th>
                <th>SPs</th>
                <th>Ts</th>
                <th>SD</th>
                <th>Rs</th>
                <th>Ld</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
               <th>Lame</th>
                <th>IAr</th>
                <th>Hee</th>
                <th>ED</th>
                <th>SPs</th>
                <th>Ts</th>
                <th>SD</th>
                <th>Rs</th>
                <th>Ld</th>
            </tr>
        </tfoot>
    </table>
<script>
function setupData() {
    $(document).ready(function () {
        $('#example').DataTable({
            responsive: true,
            "ajax": {
                "url": "/index_get_data",
                "dataType": "json",
                "dataSrc": "data",
                "contentType":"application/json"
            },
            "columns": [
                {"data": "L_name"},
                {"data": "Ia_r"},
                {"data": "He_e"},
                {"data": "e_d",
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        $(nTd).html("<a href='//abc.app.com/"+oData.e_d+"'>"+oData.e_d+"</a>");
                    }
                },
                {"data": "s_ps"},
                {"data": "t_s"},
                {"data": "s_d",
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        $(nTd).html("<a href='//app2.com/"+oData.s_d+"'>"+oData.s_d+"</a>");
                    }
                },
                {"data": "R_n",
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        $(nTd).html("<a href='//sshse.com/"+oData.R_n+"'>"+oData.R_n+"</a>");
                    }
                },
                {"data": "l_d"}

            ],
            dom: 'Bfrtip',
                buttons: [
                    {
                        extend: 'pdfHtml5',
                        orientation: 'landscape',
                        pageSize: 'LEGAL'
                    },
                    'copyHtml5',
                    'excelHtml5'
        ]
        });

    });
}
$( window ).on( "load", setupData );
</script>
</body>
</html>

这是我的 app.py :

from flask import Flask, render_template, jsonify
import json

app = Flask(__name__)

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

@app.route('/index_get_data')
def stuff():
  # Assume data comes from somewhere else
  with open('crtp.json') as test_file:
    data = json.load(test_file)
  return jsonify(data)


if __name__ == '__main__':
  app.run()

这可以按预期工作,但是当我在浏览器中点击 url 时localhost:5000,我还可以在浏览器中看到 API 调用network tab,然后如果我点击端点localhost:5000/index_get_data ,我就可以公开使用 API。我已经探索了发送token based auth标头的选项,basic auth但仍然可以派生响应,并且我想要限制的 API 仍然开放供使用,我在这里遇到了flask-sockets这似乎有帮助,但不知道如何在这里实现它。

关于这个或任何其他方法来实现这一点的任何帮助都会很棒。

4

1 回答 1

1

目前尚不清楚您真正想要做什么,以及为什么您不希望“浏览器”能够看到您的端点。Flask(和浏览器)使用 HTTP 和 HTTPS 进行通信。有大量信息、软件包等关于所有这些如何工作——如果你对任何类型的互操作性感兴趣——你应该坚持使用完善的协议。您可能会问如何保护您的端点,以便只有具有适当凭据的客户端才能访问您的站点。这就是 Flask-Security-Too、Flask-Dance、Flask-Login 等软件包所提供的。Flask 本身不提供任何端点保护

于 2020-05-31T14:31:34.587 回答