'How can i have connection to 0.0.0.0 port 5000?

I'm Creating an endpoint that retrieves the number of each objects by type. when i execute the code with:

curl -X GET http://0.0.0.0:5000/api/v1/stats

it says:

curl: (7) Failed to connect to 0.0.0.0 port 5000: Connection refused

i've been trying to see if I have a config file, checked my listed ports, Google on how to change/activate the port, & still haven't found something.

This is my full code:

''' module that makes this an app '''

from flask import Flask, abort, jsonify
from models import storage
from api.v1.views import app_views
from os import getenv
app = Flask(__name__)


app.register_blueprint(app_views)


@app.teardown_appcontext
def close(self):
    ''' method that close the yes '''
    storage.close()


@app.errorhandler(404)
def invalid_route(e):
    return jsonify({"error": "Not found"}), 404


if __name__ == '__main__':
    app.run(host=getenv("HBNB_API_HOST", "0.0.0.0"),
            port=int(getenv("HBNB_API_PORT", "5000")), threaded=True)```


Solution 1:[1]

Your code is completely right, you are not sending the request to the right host.

Your flask server is binded to 0.0.0.0, which means that your server will be bounded to the default address (either localhost or your actual ip address)

This should be fine: curl -X GET http://localhost:5000/api/v1/stats

Solution 2:[2]

I used an executable command like this:

HBNB_MYSQL_USER=hbnb_dev HBNB_MYSQL_PWD=hbnb_dev_pwd HBNB_MYSQL_HOST=localhost HBNB_MYSQL_DB=hbnb_dev_db HBNB_TYPE_STORAGE=db HBNB_API_HOST=0.0.0.0 HBNB_API_PORT=5000 sudo python3 -m api.v1.app

but I forgot to add sudo before python3. Then you should open a new terminal and execute the curl command. THANKS

Solution 3:[3]

I think the problem might be in the connection to the external API from a vm. When you start a DockerContainer you can specify the ports beeing map to from the vm to your localmachine using the -p Parameter. For detailed information you may wanna check the docs: https://docs.docker.com/engine/reference/commandline/port/

Solution 4:[4]

We managed to fix it by adding the proxy server config to the feign config. Apparently even when the proxy was set to work for the VM & the docker container the java code needed it explicitly.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Liron Berger
Solution 2 Luis Colon
Solution 3 Hiero
Solution 4 arkhadi