Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask application cannot be exposed on droplet

I'm deploying a flask server to a Digital Ocean droplet.

from flask import Flask

app = Flask(__name__)

@app.route("/a/<string:b>")
def deploy(b):
    return "Response"

Using the following command:

FLASK_APP=server.py python -m flask run --host=0.0.0.0 --port=5555

When I deploy the application locally, I can receive response by doing

curl -XGET localhost:5555/a/random

When deploying on the droplet, it works internally, but when calling the droplet externally (despite having exposed port 5555 on TCP) it does not connect.

What could have changed? I'm also deploying a flask graphql server on the same droplet via docker which works perfectly fine.

like image 895
Peteris Avatar asked Jul 05 '18 20:07

Peteris


2 Answers

This is possible a common issue when uses VPS. People like me may often forget to setup firewall(s) if the codes are correct.

You mentioned that it works locally but not externally. I guess it should be.

Digital Ocean level:

  • Add inbound TCP port 5555 in droplet firewall setting

System level

  • iptables: e.g. iptables -A INPUT -p tcp -dport 5555 -j ACCEPT
  • firewalld: e.g. firewall-cmd --permanent --zone=public --add-port=5555/tcp

Or you may disable OS firewalls by systemctl stop [service-name] or service [service-name] stop. You may google the commands.

like image 64
Samuel Chen Avatar answered Sep 26 '22 06:09

Samuel Chen


Flask accepts connections from localhost by default.

In order to make your Flask app publicly available, you have to bind it to 0.0.0.0 address by adding --host=0.0.0.0 parameter.

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.

like image 30
Ramazan Polat Avatar answered Sep 24 '22 06:09

Ramazan Polat