3

I'm trying to put the user IP in the page I send to the user. I've tried like explained here.

So my view looks like :

def yourip(request):
    return HttpResponse("Your IP is : %s" % request.META.get('HTTP_X_FORWARDED_FOR'))

but request.META.get('HTTP_X_FORWARDED_FOR') returns None. Is it possible to do this from the development server? Or is the problem elsewhere?

Thank you.

Community
  • 1
  • 1
vmonteco
  • 14,136
  • 15
  • 55
  • 86

2 Answers2

7

Try this:

def get_client_ip(request):
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        print "returning FORWARDED_FOR"
        ip = x_forwarded_for.split(',')[-1].strip()
    elif request.META.get('HTTP_X_REAL_IP'):
        print "returning REAL_IP"
        ip = request.META.get('HTTP_X_REAL_IP')
    else:
        print "returning REMOTE_ADDR"
        ip = request.META.get('REMOTE_ADDR')
    return ip
Yahya Yahyaoui
  • 2,833
  • 23
  • 30
0

In the development server, you can't get client IP so use Ngrok

Usage: ngrok http <your_development_server PORT>