def get_client_ip(request):
access_route = get_access_route(request)
if len(access_route) == 1:
return access_route[0]
expression = """
(^(?!(?:[0-9]{1,3}\.){3}[0-9]{1,3}$).*$)| # will match non valid ipV4
(^127\.0\.0\.1)| # will match 127.0.0.1
(^10\.)| # will match 10.0.0.0 - 10.255.255.255 IP-s
(^172\.1[6-9]\.)| # will match 172.16.0.0 - 172.19.255.255 IP-s
(^172\.2[0-9]\.)| # will match 172.20.0.0 - 172.29.255.255 IP-s
(^172\.3[0-1]\.)| # will match 172.30.0.0 - 172.31.255.255 IP-s
(^192\.168\.) # will match 192.168.0.0 - 192.168.255.255 IP-s
"""
regex = re.compile(expression, re.X)
for ip in access_route:
if not ip:
# it's possible that the first value from X_FORWARDED_FOR
# will be null, so we need to pass that value
continue
if regex.search(ip):
continue
else:
return ip
评论列表
文章目录