Access Django devserver from another machine same network - python

I'm using django's builtin server to develop a site and I want other computers in the same network I'm on to be able to access the server using the local IP address.
I have seen many posts about this and after trying all suggestions it's still not allowing other computers in my network to access the site.
I run the server using
python manage.py runserver 0.0.0.0:8000
and have already opened port 8000 as you can see in the following image.
I'm running Django 1.4.2, Python 2.7.3, Fedora 18 on kernel 3.8.11-200
Any help is highly appreciated. Thanks.

Use python manage.py runserver <ip>:<port>
For example,my IP is 192.168.0.100 and I want to run django app on port 80,I have to do
[sudo] python manage.py runserver 192.168.0.100:80
My port 80 needed root permissions,maybe because I have other applications accessing it.
You also have to add the IP address to ALLOWED_HOSTS list in settings.py
By doing this all clients in the 192.168.0 network will be able to access the site at 192.168.0.100

You're starting Django as needed - it will accept connections from anywhere as soon as the connections get to it.
Check your firewall and make sure it's allowing 8000 port connections. Something like this should work:
iptables -I INPUT -p tcp -m tcp --dport 8000 -j ACCEPT
Optionally you will need to extend the INTERNAL_IPS variable in the setting to allow remote debugging: https://docs.djangoproject.com/en/dev/ref/settings/#internal-ips .

skarap is correct. If your network is configured correctly and your django application with pytho9n manage.py runserver 0.0.0.0:8000 and you still can't access your django app from the VM host there is almost certainly a firewall issue. The illustration above is good if you are running iptables.
I deployed CentOS 7 on a virtualbox VM from a Windows 7 host. I didn't know that this distribution uses firewalld, not iptables to control access.
if
ps -ae | grep firewall
returns something like
602 ? 00:00:00 firewalld
your system is running firewalld, not iptables. They do not run together.
To correct you VM so you can access your django site from the host use the commands:
firewall-cmd --zone=public --add-port=8000/tcp --permanent
firewall-cmd --reload
Many thanks to pablo v on the http://www.scriptscoop.net site for pointing this out.

You have to add the server IP to the ALLOWED_HOSTS in the settings.py

set firewall rules with
$ sudo ufw enable
$ sudo ufw allow 8000
type ipconfig
copy addr under inet
do python3 manage.py runserver your_inet_addr:8000

Related

Django server inaccessible [duplicate]

I am running python manage.py runserver from a machine A
when I am trying to check in machine B. The url I typed is http://A:8000/ .
I am getting an error like The system returned: (111) Connection refused
You can run it for machines in your network by
./manage.py runserver 0.0.0.0:8000
And than you will be able to reach you server from any machine in your network.
Just type on other machine in browser http://192.168.0.1:8000 where 192.168.0.1 is IP of you server... and it ready to go....
or in you case:
On machine A in command line ./manage.py runserver 0.0.0.0:8000
Than try in machine B in browser type http://A:8000
Make a sip of beer.
Source from django docs
You need to tell manage.py the local ip address and the port to bind to. Something like python manage.py runserver 192.168.23.12:8000. Then use that same ip and port from the other machine. You can read more about it here in the documentation.
I was struggling with the same problem and found one solution. I guess it can help you. when you run python manage.py runserver, it will take 127.0.0.1 as default ip address and 8000. 127.0.0.0 is the same as localhost which can be accessed locally. to access it from cross origin you need to run it on your system ip or 0.0.0.0. 0.0.0.0 can be accessed from any origin in the network.
for port number, you need to set inbound and outbound policy of your system if you want to use your own port number not the default one.
To do this you need to run server with command python manage.py runserver 0.0.0.0:<your port> as mentioned above
or, set a default ip and port in your python environment. For this see my answer on
django change default runserver port
Enjoy coding .....
Just in case any Windows users are having trouble, I thought I'd add my own experience. When running python manage.py runserver 0.0.0.0:8000, I could view urls using localhost:8000, but not my ip address 192.168.1.3:8000.
I ended up disabling ipv6 on my wireless adapter, and running ipconfig /renew. After this everything worked as expected.
in flask using flask.ext.script, you can do it like this:
python manage.py runserver -h 127.0.0.1 -p 8000
For people who are using CentOS7, In order to allow access to port 8000, you need to modify firewall rules in a new SSH connection:
sudo firewall-cmd --zone=public --permanent --add-port=8000/tcp
sudo firewall-cmd --reload
I had the same problem and here was my way to solve it:
First, You must know your IP address.
On my Windows PC, in the cmd windows i run ipconfig and select my IP V4 address. In my case 192.168.0.13
Second as mention above: runserver 192.168.0.13:8000
It worked for me.
The error i did to get the message was the use of the gateway address not my PC address.
First, change your directory:
cd your_project name
Then run:
python manage.py runserver
Ok just came across this post this is a little off topic but hopefully explains a few things, The IP 127.0.0.1 points to your network card so any traffic that you cause to go to that IP address will not leave your computer.
For example modern network cards in laptops for example will not even give you that IP if you are not connected to a wifi or cabled network so you'll need to be connected at least to activate the card.
If you need to run multiple servers on the same machine but want to access them with a domain then you have a couple of options
edit your computers host file to define the domain and what IP it goes to
use a DNS Alias I set up using a cname record years ago *.local.irishado.com will point to 127.0.0.1
so for example these three domains will point to your local machine
http://site1.local.irishado.com
http://site2.local.irishado.com
http://site3.local.irishado.com
will all point to your local machine then in python projects you will need to edit the projects setting file ALLOWED_HOSTS property to hold the domain it will accept
ALLOWED_HOSTS = ['site1.local.irishado.com']

python3.7 django after runserver command 127.0.0.1:8000 is not available, no error message(Linux Gentoo) [duplicate]

I am running python manage.py runserver from a machine A
when I am trying to check in machine B. The url I typed is http://A:8000/ .
I am getting an error like The system returned: (111) Connection refused
You can run it for machines in your network by
./manage.py runserver 0.0.0.0:8000
And than you will be able to reach you server from any machine in your network.
Just type on other machine in browser http://192.168.0.1:8000 where 192.168.0.1 is IP of you server... and it ready to go....
or in you case:
On machine A in command line ./manage.py runserver 0.0.0.0:8000
Than try in machine B in browser type http://A:8000
Make a sip of beer.
Source from django docs
You need to tell manage.py the local ip address and the port to bind to. Something like python manage.py runserver 192.168.23.12:8000. Then use that same ip and port from the other machine. You can read more about it here in the documentation.
I was struggling with the same problem and found one solution. I guess it can help you. when you run python manage.py runserver, it will take 127.0.0.1 as default ip address and 8000. 127.0.0.0 is the same as localhost which can be accessed locally. to access it from cross origin you need to run it on your system ip or 0.0.0.0. 0.0.0.0 can be accessed from any origin in the network.
for port number, you need to set inbound and outbound policy of your system if you want to use your own port number not the default one.
To do this you need to run server with command python manage.py runserver 0.0.0.0:<your port> as mentioned above
or, set a default ip and port in your python environment. For this see my answer on
django change default runserver port
Enjoy coding .....
Just in case any Windows users are having trouble, I thought I'd add my own experience. When running python manage.py runserver 0.0.0.0:8000, I could view urls using localhost:8000, but not my ip address 192.168.1.3:8000.
I ended up disabling ipv6 on my wireless adapter, and running ipconfig /renew. After this everything worked as expected.
in flask using flask.ext.script, you can do it like this:
python manage.py runserver -h 127.0.0.1 -p 8000
For people who are using CentOS7, In order to allow access to port 8000, you need to modify firewall rules in a new SSH connection:
sudo firewall-cmd --zone=public --permanent --add-port=8000/tcp
sudo firewall-cmd --reload
I had the same problem and here was my way to solve it:
First, You must know your IP address.
On my Windows PC, in the cmd windows i run ipconfig and select my IP V4 address. In my case 192.168.0.13
Second as mention above: runserver 192.168.0.13:8000
It worked for me.
The error i did to get the message was the use of the gateway address not my PC address.
First, change your directory:
cd your_project name
Then run:
python manage.py runserver
Ok just came across this post this is a little off topic but hopefully explains a few things, The IP 127.0.0.1 points to your network card so any traffic that you cause to go to that IP address will not leave your computer.
For example modern network cards in laptops for example will not even give you that IP if you are not connected to a wifi or cabled network so you'll need to be connected at least to activate the card.
If you need to run multiple servers on the same machine but want to access them with a domain then you have a couple of options
edit your computers host file to define the domain and what IP it goes to
use a DNS Alias I set up using a cname record years ago *.local.irishado.com will point to 127.0.0.1
so for example these three domains will point to your local machine
http://site1.local.irishado.com
http://site2.local.irishado.com
http://site3.local.irishado.com
will all point to your local machine then in python projects you will need to edit the projects setting file ALLOWED_HOSTS property to hold the domain it will accept
ALLOWED_HOSTS = ['site1.local.irishado.com']

Django application not working on a new centos server [duplicate]

I followed the instructions here to run Django using the built-in webserver and was able to successfully run it using python manage.py runserver. If I access 127.0.0.1:port locally from the webserver, I get the Django page indicating it worked.
I realize the Django webserver is not a production server, but it's important for me for testing purposes to be able to access it from the outside world -- i.e. not from a web browser on the server, but from a different computer.
I tried:
http://mywebserver:port_django_runs_on
but it did not work. I also tried using the IP instead (based on ifconfig) to access:
http://myipaddress:port_django_runs_on
which did not work either.
The web server is running so it must be accessible from the outside, I'm just not sure how. I am running Linux with Apache, though I have not configured Django with Apache.
Any ideas on how to do this?
You have to run the development server such that it listens on the interface to your network.
E.g.
python manage.py runserver 0.0.0.0:8000
listens on every interface on port 8000.
It doesn't matter whether you access the webserver with the IP or the hostname. I guess you are still in your own LAN.
If you really want to access the server from outside, you also have to configure your router to forward port e.g. 8000 to your server.
Check your firewall on your server whether incoming connections to the port in use are allowed!
Assuming you can access your Apache server from the outside successfully, you can also try this:
Stop the Apache server, so that port 80 is free.
Start the development server with sudo python manage.py runserver 0.0.0.0:80
I had to add this line to settings.py in order to make it work (otherwise it showed an error when accessed from another computer)
ALLOWED_HOSTS = ['*']
then ran the server with:
python manage.py runserver 0.0.0.0:9595
Also ensure that the firewall allows connections to that port
Pick one or more from:
Your application isn't successfully listening on the intended IP:PORT
Because you haven't configured it successfully
Because the user doesn't have permission to
Your application is listening successfully on the intended IP:PORT, but clients can't reach it because
The server local iptables prevents it.
A firewall prevents it.
So, you can check that your application is listening successfully by running lsof -i as root on the machine and look for a python entry with the corresponding port you've specified.
Non-root users generally cannot bind to ports < 1024.
You'll need to look at iptables -nvL to see if there's a rule that would prevent access to the ip:port that you are trying to bind your application to.
If there is an upstream firewall and you don't know much about it, you'll need to talk to your network administrators.
just do this:
python manage.py runserver 0:8000
by the above command you are actually binding it to the external IP address.
so now when you access your IP address with the port number, you will be able to access it in the browser without any problem.
just type in the following in the browser address bar:
<your ip address>:8000
eg:
192.168.1.130:8000
you may have to edit the settings.py
add the following in the settings.py in the last line:
ALLOWED_HOSTS = ['*']
hope this will help...
For AWS users.
I had to use the following steps to get there.
1) Ensure that pip and django are installed at the sudo level
sudo apt-get install python-pip
sudo pip install django
2) Ensure that security group in-bound rules includ http on port 80 for 0.0.0.0/0
configured through AWS console
3) Add Public IP and DNS to ALLOWED_HOSTS
ALLOWED_HOSTS is a list object that you can find in settings.py
ALLOWED_HOSTS = ["75.254.65.19","ec2-54-528-27-21.compute-1.amazonaws.com"]
4) Launch development server with sudo on port 80
sudo python manage.py runserver 0:80
Site now available at either of the following (no need for :80 as that is default for http):
[Public DNS] i.e. ec2-54-528-27-21.compute-1.amazonaws.com
[Public IP] i.e 75.254.65.19
I'm going to add this here:
sudo python manage.py runserver 80
Go to your phone or computer and enter your computers internal IP (e.g 192.168.0.12) into the browser.
At this point you should be connected to the Django server.
This should also work without sudo:
python manage.py runserver 0.0.0.0:8000
UPDATED 2020
TRY THIS WAY
python manage.py runserver yourIp:8000
ALLOWED_HOSTS = ["*"]
You need just to allow any hosts :
settings.py :
ALLOWED_HOST = ['*']
Run your server :
python3 manage.py runserver 0.0.0.0:8000
If you want to connect android app just add internet permission in AndroidManifest
It's work for me ;)
open terminal
type : ifconfig
check results of ifconfig command
use the inet IP .. should look like this.. 192.168.1.121 or similar 192.168.x.x.
now runserver like you normally do but this time specify the inet IP
python3 manage.py runserver 192.168.x.x:8000 (replace the x with your inet)
also
on settings.py
ALLOWED_HOSTS = ['*']
install ngrok in terminal
sudo apt-get install -y ngrok-client
after that run:
ngrok http 8000
or
ngrok http example.com:9000
If you are using Docker you need to make sure ports are exposed as well

Running django on 0.0.0.0:8000

I am using vagrant along with Virtualbox, so I can't runserver on the default port and address or it won't work.
Django says:
(...) to listen on all public IPs (useful if you want to show off your work on
other computers), use:
python manage.py runserver 0.0.0.0:8000
Then I tried to access the server via http://127.0.0.1:8888/ but it says Unable to connect. Any guess how to run the server correctly?
I have followed the gettingstartedwithdjango vidoes and this http://127.0.0.1:8888/ worked for the author.
For people who are just looking where to configure the IP, it is run like so (as seen in the OP) -
python manage.py runserver 0.0.0.0:8000
If you are accessing it over your local network you will also need to update settings.py with the IP of your host, as well as specifying localhost. Your local IP can be gotten on Windows by running ipconfig in the command prompt, or ip addr show in the terminal on Ubuntu.
ALLOWED_HOSTS = ['192.168.1.XXX', 'localhost']
You have to run with root privileges. If you run with sudoyou will succeed.
UPDATE 1
The previous information is irrelevant with the topic.
In vagrant you have to forward your port to a local port. Please have a look at this.
UPDATE 2
This page explained how to install and configure django in a vagrant box.

How to access the local Django webserver from outside world

I followed the instructions here to run Django using the built-in webserver and was able to successfully run it using python manage.py runserver. If I access 127.0.0.1:port locally from the webserver, I get the Django page indicating it worked.
I realize the Django webserver is not a production server, but it's important for me for testing purposes to be able to access it from the outside world -- i.e. not from a web browser on the server, but from a different computer.
I tried:
http://mywebserver:port_django_runs_on
but it did not work. I also tried using the IP instead (based on ifconfig) to access:
http://myipaddress:port_django_runs_on
which did not work either.
The web server is running so it must be accessible from the outside, I'm just not sure how. I am running Linux with Apache, though I have not configured Django with Apache.
Any ideas on how to do this?
You have to run the development server such that it listens on the interface to your network.
E.g.
python manage.py runserver 0.0.0.0:8000
listens on every interface on port 8000.
It doesn't matter whether you access the webserver with the IP or the hostname. I guess you are still in your own LAN.
If you really want to access the server from outside, you also have to configure your router to forward port e.g. 8000 to your server.
Check your firewall on your server whether incoming connections to the port in use are allowed!
Assuming you can access your Apache server from the outside successfully, you can also try this:
Stop the Apache server, so that port 80 is free.
Start the development server with sudo python manage.py runserver 0.0.0.0:80
I had to add this line to settings.py in order to make it work (otherwise it showed an error when accessed from another computer)
ALLOWED_HOSTS = ['*']
then ran the server with:
python manage.py runserver 0.0.0.0:9595
Also ensure that the firewall allows connections to that port
Pick one or more from:
Your application isn't successfully listening on the intended IP:PORT
Because you haven't configured it successfully
Because the user doesn't have permission to
Your application is listening successfully on the intended IP:PORT, but clients can't reach it because
The server local iptables prevents it.
A firewall prevents it.
So, you can check that your application is listening successfully by running lsof -i as root on the machine and look for a python entry with the corresponding port you've specified.
Non-root users generally cannot bind to ports < 1024.
You'll need to look at iptables -nvL to see if there's a rule that would prevent access to the ip:port that you are trying to bind your application to.
If there is an upstream firewall and you don't know much about it, you'll need to talk to your network administrators.
just do this:
python manage.py runserver 0:8000
by the above command you are actually binding it to the external IP address.
so now when you access your IP address with the port number, you will be able to access it in the browser without any problem.
just type in the following in the browser address bar:
<your ip address>:8000
eg:
192.168.1.130:8000
you may have to edit the settings.py
add the following in the settings.py in the last line:
ALLOWED_HOSTS = ['*']
hope this will help...
For AWS users.
I had to use the following steps to get there.
1) Ensure that pip and django are installed at the sudo level
sudo apt-get install python-pip
sudo pip install django
2) Ensure that security group in-bound rules includ http on port 80 for 0.0.0.0/0
configured through AWS console
3) Add Public IP and DNS to ALLOWED_HOSTS
ALLOWED_HOSTS is a list object that you can find in settings.py
ALLOWED_HOSTS = ["75.254.65.19","ec2-54-528-27-21.compute-1.amazonaws.com"]
4) Launch development server with sudo on port 80
sudo python manage.py runserver 0:80
Site now available at either of the following (no need for :80 as that is default for http):
[Public DNS] i.e. ec2-54-528-27-21.compute-1.amazonaws.com
[Public IP] i.e 75.254.65.19
I'm going to add this here:
sudo python manage.py runserver 80
Go to your phone or computer and enter your computers internal IP (e.g 192.168.0.12) into the browser.
At this point you should be connected to the Django server.
This should also work without sudo:
python manage.py runserver 0.0.0.0:8000
UPDATED 2020
TRY THIS WAY
python manage.py runserver yourIp:8000
ALLOWED_HOSTS = ["*"]
You need just to allow any hosts :
settings.py :
ALLOWED_HOST = ['*']
Run your server :
python3 manage.py runserver 0.0.0.0:8000
If you want to connect android app just add internet permission in AndroidManifest
It's work for me ;)
open terminal
type : ifconfig
check results of ifconfig command
use the inet IP .. should look like this.. 192.168.1.121 or similar 192.168.x.x.
now runserver like you normally do but this time specify the inet IP
python3 manage.py runserver 192.168.x.x:8000 (replace the x with your inet)
also
on settings.py
ALLOWED_HOSTS = ['*']
install ngrok in terminal
sudo apt-get install -y ngrok-client
after that run:
ngrok http 8000
or
ngrok http example.com:9000
If you are using Docker you need to make sure ports are exposed as well

Categories

Resources