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

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!

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.

FastAPI/uvicorn not working when specifying host

I'm running a FastAPI app in Python using uvicorn on a Windows machine. It works fine when I do any one of the following options:
Run the following code on my mac, or
When I don't specify the port for uvicorn (remove the host parameter from the uvicorn.run call)
When I specify port '127.0.0.1', which is the host it uses when I don't specify a host at all.
from fastapi import FastAPI
import uvicorn
app = FastAPI()
#app.get("/")
async def root():
return {"message": "Hello World"}
if __name__ == '__main__':
uvicorn.run(app, port=8080, host='0.0.0.0')
When I go to 0.0.0.0:8080 on my browser, I get an error that says "This site can’t be reached".
I have checked my current active ports to make sure I'm not getting a collision using netstat -ao |find /i "listening" and 0.0.0.0:8080 is not in use.
My current file configuration looks like this:
working_directory
└── app
├── gunicorn_conf.py
└── main.py
My gunicorn_conf.py is super simple and just tries to set the host and port:
host = "0.0.0.0"
port = "8080"
How can I get this to work when I specify host '0.0.0.0'?
As I was writing the question above, I found the solution and thought I would share in case someone else runs into this. To get it to work put "http://localhost:8080" into the web browser instead of "http://0.0.0.0:8080" and it will work fine. This also works if you're hitting the endpoint via the python requests package, etc.
Run this in terminal uvicorn main:app --port 8086 --reload

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)

issue about flask python app deployed in AWS ubuntu server

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

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