Alessandro Gaballo
Alessandro Gaballo

Reputation: 770

Can't get external visibility on flask server

I'm running a server on python using flask, i've read the documentation but i still can't get external visibility. Here's the code i'm trying to test.

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

When I open this html for testing from another pc in my LAN, i can't connect to the server, what i am missing?

$(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>

Upvotes: 1

Views: 1309

Answers (2)

Igor Chubin
Igor Chubin

Reputation: 64563

Your problem is that in javascript you use 0.0.0.0 to access the server, and you should use it's reall address or omit the servername at all.

Another option (the best solution for $.get) is to use location.host and location.protocol:

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

Upvotes: 1

Martin Konecny
Martin Konecny

Reputation: 59571

When I open this html for testing from another pc in my LAN

In your server code 0.0.0.0, means bind to all network interfaces. It does not represent the IP address of that server.

Therefore you cannot connect to http://0.0.0.0:5000/api/v1.0/map from your client on a different machine as it is not a valid IP!

You need to find the correct IP address of your server, and then replace 0.0.0.0 in the client code with that. Since it appears you are using Linux as the server, open a terminal on the server and type

sudo ifconfig

Looks for the IP address listed under eth0 (network cable) or wlan0 (wireless internet)

Upvotes: 4

Related Questions