<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def by_ip(self, ip):
 """ Find the smallest range containing the given IP. """

        try:
            number = inet_aton(ip)
        except Exception:
            raise IpRange.DoesNotExist

        try:
            return self.filter(start_ip__lte=number, end_ip__gte=number)\
                       .order_by('end_ip', '-start_ip')[0]
        except IndexError:
            raise IpRange.DoesNotExist