Trying to run Flask on localhost, refused to connect - flask

I'm trying to run my Flask application on 127.0.0.1:4141 but all I've been getting is this which essentially is the localhost refused to connect, ERR_CONNECTION_REFUSED & vice versa.
link
I was running my application on PuTTY. If I run it on Flask on my local cmd prompt, it works for some reason.
I'm just wondering whether there's anything I can do to get it working.

Are you trying to reach the url from the same machine? if not try to change the ip from 127.0.0.1 to 0.0.0.0

Related

Can't access django development server on a remote vps

I've just got a VPS with Ubuntu 18.04 on it.
Now I want to move my Django app I've been developing on a local PC to the VPS. I moved it and it starts okay - it says the server is running on 0.0.0.0:8000.
So the server is running fine, but I can't connect to it from a web browser on my local PC. Note: I'm trying to access it with ip of the server (ip:8000)
I have port 8000 enabled with netstat and I have added the IP to ALLOWED_HOSTS.
Update: I managed to access it using ngrok. By running the command ngrok http 8000 I got the url with which I was able to access the server.
Now I'd like to know how can I access it with IP.

Opening port 5000 for the internet is not working for google cloud compute engine instance

I have a google cloud compute engine instance with Ubuntu 16.04 on it. I have a flask app running on port 5000.
I've set up firewall rules to allow ingress traffic for any host (using 0.0.0.0/0 filter) for tcp:5000. I ran the
sudo ufw allow 5000
command on the console.
At this point I was expecting to see the flask app by entering http://external_ip:5000 on my browser. But that is not the case. I get "external_ip refused to connect." error on the browser. What am I doing wrong?
Its working if I run the flask app on port 80 though.
As the allow-internal rule is active in the firewall rules. I thought maybe try accessing from a node under the same project (thus same default network). But no luck.
I had the same problem. The way to fix is, to add host parameter to Flask app as shown below. By default Flask App is designed to work on localhost only. This has fixed the problem for me
if __name__ == '__main__':
app.run(debug=False, port=8081, host='0.0.0.0')

SSH tunnelling to a remote server with django

I'm trying to set up an SSH tunnel to access my server (currently an ubuntu 16.04 VM on Azure) to set up safe access to my django applications running on it.
I was able to imitate the production environment with Apache WSGI and it works pretty good but since I'm trying to develop the application I don't want to make it available to broader public right now - but to make it visible only for a bunch of people.
To the point: when I set up the ssh tunnel using putty on Windows 10 (8000 to localhost:8000) and I run http://localhost:8000/ I get the folowing error:
"Not Found HTTP Error 404. The requested resource is not found.".
How can I make it work? I run the server using manage.py runserver 0:8000.
I found somewhere that the error may be due to the fact that the application does not have access to ssh files, but I don't know whether that's the point here (or how to change it).
Regards,
Dominik
After hours of trying I was able to solve the problem.
First of all, I made sure putty connects to the server and creates the desired tunnel. To do that I right-clicked on the putty window (title bar) and clicked event log. I checked the log and found the following error:
Local port 8000 forwarding to localhost:8000 failed: Network error:
Permission denied
I was able to solve it by choosing other local port (9000 instead of 8000 in my instance).
Second of all, I edited the sshd_config file: sudo vi etc/ssh/sshd_config
and added these three lines:
AllowAgentForwarding yes
AllowTcpForwarding yes
GatewayPorts yes
I saved the file and restarted the ssh service:
sudo service ssh stop
sudo service ssh start
Now when I visit localhost:9000 everything works just fine.

Flask host='0.0.0.0' Google Chrome could not connect

I just begun using Flask and tried to run the hello_world example (hello.py)
The point is that I run the server through ssh on a remote machine and I want to browse it from my local machine so I used
app.run(host='0.0.0.0'),
However when I browse on chrome I put let's say http: //RemoteMachineIP:5000/ I got:
Oops! Google Chrome could not connect to RemoteMachineIP:5000
The remote machine has multiple ethernet IP addresses so I tried them (as http: //RemoteMachineIP:5000/) and I am always getting the same error.
I have also tried using app.run(host='LocalMachineIP') in hello.py but I got this error Flask [Errno 99] Cannot assign requested address.
Am I missing something ?
If you can run the server but the browser won't see it, it is a firewall problem.
If you can't assign the ip port, probably there is a PID already using the port, so you should try
lsof -i :5000
Then
kill -9 [PID From the lsof]

Pycharm: testing from anther computer in local network

I am using Pycharm and testing on local dev server. All goes well until I try to connect to the local dev server from another laptop (windows).
My dev server is 127.0.0.1:8000 on a mac. On the second computer, I am able to ping the server's LAN ip 10.0.2.2 successfully. But enter 10.0.2.2:8000 in browser address bar doesn't connect to anything.
telnet 10.0.2.2 8000 in command line also fails.
How can I do this?
Listen to address 0.0.0.0 instead of 127.0.0.1
This means it will listen to all, you can listen to specific IP but this would work on both local and network.
For beginner like me, screenshot can help.
If leaving Host field to be empty, it will default to 127.0.0.1, which is not we want in such situation.