How to access Virtualbox Python Flask service from host browser? - python

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)

Related

Trying to make a simple python web server and its not starting

Ive been trying to make a web server and I have the code down that should be able to get it running but when I go in to the Command Prompt and type python app.py it doesn't run when it should this is the code that I have
from flask import Flask
app = Flask(__name__)
#app.route("/")
def main():
return "Welcome to my Flask page"
if __name__ == "__main__":
app.run(debug = True, host = "0.0.0.0", port=80)```
The server won't run on port 80, it will run on the default port (5000). If you run the server and navigate to HTTP://0.0.0.0:5000/, you should see your / response. See Why can't I change the host and port that my Flask app runs on?.
To change the port Flask runs on, you can specify it in the command line:
flask run -h localhost -p 3000
Here, I run the server on localhost:3000. If you try to run the server on port 80, you will get a permission denied error since any port under 1024 needs root privileges (as m1ghtfr3e said in their answer).
Also, this is a great tutorial I recommend to anyone learning flask https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
I think the problem is port 80.
Which OS are you using?
Ports under 1024 need root privileges, there is also a possibility that it is not working because some other service (like Apache) is running on this port.
So either fixing privileges or services or changing the port should make it run.

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.

Flask always listens default address and port [duplicate]

This question already has answers here:
How can I change the host and port that the flask command uses?
(6 answers)
Closed 6 years ago.
I am trying to access my flask program on localhost from other devices like Android phone in my network.
I can access my localhost created using Apache but can not access program created by Flask.
This is my Flask program:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def home_page():
return '<h1>Welcome to my site!</h1>'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5050, debug=False)
When I run it, it always listens 127.0.0.1:5000.
* Serving Flask app "server"
* Forcing debug mode off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
I can see my PHP files in localhost but cannot see Flask program. (i.e. http://my_ip:5000 doesn't work)
Simply change '0.0.0.0' to your LAN IP address. You can know that typing ifconfig on Linux or ipconfig in windows in a cmd or a terminal
For Example
app.run(host="192.168.1.41")

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!

Vagrant, Flask — App not running on 10.10.10.10, 127.0.0.1

I'm running an app on my local box via Vagrant. The Python/Flask app launches and prints:
* Running on http://127.0.0.1:5000/
* Restarting with reloader
I found this https://github.com/makersquare/student-dev-box/wiki/Using-Vagrant-for-Development#testing-web-based-applications-in-vagrant
which suggests that Vagrant apps run on 10.10.10.10 (not 127.0.0.1), but when I navigate to that IP address (port 5000), I get the same result: "This webpage is not available".
Question: my app is running, but on what IP address? I can't seem to find it. Do I need to modify some configuration files?
Thanks in advance.
There are many ways how you could run flask web app on virtual machine (managed by vagrant). I think that following approach is quite flexible, because you don't have to deal with different ip address. Also it looks like you are developing on a host machine.
There are 2 things you need to configure. In VagranFile, you need configure port forwarding.
Vagrant.configure(2) do |config|
# use default box
config.vm.box = "ubuntu/trusty64"
# forward port guest machine:5000 -> host machine:5000
# port 5000 is default for flask web app
config.vm.network "forwarded_port", guest: 5000, host: 5000
end
Then, on virtual machine, you should start flask app on ip 0.0.0.0 which means that web app will serve for any IP address. More on this topic -> flask doc section Externally Visible Server
if __name__ == "__main__":
app.run("0.0.0.0", debug=True)
That's it. You should be able to connect to http://localhost:5000
In the file where you call app.run, it should be
app.run(host='0.0.0.0', port=...
In the host OS, navigate to the IP of the guest with the port that you're running the app from.
sjudǝʊ is right but it took me 4 hours to figure out he forgot to mention you also must run:
vagrant halt and then vagrant up
in order for your update to the vagrant file to actually take affect
In the latest version of Flask and python you do not need to configure vagrant file or no change required in the python script. Just run the flask with --host
flask run --host=192.168.10.80

Categories