Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't connect to a Flask server from other devices(=not from localhost)

My goal is to build a RESTful API communication, between a python server and an android (java) client.

I want to start with a client using the URL in chrome, and then with an android-java client.

I have a very simple Flask server:

from flask import Flask
app = Flask(__name__)

@app.route("/RESTfulExample/json/product/get")
def hello():
    return "Hello !!"

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

When this is localhost(ip=127.0.0.1) the communication between the server and the client works well.

The problem is when I run the client from a different device (I tried with a phone and another computer):

from flask import Flask
app = Flask(__name__)

@app.route("/RESTfulExample/json/product/get")
def hello():
    return "Hello !!"

if __name__ == "__main__":
    #app.run()
    app.run(host='10.0.0.54', threaded=True)

When I run the server and enter this url (http://10.0.0.54:5000//RESTfulExample/json/product/get) in the chrome URL, After 2-3 minutes it returns an ERR_CONNECTION_TIMED_OUT error, or an ERR_ADDRESS_UNREACHABLE error.

I don't have a clue about a solution for this simple problem. I will glad if someone here will help me with it.

Thanks in advance !!

like image 591
Omer Setty Avatar asked Jan 29 '23 12:01

Omer Setty


2 Answers

Try this

  • app.run(host='0.0.0.0', debug=True)
  • Allow TCP traffic on port 5000
  • Turn off firewall
like image 200
WK5 Avatar answered Jan 31 '23 02:01

WK5


Please set the host in the python script to

host='0.0.0.0'

With that the Server will listen on all available network Interfaces.

You also have to assign your port (also in python):

port=5000

And take care, that your firewall is not blocking requests. To change the behavior of your firewall you can use (on windows) the integrated firewall tool. There you can also easily enable TCP traffic for port 5000 in your local network.

Sometimes it also happens that you have to enable peer to peer traffic in your local router.

Ps: you also have two slashes in your link

like image 36
Nils Avatar answered Jan 31 '23 03:01

Nils