Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 657
Ben Avatar asked Jul 11 '13 13:07

Ben


People also ask

What does Django Runserver do?

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.

What is the use of this command python manage py Runserver?

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.

Why does Python manage py Runserver not work?

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.

How do I run a Django project on a different port?

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"


1 Answers

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

like image 197
fsw Avatar answered Sep 29 '22 10:09

fsw