Running Django’s Development Server on a Custom Port

Django’s development server, runserver, is a lightweight web server included with Django that is used for testing and local development. By default, runserver serves Django’s web application on port 8000, but you may want to use a different port for various reasons. This article will explain how to specify a custom port when running runserver.

Starting the Server on a Custom Port

To start runserver on a custom port, just pass the desired port as an argument. For example, to start the server on port 8080, use the following command:

python manage.py runserver 8080
Code language: Bash (bash)

Considerations for Custom Port Selection

There are a few things to consider when choosing a custom port for runserver.

  • Port numbers below 1024 are reserved for privileged services and are typically only bound to by system processes or by programs executed by privileged users. As a general rule, you should not use these ports unless you have a specific reason to do so.
  • Some ports are reserved for specific purposes, such as port 80 for HTTP and port 443 for HTTPS. If you want to test your Django application with these ports, you may need to run runserver as a privileged user or use a port forwarding tool such as iptables or socat.
  • It is important to ensure that the port you choose is not already in use by another service on your system. To check if a port is in use, you can use the netstat command:
netstat -tulpn | grep :8080
Code language: Bash (bash)

This will list all processes listening on port 8080. If the port is not in use, the command will return no output.

Additional Options

In addition to the --port option, runserver has several other options that may be useful in certain situations. For example:

  • --nothreading: Disables multi-threading.
  • --noreload: Disables auto-reloading of the server when code changes.
  • --insecure: Allows serving static files with runserver.

You can view the full list of options by running python manage.py runserver --help.

Conclusion

In summary, Django’s runserver can be run on a custom port by passing the port as an argument. It is important to consider the port number you choose and to ensure that the port is not already in use by another service on your system.

References


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *