Search code examples
pythonflasktcpubuntu-18.04digital-ocean

port 8000 not enable even after enable it on firewall Ubuntu 18


I'm stuck with a strange behavior with ubuntu 18 into digital ocean, I just created a droplet and didn't use DO firewall but try to enable port 8000 into server, like this:

sudo ufw allow 8000/tcp

When I check my port rules its ok, I mean its really what I need, there's even more (cause I also added udp)

sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere
80/tcp                     ALLOW       Anywhere
443/tcp                    ALLOW       Anywhere
80/udp                     ALLOW       Anywhere
443/udp                    ALLOW       Anywhere
8000/tcp                   ALLOW       Anywhere
8000/udp                   ALLOW       Anywhere
22/tcp (v6)                ALLOW       Anywhere (v6)
80/tcp (v6)                ALLOW       Anywhere (v6)
443/tcp (v6)               ALLOW       Anywhere (v6)
80/udp (v6)                ALLOW       Anywhere (v6)
443/udp (v6)               ALLOW       Anywhere (v6)
8000/tcp (v6)              ALLOW       Anywhere (v6)
8000/udp (v6)              ALLOW       Anywhere (v6)

When I run a flask service using port 8000 into server, something like this:

FLASK_APP=app.py flask run --host=127.0.0.1 --port=8000

I can get a response inside server, eg:

curl 127.0.0.1:8000

But when I try to do the same thing out site, I got refused connection, eg:

curl IPSERVER:8000

response:

curl: (7) Failed to connect to IPSERVER port 8000: Connection refused

I also check the port status using this service: https://www.yougetsignal.com/tools/open-ports/

And it says that port 8000 is closed, but I enabled it and there's no firewall on DO (I have one but I removed to test and it still not working)

Does anyone know what could be? Ubuntu 18 has a another place to enable it? Should I restart the server before made these changes?


Solution

  • You need to call your script with;

    --host=<your public ip> if you want to bind to just the address specified.

    --host=0.0.0.0 to bind to all available addresses.

    --host=127.0.0.1 will only bind to the loop-back address.

    The Quick Start docs states;

    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.