Django runserver bound to 0.0.0.0, how can I get which IP took the request?
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
Comments
-
Ben almost 2 years
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
and10.0.0.2
. When runserver is listening on0.0.0.0
, how can my application know whether the user hithttp://10.0.0.1/app/path/etc
orhttp://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 therequest.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 eitherhttp://10.0.0.1/foo
orhttp://10.0.0.2/foo
- butrequest.get_host()
will return10.10.10.10
, not the actual IPs the server is listening on.Thanks, Ben