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.
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:
System level
iptables -A INPUT -p tcp -dport 5555 -j ACCEPT
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With