While implementing a certain feature, we wanted to restrict it's functionality to a range of IP's only available in the office.
To do that, you first need to download netaddr from github.
01.
import
netaddr
02.
03.
def
cache_purge_allowed(request):
04.
client_ip
=
request.META.get(
'HTTP_X_FORWARDED_FOR'
, request.META.get(
'REMOTE_ADDR'
,
None
))
05.
06.
if
client_ip
is
None
:
07.
return
False
08.
09.
ip
=
netaddr.IPAddress(client_ip)
10.
return
(ip
in
netaddr.IPNetwork(
'203.34.46.0/23'
))
or
(ip
in
netaddr.IPNetwork(
'192.168.0.0/16'
))
or
(ip
in
netaddr.IPNetwork(
'119.225.10.1'
))
The example searches through 2 IP ranges and then compares to an exact IP match.