guettli
guettli

Reputation: 27806

Django: Avoid clash of port 8000 (runserver)

I develop on serveral projects at once.

If I run the runserver twice, I get this error:

System check identified no issues (0 silenced).
September 10, 2021 - 10:44:26
Django version 3.1.4, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Error: That port is already in use.

I know that I can supply a different port manually, but a more automatic approach would be nice.

How could I solve this clash without manually giving each project a port by hand?

I have more than 50 systems on my laptop, and I don't want to give them each a port number by hand.

Upvotes: 1

Views: 4747

Answers (5)

Sabil
Sabil

Reputation: 4510

You can implement something like following.

  1. Add RUN_SERVER_PORT in settings.py
RUN_SERVER_PORT = 8080
  1. Implement following code in manage.py
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
from django.core.management.commands.runserver import Command as runserver
from django.conf import settings


def main():
    """Run administrative tasks."""
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'accountant.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    runserver.default_port = settings.RUN_SERVER_PORT
    execute_from_command_line(sys.argv)


if __name__ == '__main__':
    main()

Proof of Work:

python manage.py runserver

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
September 19, 2021 - 12:47:59
Django version 3.2.6, using settings 'accountant.settings'
Starting development server at http://127.0.0.1:8080/
Quit the server with CONTROL-C.

You can use any available port in RUN_SERVER_PORT

Upvotes: 4

rabbit.aaron
rabbit.aaron

Reputation: 2579

put this in your .bashrc .zshrc or .bash_profile depends on your environment.

#!/usr/bin/env bash

function runserver()
{
    local PORTNUM=8000
    for (( ; ; )); do
        nc -z 127.0.0.1 $PORTNUM
        [ $? -ne 0 ] && break
        PORTNUM=$(( $RANDOM % 1000 + 8000 ))
    done
    python manage.py runserver $PORTNUM $@
}

Then just run runserver and it should bring up a test server with a port in the range of [8000, 9000)

This function uses netcat to test if the port is available, you might need to install netcat.

Upvotes: 1

Ahmed Ablak
Ahmed Ablak

Reputation: 738

Reference to this post: https://stackoverflow.com/a/38319452/16936785

I would recommend to amend the server port under manage.py into the following for a random port selection:

import random
from django.core.management.commands.runserver import Command
port_number = random.randint(8000, 8888)
Command.default_port = str(port_number)

Upvotes: 1

guettli
guettli

Reputation: 27806

You can create your own manage command runsever_foo.

Then you can fetch the default value for the port from settings.py or from os.environ.

Upvotes: 3

Rvector
Rvector

Reputation: 2432

To run Django on different port

python manage.py runserver <your IP>:<port> : python manage.py runserver 0.0.0.0:5000

Or python manage.py runserver <port> : python manage.py runserver 5000

For automation Create a bash script like django_5000 and django_7000 where you execute differents command according to your desired port.

Upvotes: 0

Related Questions