Django runserver bound to 0.0.0.0, how can I get which IP took the request?

10,566
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

Share:
10,566
Ben
Author by

Ben

SOreadytohelp

Updated on June 26, 2022

Comments

  • Ben
    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 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