簡體   English   中英

無法在Flask服務器上獲得外部可見性

[英]Can't get external visibility on flask server

我正在使用flask在python上運行服務器,我已經閱讀了文檔,但仍然無法獲得外部可見性。 這是我要測試的代碼。

@app.route('/api/v1.0/map')
def provideMap():
    response = {}
    response['url']=mappa
    response['bathrooms']=bathroomsState()
    result=jsonify(response)
return result;

if __name__ == '__main__':
    app.run(host='0.0.0.0',debug=False)

當我從局域網中的另一台PC打開此html進行測試時,我無法連接到服務器,我丟失了什么?

$(document).ready(function() {

        $.get( "http://0.0.0.0:5000/api/v1.0/map", function( data ) { //set the right url for the img (the url is sent by the server)
            url=data["url"];
            bathrooms=data["bathrooms"];
            document.getElementById("map").src="http://0.0.0.0:5000"+url
});


});
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>load demo</title>
  <style>
  body {
    font-size: 12px;
    font-family: Arial;
  }
  </style>
    <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
    <script src="js/prova.js"></script>


</head>
<body>
    <h1>Map</h1>
    <div id="mappa"><img id="map" src=""></img></div> 
</body>
</html>

當我打開此html從局域網中的另一台PC進行測試時

在服務器代碼0.0.0.0 ,意味着綁定到所有網絡接口。 它不代表該服務器的IP地址。

因此,您不能從另一台計算機上的客戶端連接到http://0.0.0.0:5000/api/v1.0/map ,因為它不是有效的IP!

您需要找到服務器的正確IP地址,然后用該地址替換客戶端代碼中的0.0.0.0 由於您似乎使用Linux作為服務器,因此請在服務器上打開一個終端,然后鍵入

sudo ifconfig

查找eth0 (網絡電纜)或wlan0 (無線互聯網)下列出的IP地址

您的問題是,在javascript中,您使用0.0.0.0來訪問服務器,並且應該使用它的實地址或完全省略服務器名。

另一種選擇( $.get的最佳解決方案)是使用location.hostlocation.protocol

$.get( location.protocol + location.host + "/api/v1.0/map", 
...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM