[Django]-Django: Error: You don't have permission to access that port

10đź‘Ť

If you’re on Linux, you’ll receive this error.

First and foremost, Django does not have a production server, just a very basic development server and uses port 8080 by default.

when you execute the command

python manage.py runserver

you tell django to start its development server and it runs so you can test your web app before deployment to a production server.

Django Documentation -> django-admin -> Run Server

The way to access the server is to use your browser and plug in the URL in the address bar as so

localhost:8080

by default, most HTTP applications run on port 80 unless otherwise stated. For example, your MySQL server could run on port 3306 by default.

Basically, you can think of ports as old school telephone lines that connect you to whom ever your looking to communicate with.

There’s nothing really special about any of this. You should probably play with bottle to get the basics down first; just a friendly suggestion.

You can dig in to the details on the website. While not secure, you can use sudo to run on port 80, but for security reasons you should avoid it.

@mtt2p mentions a serverfault post that does a great job of the why

I’m sure there’s a way to tell the server to allow only local connections, but you should only use 0.0.0.0:80 when you want to show off your work to other people or see what your web app looks like on other devices.

In the long run, sudo is just easier and quicker, but lazy and insecure.

This is a link that explains it in the context of a virtualenv.

Django runserver error when specifying port

The answer states

I guess the sudo command will run the process in the superuser
context, and the superuser context lack virtualenv settings.

Make a shell script to set the virtualenv and call manage.py
runserver, then sudo this script instead.

You should note that the answer explaining a virtualenv based context is also insecure. It should just be run as

sudo python manage.py runserver 80

not

sudo bash script-name

outside of a virtualenv. Doing so defeats the purpose of sand-boxing your application. If you ignore this, you’ll be exposing yourself to a race condition.

👤user3159377

7đź‘Ť

Below helps to me to rectify same error:

in command line in windows:

net stop winnat

net start winnat

👤Shkum

5đź‘Ť

I am in Xubuntu 20.04 version and I use this command (because I have an python env) :

$ sudo ~/.virtualenvs/myproject/bin/python manage.py runserver 0.0.0.0:80

And to know where is your envs python folder, I did :

$ which python

1đź‘Ť

sudo python manage.py runserver 0.0.0.0:80

you need admin rights for port 80

👤mtt2p

1đź‘Ť

I set up a virtualenv called “aira” and installed virtualenvwrapper in the root environment (my virtualenvwrapper settings in /root/.bashrc are at the bottom). This reduces the number of sudo commands I need to cascade to -c together to get runserver working:

sudo sh -c "workon aira && python manage.py runserver --insecure 0.0.0.0:80"

If you’ve set up your django app’s virtualenv without virtualenvwrapper you’ll need to manually change to the correct directory and activate your virtualenv within the sudo command sequence. My virtualenv is called aira and I keep my virtualenvs in /root/.virtualenvs. My django project is in the ubuntu user’s home directory:

sudo sh -c "source $HOME/.virtualenvs/aira/bin/activate && cd /ubuntu/src/aira/ && python manage.py runserver --insecure 0.0.0.0:80"

If you’ve installed django and your requirements.txt in the system site packages then you can use sudo to runserver.

sudo python manage.py runserver --insecure 0.0.0.0:80"

The --insecure option allows staticfiles to serve your static assets (images, css, javascript).

For completeness, here’re my virtualenvwrapper configuration variables in /root/.bashrc on Ubuntu 16.04:

# python3 is used for virtualenv and virtualenvwrapper
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 

# *root* reuses the virtualenvs in *ubuntu*'s home directory
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=/home/ubuntu/src

source /usr/local/bin/virtualenvwrapper.sh
👤hobs

1đź‘Ť

sudo ./venv/bin/python manage.py runserver 80

0đź‘Ť

Run these Commands:

  • sudo su
  • pip3 install django
  • python3 manage.py runserver 0:80

0đź‘Ť

If you have the same case as me and your port was working before but suddenly stopped working the most likely case is that the port has been taken by other service or that did not close properly.

In windows you can do:

1. windows+r and type cmd.
2. run: netstat -a -n -o
3. here you can see which PID has taken the port
4. for closing you can go to task manager (ctrl+alt+delete)
5. go to processes tab
6. look for the matching PID and stop the process

This was he working solution for me to make it work again in the usual port apparently other app I had hijacked the port.

👤Javiagu13

Leave a comment