django获取客户ip

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。
 

猜你喜欢

转载自www.cnblogs.com/liujuejun/p/10508333.html