I'm running a temporary Django app on a host that has lots of IP addresses. When using manage.py runserver 0.0.0.0:5000
, how can the code see which of the many IP addresses of the machine was the one actually hit by the request, if this is even possible?
Or to put it another way:
My host has IP addresses 10.0.0.1
and 10.0.0.2
. When runserver is listening on 0.0.0.0
, how can my application know whether the user hit http://10.0.0.1/app/path/etc
or http://10.0.0.2/app/path/etc
?
I understand that if I was doing it with Apache I could use the Apache environment variables like SERVER_ADDR
, but I'm not using Apache.
Any thoughts?
EDIT
More information:
I'm testing a load balancer using a small Django app. This app is listening on a number of different IPs and I need to know which IP address is hit for a request coming through the load balancer, so I can ensure it is balancing properly.
I cannot use request.get_host()
or the request.META
options, as they return what the user typed to hit the load balancer.
For example: the user hits http://10.10.10.10/foo
and that will forward the request to either http://10.0.0.1/foo
or http://10.0.0.2/foo
- but request.get_host()
will return 10.10.10.10
, not the actual IPs the server is listening on.
Thanks, Ben
The runserver command is a built-in subcommand of Django's manage.py file that will start up a development server for this specific Django project.
1) python manage.py runserver This command you'll probably run the most of all commands. It means to run a emulated server on your local computer. So, after running it, you can go to localhost:8000 or 127.0. 0.1:8000.
The site could be temporarily unavailable or too busy. Try again in a few moments. If you are unable to load any pages, check your computer's network connection. If your computer or network is protected by a firewall or proxy, make sure that Firefox is permitted to access the Web.
Inside the commands folder open up the runserver.py script with a text editor. Find the DEFAULT_PORT field. it is equal to 8000 by default. Change it to whatever you like DEFAULT_PORT = "8080"
request.get_host()
https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.get_host
but be aware this can be cheated so don't relay your security on it.
If users are seeing your machine under same address I am not sure if this is possible via runserver (it is supposed to be simple development tool).
Maybe you could use nginx? Or if this is only for testing do something like:
for i in 1 2 3 4 5; do manage.py runserver 10.0.0.$i:5000; done
and then sys.args[2] is your address
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With