标签:def request pretty lse djang split 均衡 turn strip()
1 def get_client_ip(request): 2 x_forwarded_for = request.META.get(‘HTTP_X_FORWARDED_FOR‘) 3 if x_forwarded_for: 4 ip = x_forwarded_for.split(‘,‘)[-1].strip() 5 else: 6 ip = request.META.get(‘REMOTE_ADDR‘) 7 return ip
使用django来获取用户访问的IP地址 request.META[‘REMOTE_ADDR‘] 服务器会使用ngix等代理http,或者是该网站做了负载均衡,使用remote_addr抓取到的是1270.0.1,
使用HTTP_X_FORWARDED_FOR才获得真实IP。
标签:def request pretty lse djang split 均衡 turn strip()
原文地址:https://www.cnblogs.com/liujuejun/p/10508333.html