0

I am currently trying to create an endpoint which can be called by other computers on the same network as me. Following is the sample code I am working with

from flask import Flask, request, jsonify
import requests

app = Flask(__name__)


countries = [
    {"id": 1, "name": "Thailand", "capital": "Bangkok", "area": 513120},
    {"id": 2, "name": "Australia", "capital": "Canberra", "area": 7617930},
    {"id": 3, "name": "Egypt", "capital": "Cairo", "area": 1010408},
]

@app.get("/countries")
def get_countries():
    return jsonify(countries)

Currently the above endpoint can be called at

http://127.0.0.1:5000/countries

from my own PC. How can I configure it such that it can be called from other computers on my network. I tried replacing 127.0.0.1 with my PC name but connection was refused.

CS1999
  • 303
  • 2
  • 10

1 Answers1

1

You can use the host argument

if __name__ = "__main__":
   app.run(host="0.0.0.0", port=5000)

With your pc's ip you can access to it from any pc in the network.

And lastly do not forget to open your port for this example it is 5000, if windows you need to allow incoming at the windows defender and if linux you can check netstat options.

Carmelot
  • 46
  • 2