Python flask expose to external visible - python

With the help of the post Flask - configure dev server to be visible across the network, I have tried the same to make my Flask externally visible so that I can send HTTP requests from my local browser to the Flask in remote server.
Can someone please help on why its not working for me even I have opened the connections.
I started my flask in Putty [script in dev server] and tried accessing the URL from my Chrome as http://[my_sys_ip]:5000/. Chrome reports me OOPS error.
On Flask, I have made it externally visble with debug mode turned off:
if __name__ == '__main__':
app.run(host='0.0.0.0', debug = False)
From netstat, I can see its listening on 5000:
netstat -an | grep :5000
tcp 0 0 0.0.0.0:5000 0.0.0.0:* LISTEN
When tried to send a GET request from the same dev server, I'm successful with the expected response:
python testing.py
URL called is http://0.0.0.0:5000/
Message to the user is Hello World!!!!!!!
What am I missing ?

I know this is an old question, but I figured I'll throw my 2 cents in.
From your description, it sounds like you are launching your flask application on a remote server (dev server) through PuTTY. You are then trying to access the app on your local system (localhost). The application isn't running on your local system, so that would explain the error in chrome.
Instead of going to http://[my_sys_ip]:5000, you will need to go to http://[dev_svr_ip]:5000.

Related

How to access Flask API URL running on Proxy server

Let's consider,
Flask app is deployed in the Server (Using proxy)
Already using 'host=0.0.0.0' & 'port=80' (or 8080 etc)
Already added '127.0.0.1 localhost' in '/etc/hosts' file.
Steps:
I have used the scp command to upload the Flask app files. And have connected to that server using 'ssh' from terminal and ran the Flask app using 'python app.py'
Flask app is running without any issue and I can see the message running on 'http://xx.xx.xx.xx:80'
Problem:
When I open the same URL in the Chrome Browser from my same local machine, it's showing "Not reachable error".
Expected:
Anybody can able to access that Flask app url (e.g http://xx.xx.xx.xx:80/home) from their local chrome browser if they are already connected to VPN on their machine.
Note: By normally I will connect to VPN in order to access that server (at the time of using SCP & SSH)
Please consider the following sample address(s):
Server - 10.xx.xx.xx
Proxy - 34.xx.xx.xx
VPN - my.abc.com (Ip Address: 9.89.xx.xx)
Browser- 173.xx.xx.xx (Chrome Browser)
Any information would be helpful, thanks!

Flask server works on internal network but doesn't connect with VPN

SSH works everywhere
I recently obtained a static IP at work and attached it to an internal domain. I am able to SSH into the machine with ssh MY_DOMAIN regardless of whether I'm
physically at work and on the same network, or
connected with full-tunnel VPN.
Flask works at work
If I'm physically at work and start a Flask server (code below) after logging into the desktop the domain is routed to, I can open up MY_DOMAIN in a browser on any computer at work (no VPN) and see Hello There!.
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "<h1 style='color:blue'>Hello There!</h1>"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80) # 127.0.0.1 didn't work either
# changing host to MY_DOMAIN or IP and port to 5000, etc. also didn't work
If I'm away from work but on VPN and SSH into the machine to start the Flask server, my connection times out when navigating to MY_DOMAIN in a browser.
Ping also works at work
On VPN, when I ping MY_DOMAIN, I see 0 packets received (all packets received when I'm at work):
9 packets transmitted, 0 packets received, 100.0% packet loss
If I turn off VPN away from work and ping MY_DOMAIN, I see:
ping: cannot resolve MY_DOMAIN: Unknown host
How am I able to SSH without any issues when I'm at work or away and on VPN, but can't interact with my domain in any other way on VPN?
I suspect this could be related to ports but I have limited networking knowledge.
And yep, I've done my research. This was the closest question on here connecting to flask app over VPN but it doesn't help (at least I don't think I want to deploy anything). I don't know if this is possible, but I think I want to configure: "forward incoming requests on port 80 of MY_DOMAIN to Flask app on localhost, port 80". (Edit: don't know any more after the Apache Web server works.)
Edit: I can connect to an Apache server on VPN but not Flask
I can navigate to MY_DOMAIN in a browser connected VPN and see "It Works" when I SSH and run:
sudo apachectl start
sudo defaults write /System/Library/LaunchDaemons/org.apache.httpd Disabled -bool false
I wonder why this doesn't work with Flask.
(reference: https://www.quora.com/How-can-I-make-my-iMac-the-hosting-for-my-website)

Django websocket works on local but not in Amazon EC2 server, error during Websocket handshake

I have a project in websockets with angular and django, it works fine locally with Linux, when I have deployed it to Amazon EC2 that is also Linux and did the same with Linux locally, I have attached a screenshot of the server in django in Amazon EC2 that it looks that is running ok, server screenshot, the client that is an angular app, when I'm trying to connect to the server this.globals.socket = new WebSocket('ws://mydomain.com.com/stocks'), I'm getting this error WebSocket connection to 'ws://mydomain.com.com/stocks' failed: Error during WebSocket handshake: Unexpected response code: 404. Do I have to configure something in nginx.conf in Amazon EC2 server?, or do I have to enable something additional in the server?. Any help or clue would really be appreciated. Thank you very much.
You are running your server on localhost (127.0.0.1:8000). It's not accessible from outside. You have to run it on a public IP or configure a proxy to route requests to your localhost server. Also do not forget to open the port to the world via EC2 management interface.
Also note that you shouldn't run your server via ./manage.py runserver on production. You should use application WSGI server (e.g. Gunicorn) and webserver (e.g. Nginx). Check out this good tutorial. Basically you run Gunicorn which is bound to unix socket, and Nginx which proxies (proxy_pass) all requests to the unix socket.

Deploy simple server using Flask on AWS

I'm using this Python code to see if I can view the simple AWS server however, I'm still not able to load the webpage properly.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return "It works!"
if __name__ == '__main__':
app.run(port=80, host='0.0.0.0')
In AWS, inbound:
HTTP TCP port 80 with a source of: 0.0.0.0/0
SSH TCP port 22 with a source of: 0.0.0.0/0
Outbound - everything is allowed.
I'm seeing no errors on terminal when I start the program. The console says Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) so I'm trying to load http://ubuntu#ec2-IPADDRESS.compute-1.amazonaws.com:5000
Flask defaults to binding to 127.0.0.1:5000.
If you're trying to access http://ubuntu#ec2-IPADDRESS.compute-1.amazonaws.com:5000 , you'll need to have port 5000 opened in the inbound firewall rules and bind to 0.0.0.0 instead of 127.0.0.1.
You can not use port 80 for your web application. Let it run on default port then host your web application by mod_wsgi or uWSGI... Check the deployment options here: http://flask.pocoo.org/docs/0.12/deploying/ .
Imo: better if you deploy your application on AWS Elastic Beanstalk. Give me a shout if you need a sample project.

Can't connect to Flask web service, connection refused

I'm trying to run a simple web server on a Raspberry Pi with Flask. When I run my Flask app, it says:
running on http://127.0.0.1:5000/
But when I enter this address on my laptop's in Chrome, I get
ERR_CONNECTION_REFUSED
I can open 127.0.0.1:5000 on the Raspberry Pi's browser. What do I need to do to connect from another computer?
Run your app like this:
if __name__ == '__main__':
app.run(host='0.0.0.0')
It will make the server externally visible. If the IP address of the machine is 192.168.X.X then, from the same network you can access it in 5000 port. Like, http://192.168.X.X:5000
when you are running the server via flask run change it to flask run --host=0.0.0.0
to connect, find the IPV4 address of the server that your script is running on. On the same network, go to http://[IPV4 address]:5000
A reason could also be in firewall refusing incoming connections on port 5000. Try:
sudo ufw allow 5000
app.run(host='0.0.0.0',port=5000)
if you run your app in this way then your server will be visible externally.
Steps by Setp:
Run your app by using the following command
app.run(host='0.0.0.0',port=5000)
Go to the window cmd . Type ipconfig and get the get the IPV4 adress suppose your IPV4 address is 192.168.X.X
Go to the mobile browser and type the 192.168.X.X:5000
If you have debug = True inside your app.run(), then it will not be visible to other machines either. Specify host and port inside app.run() without the debug = True.
You will have to run the development server such that it listens to requests on all interfaces and not just the local one
Ask Flask to listen on 0.0.0.0:PORT_NUMBER
or any other port you may choose
On MacOS 12.4 (Monterey) I couldn't load localhost nor my local IP but it worked with both of these:
0.0.0.0
127.0.0.1
Just change the URL in the browser if it loads with "localhost".
Both devices must be connected to same network.
Use app.run(host='0.0.0.0',port=5000) and run with your own Ipv4
address like his http://[Your Ipv4 address]:5000
If you are connecting this with android app then don't forget to
put INTERNET permission in manifest file.
Contrary to popular believe 127.0.0.1 is not the same a localhost.
I solved the issue above by setting 127.0.0.1 explicitly on both ends.
Well i was also here to know answer but i guess i found out problem. The reason is that you may not activated or run flask that's why it is showing error. For that you have to start from terminal "flask run" and then surely your flask will run...
The issue may occur, if VPN is on, so try to switch it off.

Categories

Resources