issue about flask python app deployed in AWS ubuntu server - python

I am a new one to flask app. Now I want to deploy a simple app in the AWS ubuntu server following by this blog:https://chrisdtran.com/2017/deploy-flask-on-ec2/.
The code is as following:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == "__main__":
app.run()
and config file as following:
server {
listen 80;
location / {
proxy_pass http://127.0.0.1:8000/;
}
}
~
After I run: gunicorn helloworld:app
There are errors that I can't Can't connect to ('127.0.0.1', 8000) as shown in the figure

You have to give gunicorn a specific port:
gunicorn helloworld:app -b localhost:8000

Related

Deploying Flask Socket.io application with eventlet on heroku

I am using flask socket.io configured with eventlet on a free heroku instance. According to the flask socket.io documentation: https://flask-socketio.readthedocs.io/en/latest/ running socketio.run(app) will start a webserver if eventlet is installed.
I have a local react web app attempting to establish a WebSocket connection with this app engine instance. The web app uses socket.io and initially defaults to polling HTTP requests. These requests all timeout-- I'm unable to establish a web socket connection. I'm not sure what might be causing my issue, whether it is my app engine configuration or my flask socket.io setup.
Here is my initialization code for flask socket.io:
app = Flask(__name__)
socketio = SocketIO(app)
socketio.init_app(app, cors_allowed_origins="*")
..
..
if __name__ == '__main__':
socketio.run(app, debug=True)
Here is my Procfile:
web: python3 server.py
Here is the web app code attempting to set up the connection:
import io from 'socket.io-client'
const socketURL = "<app-engine-instance-url>:5000"
const socket = io.connect(socketURL)
You mentioned Heroku, then switched to App Engine. I assume you still meant Heroku?
The application running on Heroku must honor the $PORT environment variable and put the website on that port. Try this:
socketio.run(app, port=int(os.environ.get('PORT', '5000')))
Then from your client application connect to the Heroku URL for your app.

Deploying Flask Socket.io application with eventlet on Google AppEngine

I am using flask socket.io configured with eventlet on a flexible google appengine instance. According to the flask socket.io documentation: https://flask-socketio.readthedocs.io/en/latest/ running socketio.run(app) will start a webserver if eventlet is installed.
I have a local react web app attempting to establish a WebSocket connection with this app engine instance. The web app uses socket.io and initially defaults to polling HTTP requests. These requests all timeout-- I'm unable to establish a web socket connection. I'm not sure what might be causing my issue, whether it is my app engine configuration or my flask socket.io setup.
Here is my initialization code for flask socket.io:
app = Flask(__name__)
socketio = SocketIO(app)
socketio.init_app(app, cors_allowed_origins="*")
..
..
if __name__ == '__main__':
socketio.run(app, debug=True)
Here is my app.yaml:
runtime: python
env: flex
entrypoint: python3 server.py
runtime_config:
python_version: 3
manual_scaling:
instances: 1
network:
session_affinity: true
Here is the web app code attempting to set up the connection:
import io from 'socket.io-client'
const socketURL = "<app-engine-instance-url>:5000"
const socket = io.connect(socketURL)

How to access Virtualbox Python Flask service from host browser?

I have written a simple Python Flask application as follows:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello_world():
return 'Hello World2'
if __name__ == '__main__':
app.run(debug=True, port=5000)
This code is then executed in my Virtual box Ubuntu 18.04 Server VM. It starts listening to port 5000 in my VM.
However, when I try to access it from my host browser at 127.0.0.1:6000, it is not loading.
I have enabled port forwarding in Virtualbox NAT port forwarding option as shown below:
How to access the Flask server from host?
Most probably your application binds to loopback network interface.
Change it to bind to all interfaces so it is accessible from the outside:
app.run(host='0.0.0.0', debug=True, port=5000)

Error in running uwsgi with Flask on Ubuntu

I have trouble setting up my project on DigitalOcean. i'm new on Python.
Currently using flask to create a simple app.
My directory in the ubuntu server is as such:
~/kaboong/{{all files}}
the main py file is called main.py:
from flask import Flask, render_template, request, redirect,jsonify, url_for, flash
app = Flask(__name__)
if __name__ == '__main__':
app.debug = True
app.run(host = '0.0.0.0', port = 8000)
Inside my wsgi.py file:
from main import app
if __name__ == '__main__':
app.run()
I tried running the wsgi as such :
uwsgi --socket 0.0.0.0:8000 --protocol=http -w wsgi
I got an error saying:
* no app loaded. going in full dynamic mode *
Whats the problem here?
can you try this?
uwsgi --socket 0.0.0.0:8000 --protocol=http -w wsgi:app --callable app
if does not works, can you show more info such log console full?

How to access running python in vagrant from host machine browser?

I installed Vagrant in windows, now I have a virtual ubuntu , I run a python script :
vagrant#precise32:/vagrant/FlaskMysql/FlaskApp$ ls
app.py static storedPro.txt templates
vagrant#precise32:/vagrant/FlaskMysql/FlaskApp$ python app.py
* Running on http://127.0.0.1:5002/ (Press CTRL+C to quit)
my Vagrantefile :
config.vm.box = "hashicorp/precise32"
config.vm.provision :shell, path: "bootstrap.sh"
config.vm.network :forwarded_port, guest: 80, host: 4567
config.vm.network :forwarded_port, guest: 5002, host: 5002
I tried to access the above address from the browser in my window, the index.html page appears in a couple of seconds then disappears.
UPDATE :
vagrant#precise32:/vagrant/FlaskMysql/FlaskApp$ curl http://127.0.0.1:5000
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or
there is an error in the application.</p>
Thanks.
In addition to forwarding the port you'll need to run the Flask app with host "0.0.0.0":
app.run(host='0.0.0.0', port=5002)
This makes the dev server externally visible; in the case of Vagrant, we want the app to be externally visible (from the guest OS to the host OS).
Thanks to chucksmash's answer, this is my version of the solution:
I changed the host and port in the Python App from this:
run(host='localhost', port=8080, debug=True)
to this:
run(host='0.0.0.0', port=5002, debug=True)
and in the vagrantfile from this:
config.vm.network "forwarded_port", guest: 8080, host: 8080, host_ip: "127.0.0.1"
to this:
config.vm.network "forwarded_port", guest: 5002, host: 5002
then test a request and do NOT forget to add the resource (URL endpoint) 'hello' in my example: http://0.0.0.0:5002/hello
this fixed the issue for me.
I want to add more to Chucksmash's answer, if anyone is still facing issues with connecting to flask URL on host machine. Try one of these options (both should work):
Vagrantfile:
config.vm.network "forwarded_port", guest: 5000, host: 5000, host_ip: "127.0.0.1"
app.py:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return "Hello world!"
if __name__ == "__main__":
app.run()
Method 1. Configure host and port in app.run() and run the flash app using python.
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
Run flask app:
$ python3 app.py
Method 2. Pass host and port as arguments to flask run:
export FLASK_APP=app.py
flask run --host "0.0.0.0" --port 5000
You can verify if it works by running the following on terminal:
curl http://127.0.0.1:5000
output: Hello world!

Categories