How to solve “Failed to connect to port after 215 ms: Couldn’t connect to server” with a Flask Backend

Problem Statement

I have my Flask backend running on one server, and I use another computer, in the same network to send a post request to the Flask backend, I got an error like “Failed to connect to <ip> port <port> after 215 ms: Couldn’t connect to server” error. Nothing shows up in flask log.

Solution

  1. Check Network Connectivity: Ensure that both computers are connected to the same network and can communicate with each other. You can verify this by trying to ping one laptop from the other using their IP addresses. Make sure the ip and ports are correct.
  2. Verify Flask Server Configuration: Make sure that your Flask server is configured to listen on the correct IP address and port. By default, Flask listens on 127.0.0.1, which only allows connections from the same machine. You should change it to 0.0.0.0 to allow connections from other devices on the network. Example:
if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000)