39

Inside the system, running on virtual machine, I can access the running server at 127.0.0.1:5000.

Although the 'remote' address of the vm is 192.168.56.101 (ping and ssh work fine), I cannot access the server with 192.168.50.101:5000 neither from the virtual machine nor from the local one.

I guess there's something preventing remote connections.

Here's /etc/network/interfaces:

auto eth1
iface eth1 inet static
address 192.168.56.101
netmask 255.255.255.0

ufw is inactive.

How do I fix this problem?

Jorge Castro
  • 70,934
  • 124
  • 466
  • 653
Ilya Smagin
  • 727
  • 2
  • 6
  • 11

3 Answers3

58

First of all - make sure that your HTTP server is listening on 192.168.50.101:5000 or everywhere (0.0.0.0:5000) by checking the output of:

netstat -tupln | grep ':5000'

If it isn't, consult Flask's documentation to bind to an address other than localhost.

If it is, allow the traffic using iptables:

iptables -I INPUT -p tcp --dport 5000 -j ACCEPT

From Flask's documentation:

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 debug disabled or trust the users on your network, you can make the server publicly available simply by changing the call of the run() method to look like this:

app.run(host='0.0.0.0')
Undo
  • 302
  • 1
  • 9
  • 25
Marcin Kaminski
  • 4,881
  • 21
  • 35
  • 1) the output is 127.0.0.1:5000 0.0.0.0:* LISTEN. This means the server doesnot listen to addresses other tha localhosts? 2) I tried ufw allow 5000, no result – Ilya Smagin Dec 01 '12 at 22:14
  • This is why I suggested looking at the Flask's docs to configure it to listen on all addresses. Have you looked at it? Allowing it through the firewall alone won't fix it. – Marcin Kaminski Dec 01 '12 at 22:25
  • Yes, thank you, I get it, already looking. Already used localtunnel(I know it's too much, but it works). Just trying to understand what netstat's output "127.0.0.1:5000 0.0.0.0:* LISTEN" means. – Ilya Smagin Dec 01 '12 at 22:30
  • You don't need localtunnel :) What the netstat output shows you is that your HTTP server is only accepting connections from your local machine. It looks like you need to change SERVER_NAME variable in Flask's configuration. – Marcin Kaminski Dec 01 '12 at 22:35
  • Updated my answer - this should help. – Marcin Kaminski Dec 01 '12 at 22:37
  • 2
    Created an account here just to favorite the question and upvote this answer! – Hephaestus Jun 15 '15 at 15:54
  • If you want to limit the IP addresses of machines that can connect, you can add something like `-m iprange --src-range 10.1.1.4-10.1.1.10` to the iptables command. This example limits connections to a few boxes on my local network, namely 10.1.1.4 to 10.1.1.10, inclusive. (Hopefully some clown who happens to get one of those IPs won't try port 5000 at the next net café I wander into. ) – Michael Scheper Nov 19 '15 at 06:46
  • 2
    For those of you who want to go right to the spot on the documentation rather than browse through a lot of it: http://flask.pocoo.org/docs/0.11/quickstart/#public-server – Dark Star1 Oct 21 '16 at 11:09
  • Thanks. the `iptables ...` worked for me. But I'm just curious that I was being able to run my flask app yesterday but today I got this error. I couldn't even locally `curl localhost:5000` (which said `connection refused`), let alone remote connection! I was willing to know why all of a sudden such a thing happens and is it possible that this happens in the future and how can I prevent it? – imans77 Feb 11 '19 at 14:25
8

The best way to do it

flask run --host=0.0.0.0
Muhammad Hassaan
  • 221
  • 5
  • 10
6

I've just had the same issue. To solve it, i updated the way to run the application :

 app.run(debug=True,host='0.0.0.0')

Using host=0.0.0.0 let me access my app through my local network.

To access your flask app all you need to do is in your browser type in:

[your devices ip address]:5000

ie:

192.168.1.255:5000

3kstc
  • 319
  • 1
  • 4
  • 16
Alex andre
  • 61
  • 1
  • 1