Running Flask with Gunicorn On Raspberry Pie - python

I'm trying to run my flask app with gunicorn on my Raspberry pi. I've set up my router to port forward the localhost:5000. This works well when I run my flask app via python manage.py runserver. I can use my browser from any device and type http://**.**.***.***:5000/ and it will load my flask application. However when I try and run the app via gunicorn I receive an error connecting page. I run the the gunicorn exactly like the flask documentation says to. If I check gunicorn's logs I can see the html being rendered. Here's the kicker, when I run the app with gunicorn locally (gunicorn -w 2 -b localhost:5000 my_app:app), it works just fine. I have optimum online, my router setting are as followed...
protocol -> all
port -> 5000
forward port to -> same as incoming port
host -> raspberrypi
locate device by -> ipaddress
Like I said these settings work just fine from my pi when I use python's built in wsgi server. Gunicorn works just fine on when I run it locally and I can see my app when I type localhost:5000 in the browser, it's just when I set it up on my pi and try to access the page with the external IP, if I don't use gunicorn the external IP works just fine. I can't figure it out. Any ideas?

You need to have Gunicorn listen on 0.0.0.0 (all network interfaces). This then means it will be listening on an externally accessible IP address.
There is more information on the difference between localhost and 0.0.0.0 in this post on ServerFault.

Related

How can I fix a localhost refused to connect error? [duplicate]

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.

Running WSGI web server on a port (without using IIS)

Opening up a port for http request using WSGI (code is in Python+Django) on windows 10 - does not work for http requests coming from another machine
I have a python+django+mongodb site which works great when I run it through VS and now I would like to publish it.
My IIS set up with web.config with fastCGI gone bonkers and I am too frustrated to pursue it for the time being.
Now I decided to run a WSGI server on a port. So here is what I did. I started my app from command line by running my manage.py like this...
C:\Users\<myname>\AppData\Local\Continuum\anaconda3\python.exe manage.py runserver --noreload --settings=myapp.settings
And it starts the server and I can visit the site at http://localhost:8000 and everything works great - LOCALLY.
But with my machine IP x.y.z.w - I cannot access it from another machine
by visiting http://x.y.z.w:8000
Why can't my server listens to the port 8000 from another machine on the SAME network?
What do I have to do to allow this?
I am on Windows 10

Accessing the Django embedded server for development inside PyCharm by other hosts inside my local network?

I'm working with PyCharm and I wonder if there's a way to make the Django embedded server accesible for the other hosts in my local network or I need to deploy my app on a dedicated web server such as Apache?
Now, I'm accessing my Django app like this in the browser:
http://localhost:8000/mypage/
and I want other users inside my local network to type:
http://my_private_ip:8000/mypage/
in their browsers and see the same page.
Just run the server (which is Django's embedded server FWIW, not PyCharm's) under http://my_private_ip:8000:
# ./manage.py help runserver
Usage: manage.py runserver [options] [optional port number, or ipaddr:port]
Starts a lightweight Web server for development.
(...)
# ./manage.py runserver my_private_ip:8000
Assuming a Unix environment.
You need to ensure the server is listening not on the lo interface but on all interfaces (or at least the one used to connect to the LAN).
If you can customize the way PyCharm launches the server, use 0.0.0.0 as the host, as in:
python manage.py runserver 0.0.0.0:8000
Your coworkers can then use your LAN IP address. If you don't know it, use $ ip a.

How do I access my django app running on Amazon ec2?

So, I have looked around stack overflow + other sites, but havent been able to solve this problem: hence posting this question!
I have recently started learning django... and am now trying to run it on ec2.
I have an ec2 instance of this format: ec2-xx-xxx-xx-xxx.us-west-2.compute.amazonaws.com on which I have a django app running. I changed the security group of this instance to allow http port 80 connections.
I did try to run it the django app the following ways: python manage.py runserver 0.0.0.0:8000 and python manage.py runserver ec2-xx-xxx-xx-xxx.us-west-2.compute.amazonaws.com:8000 and that doesnt seem to be helping either!
To make sure that there is nothing faulty from django's side, I opened another terminal window and ssh'ed into the instance and did a curl GET request to localhost:8000/admin which went through successfully.
Where am I going wrong? Will appreciate any help!
You are running the app on port 8000, when that port isn't open on the instance (you only opened port 80).
So either close port 80 and open port 8000 from the security group, or run your app on port 80.
Running any application on a port that is less than 1024 requires root privileges; so if you try to do python manage.py runserver 0.0.0.0:80 as a normal user, you'll get an error.
Instead of doing sudo python manage.py runserver 0.0.0.0:80, you have a few options:
Run a pre-configured AMI image for django (like this one from bitnami).
Configure a front end server to listen on port 80, and then proxy requests to your django application. The common stack here is nginx + gunicorn + supervisor, and this blog post explains how to set that up (along with a virtual environment which is always a good habit to get into).
Make sure to include your IPv4 Public IP address in the ALLOWED_HOSTS section in Django project/app/settings.py script...

why I can't open any port when I deploy my django on my server, but my test program can run on my own machine

I'm learning django to make a test website, I can run the site on my own laptop, and use the browser to visit 127.0.0.1 , it's ok
but when I do the same thing on my server, I bought a vps and a domain, I just can't telnet the port , the browser also can't connect, I don't know why
I do the following
python manage.py runserver 8080
on my laptop, 8080 port can be connected by telnet, but on my server , it can't
Two things.
Firstly, as the documentation explains, by default runserver only binds to the localhost interface, which means it is only available on a browser running on the same machine. To get it to be visible outside the local machine, you need to bind to an externally-visible address, or 0.0.0.0 for all addresses:
python manage.py runserver 0.0.0.0:8080
Secondly, as the documentation also explains, you should not be trying to use the development server in a production setting anyway. Use a proper webserver, eg Apache + mod_wsgi.

Categories