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.
client_ip = request.META.get('HTTP_X_FORWARDED_FOR', request.META.get('REMOTE_ADDR', None))
if client_ip is None:
ip = netaddr.IPAddress(client_ip)
return (ip in netaddr.IPNetwork('22.214.171.124/23')) or (ip in netaddr.IPNetwork('192.168.0.0/16')) or (ip in netaddr.IPNetwork('126.96.36.199'))
The example searches through 2 IP ranges and then compares to an exact IP match.