Django - get local listening IP address
In Django, how can I get Django's local IP 192.168.1.200
that it is presently listening on?
I need to use the IP in Django logging format.
settings.py
ALLOWED_HOSTS = ['192.168.1.200', '127.0.0.1']
Run server
python manage.py runserver 192.168.1.200:8000
You can use the socket library
import socket
def get_local_ip():
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80)) # Connect to a public IP
ip = s.getsockname()[0]
s.close()
return ip
and then
ALLOWED_HOSTS = [get_local_ip(), '127.0.0.1']
Go to your cmd and type ipconfig it will give you the local ip address of your pc.. You can add the ip address into the ALLOWED_HOSTS = []
and if you want to allow for all of the IP's then just do this ALLOWED_HOSTS = [*]