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:
Code language: Bash (bash)python manage.py runserver 8080
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 asiptables
orsocat
. - 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:
Code language: Bash (bash)netstat -tulpn | grep :8080
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 withrunserver
.
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
- Django documentation: runserver
Leave a Reply