Monday, 7 August 2023

get client IP address from django request

 def get_client_ip(request):

    ip=None
    x_forwarded = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded:
        ip=x_forwarded.split(',')[0]
    elif not ip:
        ip = request.META.get('REMOTE_ADDR')
    else:
        ip = request.META.get('HTTP_X_REAL_IP')
    return ip