Access django server on VirtualBox/Vagrant machine from host browser? - python

I have a Django web server on a VirtualBox/Vagrant machine running "bento/centos-6.7-i386".
I have followed this guide to create a Django project: https://docs.djangoproject.com/en/dev/intro/tutorial01/
I have a web server running at http://127.0.0.1:8000/ inside my guest machine. This is the first time I am running a Django web server. It is supposed to be a hello world app.
How can I access this web application from my host browser?
I tried adding this line - config.vm.network "private_network", ip: "55.55.55.5" in the vagrant file and then tried to run the python manage.py runserver 0.0.0.0:80 command as per 1 of the solutions explained in previous discussions by others but I couldn't access the site from my host browser using 55.55.55.5:8000.
How can I access the web server from my browser?
Following given is my Vagrant File:
# -- mode: ruby --
# vi: set ft=ruby :
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "bento/centos-6.7-i386"
config.vm.network "forwarded_port", guest: 8000, host: 8000
config.vm.network "forwarded_port", guest: 8080, host: 8080
config.vm.network "forwarded_port", guest: 5000, host: 5000
config.vm.network "private_network", ip: "10.10.10.10"
end

You're forwarding port 8000 on the guest to 8000 on the host. Try:
python manage.py runserver 0:8000
Then in your browser visit: http://localhost:8000
That'll leave port 80 free if you end up wanting to run a web server for testing as well. Good luck!

if you are using below line in your Vagrantfile for port forwarding
config.vm.network "forwarded_port", guest: 8000, host: 8000
It means you will be able to access your guest application which running at port 8000 in your host browser at port 8000. so you will be able access by hitting http://127.0.0.1:8000 or http://localhost:8000 in your host.
why you are forcing your app to run on 0.0.0.0 ? .
it is not required , or if you want to access with guest IP address then there is no sense to use port forwarding.
if you changed this configuration in Vagrantfile after provision then don`t forget to reload
vagrant reload

I got a problem with Django and port forwarding insider VB.
Django doesn't allow external hosts to access it.
So change the start command to
python3 ./manage.py runserver 0.0.0.0:8000
and everything worked

Related

Docker container with Python Web App - Connect to Postgres on Host machine (OSX)

I can connect to my local Postgres DB in my web app, but NOT if I am running the web app inside of a Docker container.
Web app running inside of a Docker container
Postgres running in the Host machine
I am not sure if it is related to the Postgres connection settings or to the Docker network settings.
Follow my settings and commands:
Host:
OSX 10.11.6
PostgreSQL 9.6
Docker container
Docker 1.13.1
Docker-machine 0.9.0
Docker container OS: python:3.6.0-alpine
Python 3.6 + psycopg2==2.7
postgresql.conf:
listen_addresses = '*'
pg_hba.conf
host all all 127.0.0.1/32 trust
host all all 0.0.0.0/0 trust
host all all 172.17.0.0/24 trust
host all all 192.168.99.0/24 trust
With Docker network in HOST mode
docker run -i --net=host -h 127.0.0.1 -e POSTGRES_URI=postgresql://127.0.0.1:5432/db my/image
Error:
could not connect to server: Connection refused
Is the server running
on host "127.0.0.1" and accepting TCP/IP connections on port 5432?
With Docker network in BRIDGE mode
docker run -i --add-host=dockerhost:`docker-machine ip ${DOCKER_MACHINE}` -e POSTGRES_URI=postgresql://dockerhost:5432/db -p 8000:8000 -p 5432:5432 my/image
Error:
server closed the connection unexpectedly
This probably means the
server terminated abnormally before or while processing the request.
Any ideas?
There is a note about doing this in the docs
I want to connect from a container to a service on the host
The Mac has a changing IP address (or none if you have no network access). Our current recommendation is to attach an unused IP to the lo0 interface on the Mac; for example: sudo ifconfig lo0 alias 10.200.10.1/24, and make sure that your service is listening on this address or 0.0.0.0 (ie not 127.0.0.1). Then containers can connect to this address.

In zulip dev environment, how can i access zulip's first dev web browser using run-dev.py in vagrant env but i can't access from global ip

I have some problem now using zulip dev-env ...
I did success-installation by Dev-env(using run-dev.py in vagrant env) in my Mac PC and then i could access the first-dev page successfully through my mac-pc's browser
But i have been trying to show the first page using Dev-dev in my cloud hosting service (like AWS or google Cloud hosting) for several days
but i could not access and could not show the first page by using global IP and browser
Which part must I change the setting environment?
(that is, I want to access my dev-env[using run-dev.py] by global IP
after that, i want to test... )
Now I am using a cloud hosting. Of course I opened port 9991 and etc...
I use vagrant env and succeed it in cloud host.
In vagrant ssh, I could access zulip by using curl 127.0.0.1:9991 and received a success message:
29/Sep/2016 00:42:04]"GET / HTTP/1.0" 302 0" from running console
but i cannot access it by host console like this
ai2#mrtalk:~/project/zulip$ curl 127.0.0.1:9991
curl: (7) Failed to connect to 127.0.0.1 port 9991: Connection refused
of course , i can't access http://182.162.90.79:9991/ through my pc browser
for this I tried like these
In zproject/dev_setting.py (I changed...these lines)
EXTERNAL_HOST = '0.0.0.0:9991' #orgin localhost but change 0.0.0.0
ALLOWED_HOSTS = ['localhost','182.162.90.79','0.0.0.0'] #orgin localhost but add + '182.162.90.79','0.0.0.0'
In Vagrantfile I changed this line:
config.vm.network "forwarded_port", guest: 9991, host: host_port, host_ip: 127.0.0.1, auto_correct: true
What is wrong with here? Or which part should I change my dev-setting?

Django Cannot Be Accessed From Localhost

It's kinda weird I cannot access django from localhost but I able to access it from local IP.
python manage.py runserver 0.0.0.0:8000
then when I try to access from
My host file
127.0.0.1 lmlicenses.wip4.adobe.com
127.0.0.1 lm.licenses.adobe.com
127.0.0.1 gc.kis.scr.kaspersky-labs.com ff.kis.scr.kaspersky-labs.com ie.kis.scr.kaspersky-labs.com
0.0.0.0 gc.kis.scr.kaspersky-labs.com ff.kis.scr.kaspersky-labs.com ie.kis.scr.kaspersky-labs.com
I'm guessing something wrong with windows firewall or kaspersky,but I don't know what to do.
I've add exception to port 8000 and python.exe too
I cannot access localhost:8000 from my browser if I run python manage.py runserver
I don't know why that command cannot work in my laptop, but in my pc it just works.
====
ok, so my solution now is to use ipv6 and port 8000 which is
python manage.py runserver [::]:8000
Just try http://0.0.0.0:8000 instead of localhost:8000
Default localhost value is http://127.0.0.1
If you see the runerver result you have :
starting developement server at http://0.0.0.0:8000
Because you told that django server start at http://0.0.0.0:8000 when you run this command :
python manage.py runserver 0.0.0.0:8000
I'm also beginner to django, but I needed following things in order to run the server:
In settings.py, added ALLOWED_HOSTS as
ALLOWED_HOSTS = [
'elearning.com'
]
where elearning.com is my hostname. You can have multiple hosts separated with comma.
Used following command to run the server:
python manage.py runserver [::]:8000 or python manage.py runserver 0.0.0.0:8000 or python manage.py runserver 0:8000.
It will now listen to all interfaces. Read Django doc here.
Browsed to given hostname. In my case, it is http://elearning.com:8000/
I tried writing IP in ALLOWED_HOSTS but it didn't work, I could not open http://192.168.x.x:8000 in browser. Can anyone please correct me?
simply execute
python manage.py runserver
automatically you can access by localhost and for the extern access you could have access if not are it's locked the port 8000, for access to django admin use the next url:
http://127.0.0.1:8000/admin/
How your host file end up like this ?? . Can you test something.
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
My host file see everything is commented out. Can you do the same with your hosts file and give it a Try .. .
In the settings.py file in your project folder,
add '0.0.0.0' to the allowed host list.
Then save the file and run the command.Guess will work.

Running ubuntu vagrant server won't show anything on debian local browser

I've done this multiple times and never had an issue.
I'm trying to run a website on vagrant (trusty64) through shh on my Debian 8 Jessie x64.
I have used the same Vagrantfile n times before and it always worked perfectly but for I don't know what reason, on my new laptop, when I enter (on vagrant server - tried with sudo and without):
vagrant up
vagrant ssh
cd /opt/site/
python manage.py runserver 0.0.0.0:8000
nothing happens in console. It shows the usual:
System check identified 1 issue (0 silenced).
October 28, 2015 - 10:16:41
Django version 1.8.4, using settings 'settings.development'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
and when I type the ip:port in my local browser it just say:
This webpage is not available
ERR_CONNECTION_REFUSED
Any workaround for this ?
My vagrant config file:
# -*- mode: ruby -*-
# vi: set ft=ruby :
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.provider "virtualbox" do |v|
v.memory = 512
end
config.vm.box = "trusty64"
config.vm.box_url = "https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box"
config.vm.network "private_network", ip: "192.168.100.10"
config.vm.synced_folder ".", "/opt/site"
config.vm.hostname = "alexander"
end
As a mention: I tried multiple ports and still nothing. More, I have the exactly same configuration on another pc and it works flawless.
If changing the port forwarding doesn't work.You might also need to change in the settings of virtual box from NAT to Bridged Adapter.
The firewall will block incoming traffix on port 8000, so you will have to forward that port. Try adding this this to the configuration. you're using:
config.vm.network "forwarded_port", guest: 8000, host: 8080
and then browse to http://192.168.100.10:8080 it should work.
Also dont forget to privision your box again, or even restart just to be sure.
vagrant reload --provision
Why it works on your other machine is a mystery to me. Maybe there is no firewall, or the port is already forwarded?

Having problems accessing port 5000 in Vagrant

I am trying to teach myself Flask in a Vagrant environment. I understand that Flask runs a server on port 5000 by default. In my Vagrantfile I have:
config.vm.network :forwarded_port, guest: 80, host: 8080
config.vm.network :forwarded_port, guest: 5000, host: 5000
I have a simple tutorial Flask app:
from flask import Flask
app = Flask(__name__)
#app.route('/hello')
def hello_world():
return 'Hello world!'
if __name__ == '__main__':
app.run(debug=True)
Yet when I run python hello.py in my Vagrant environment and subsequently go to 127.0.0.1:5000/hello in Chrome on my desktop, I can't connect.
I don't know nearly enough about networking. What am I missing?
If you are accessing from Chrome in your desktop, you are technically accessing from a different computer (hence you need to place host='0.0.0.0' as argument to app.run() to tell the guest OS to accept connections from all public (external) IPs.
This is what worked for me (for both 127.0.0.1:5000/hello and localhost:5000/hello in Chrome):
from flask import Flask
app = Flask(__name__)
#app.route("/hello")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run(host='0.0.0.0')
You possibly need to get Flask to serve on an externally-visible URL: see the docs
This may be caused by Vagrant (VirtualBox) NAT Port forwarding not working properly (port conflicts).
To narrow down the issue, you may want to make sure that port 5000 is open correctly on both end, this can be done by using nmap, nc (netcat) or netstat etc.
e.g. on host
nmap 127.0.0.1
nc -vz 127.0.0.1 5000
curl http://127.0.0.1:5000
Within the guest
nmap GUEST_IP
nc -vz GUEST_IP 5000
curl http://GUEST_IP:5000
NOTE: GUEST_IP is most likely in the 10.0.2.0/24 network (vbox NAT engine default).
Running these commands on both your host and within the VM (guest box) will tell you if the ports are open.
Make sure your python hello world binds NOT ONLY to the loopback device so that it can serve requests from external clients.
Use lsof -i :5000 or netstat -nap | grep :5000 to determine which program is binding the port for further troubleshooting.
Do you have curl installed in your vagrant box? If not install it and try curl http://127.0.0.1:5000/hello. If you get a response and you see Hello world! in your console, then on flask side everything is fine. Back to the virtual box - When you open the network settings, from what you said above, I assume you are using a NAT address. In that case you will need to set the host address to 127.0.0.1, port 5000, leave the guest address empty and put port 5000 again and that should do the trick(port forwarding). One thing I've noticed about vagrant in those situations is that it works best if you use virtualhosts. Take a look here.
I was using Vagrant and solved it by adding this line of code to Vagrantfile:
config.vm.network "forwarded_port", guest: 5000, host: 5000, host_ip: "127.0.0.1"
make sure to 'vagrant reload' after you change your Vagrantfile

Categories

Resources