96

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

blong
  • 2,815
  • 8
  • 44
  • 110
sreekanth
  • 1,267
  • 2
  • 13
  • 21
  • 2
    http://docs.djangoproject.com/en/dev/ref/django-admin/#runserver-port-or-address-port – Rafe Kettler Apr 24 '11 at 05:24
  • 4
    By default, it uses the localhost address (127.0.0.1), which does not accept connections from other machines on the network. 127.x.x.x is considered its own network, and a computer doesn't route between it and its other interfaces unless explicitly told to do so. Therefore, connecting to it from another network will not find the open port. What you want is `./manage.py runserver A:8000`. – Mike DeSimone Apr 24 '11 at 05:27

9 Answers9

174

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:

  1. On machine A in command line ./manage.py runserver 0.0.0.0:8000
  2. Than try in machine B in browser type http://A:8000
  3. Make a sip of beer.

Source from django docs

Antoine Brunel
  • 1,065
  • 2
  • 14
  • 30
Pol
  • 24,517
  • 28
  • 74
  • 95
  • Yes. This is exaclty what I've been doing. I run django in my ubuntu VM and then connect to it on my host windows machine to test on windows. – mpen Apr 24 '11 at 06:30
  • For me it works like clock. I have ubuntu as well. And my friend has windows 7. Try to ping you host first. And make sure that you adjusted you VM correctly! – Pol Apr 24 '11 at 06:45
  • 2
    I use this version of the command so that I can access Django dev server on my Mac from my Parallels WinXP VM. – Carl G Jul 10 '12 at 23:28
  • By this way, I could connect ios device. – Nidhin Dec 06 '17 at 08:13
  • It seems basic but your note about ip made me reialize that I should use http://localhost:8000 instead of using http://0.0.0.0:8000 when running django locally. – ReemRashwan Dec 22 '22 at 10:02
24

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.

Preston
  • 7,399
  • 8
  • 54
  • 84
Jason Webb
  • 7,938
  • 9
  • 40
  • 49
  • 1
    Thanx for the reply,but still I am getting an error like Error: That IP address can't be assigned-to. – sreekanth Apr 24 '11 at 05:36
  • 1
    You may have something binding that port on the external ip address. Maybe try another port. Also be sure that you don't have a firewall blocking the connection. – Jason Webb Apr 24 '11 at 05:38
6

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 .....

Community
  • 1
  • 1
Amrendra
  • 511
  • 8
  • 18
4

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.

nehem
  • 12,775
  • 6
  • 58
  • 84
benrules2
  • 417
  • 4
  • 14
3

in flask using flask.ext.script, you can do it like this:

python manage.py runserver -h 127.0.0.1 -p 8000

wuxueqian
  • 64
  • 4
2

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
Ernie
  • 120
  • 2
  • 9
1

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.

alvaro562003
  • 678
  • 1
  • 6
  • 27
1

First, change your directory:

cd your_project name

Then run:

python manage.py runserver
Red
  • 26,798
  • 7
  • 36
  • 58
Vaibhav
  • 11
  • 1
0

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

  1. edit your computers host file to define the domain and what IP it goes to
  2. 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

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']