Django通过request获取客户端IP

def get_client_ip(request):
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        ip = x_forwarded_for.split(',')[0]
    else:
        ip = request.META.get('REMOTE_ADDR')
    return ip

 

posted @ 2024-11-09 00:46  lytcreate  阅读(2)  评论(0编辑  收藏  举报