-1

I am doing a project on an android app with flask backend on Pycharm and came across an issue failing to connect to flask server. I found out that my server was failing to start properly for some reason even though it shows

* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Error displayed in browser

Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

Also noticed I couldn't change ports and it only ran on this address. Just to clarify a doubt I run an old project, which was working, and found that too failed to run showing same error. Lastly I just created a fresh project to run the default auto generated code for hello world, and that too showed error.

from flask import Flask

app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Hello World!'


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

What could be the problem? Does it have something to do with pycharm?

davidism
  • 121,510
  • 29
  • 395
  • 339

1 Answers1

0

It has to do with the way you actually run Flask. If you just use flask run and you don't have any specific setup in the definition of the flask app inside your code, it will run in development mode, and only be accessible from the machine running it.
Running on http://127.0.0.1:5000/ means that it's only running locally. Look into what the 127.0.0.1 IP address means. Note that localhost is a synonym for this. Wikipedia article here.

If you want the app to be used over local network (IIRC that's needed for an Android app to connect to it during dev) you'll need to change the way you run the Flask app. To copy/paste from the Flask quickstart docs:

Externally Visible Server
If you run the server you will notice that the server is only accessible from your own computer, not from any other in the network. This is the default because in debugging mode a user of the application can execute arbitrary Python code on your computer.

If you have the debugger disabled or trust the users on your network, you can make the server publicly available simply by adding --host=0.0.0.0 to the command line:

$ flask run --host=0.0.0.0
This tells your operating system to listen on all public IPs.

Edo Akse
  • 4,051
  • 2
  • 10
  • 21
  • Thanks for pointing out what I will have to notice when connecting the android app. But here the code I've shared is for the fresh basic project. It just displays hello world in localhost address. Even that isn't working. My previous project was a complete website and none of the pages are found and it's root address too shows this error. And it was completely working before, even had ML codes running in it. Any idea what's with that? – user9353487 Jun 19 '21 at 09:50
  • You'll need to provide a bit more info as there's not enough to work with. From your initial question, I understood that you were trying to access the flask app from the Android app. If you can clarify the exact issue you're encountering, you'll likely get a better answer. Remember, the better the question, the better the answer. That being said: What happens if you start the flask server locally and then visit `http://127.0.0.1:5000` on that same machine? – Edo Akse Jun 19 '21 at 09:58
  • One other thing to note, if you get the error message in the browser, if it actually reached the flask app, it should output more information at the window running the flask app. If it doesn't, it means that it never reached the app. The error message itself is the one that Flask throws by default though. Try enabling debugging flask to see if you get more info... – Edo Akse Jun 19 '21 at 10:31