404 in python flask application on EC2 - python

i'm kinda new to all this, so bare with me
I'm trying to deploy my flask app to EC2, and before getting to set a WSGI server, id like to try to connect to port 8080
the code can be found here: https://github.com/Klasyer/POBAP
I've set the flask app host to 0.0.0.0 and port to 8080
In EC2 i opened the inbound rule for 8080
When i try to open the app through the web (http://18.223.32.186:8080/) i get a 404.
What am i missing/doing wrong?
Thanks ahead for any help

Try attaching an elastic IP address to the instance that your running and once it is done you can access the flask app running in the instance. But you should include the port number always while accessing it.
If you need the app to be accessed without the use of port number then in your flask app change the port number to 443 which is for HTTPS and 80 for HTTP. If you need HTTPS the process is somewhat different because you need to make a purchase but for HTTP it's free. So go on with HTTP by using the port number as 80
app.run(host='0.0.0.0', port=80)

Related

Flask SocketIO How To Setup Production Server? [duplicate]

This question already has answers here:
Configure Flask dev server to be visible across the network
(17 answers)
Are a WSGI server and HTTP server required to serve a Flask app?
(3 answers)
Closed 4 years ago.
I currently have a project which uses Flask-SocketIO as the backend and a separate client that interacts with the Flask-SocketIO server. Everything is working when I run the server on my local machine. However, when I run the Flask-SocketIO server on a remote server via SSH and try connecting to the host IP address, the connection doesn't seem to work.
The flask-socketio server seems to run fine on the remote server machine as I can see things being printed into the console (running some machine learning models with tensorflow), but I just don't seem to be able to connect to it from the client.
So my main questions are:
What could be the reason why I'm unable to connect to the server remotely?
How exactly can I set up load balancing with nginx? I have looked at the documentation (https://flask-socketio.readthedocs.io/en/latest/#deployment) but I'm still unsure as to where to start.
Currently the project is configured to use the default server, using
if __name__ == '__main__':
socketio.run(app, port=5000)
I connect to it using
const socket = io('http://[ip address]:5000');
It works when I do
const socket = io('http://localhost:5000');
My server is hosted with DigitalOcean running Ubuntu
Any help would be greatly appreciated!
On production it is recommended to use a proper application server/reverse proxy combo such as Gunicorn or uWSGI as described in the documentation, but to answer your question, you should have passed the host argument to the .run() method as it defaults to 127.0.0.1:
https://flask-socketio.readthedocs.io/en/latest/#flask_socketio.SocketIO.run
host – The hostname or IP address for the server to listen on. Defaults to 127.0.0.1.
Which means that it will only listen on the local loopback interface and not the Internet interface. You should change it to the public IP address of the VPS, or to 0.0.0.0 to listen on all interfaces. For example:
socketio.run(app, host='0.0.0.0', port=5000)

How to run localhost on a port on a website with Flask

so I'm trying to host my flask app on a site only when it's accessed via a certain port for example. If a user goes to www.example.com on port 80 the site acts normal and they connect to the site. However if they visit www.example.com on port 5000 they are greeted with my flask app, how can I achieve this? Side note as people are getting confused, I don't own www.example.com I just want it to display what's running on localhost (flask) when it's accessed on a certain port.
You can run both the applications at the same time. Since Flask and the webserver run on different ports, they can co-exist without any issue.
However, you need to make sure clients have access to both port 80 (usually this is allowed from most firewalls and access restricting methods) and 5000 from client devices.

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.

Python flask expose to external visible

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.

Categories

Resources