Broke it out a bit:

        # get the server's host from the HTTP headers
        host = request.META.get("HTTP_HOST", "127.0.0.1")
        # remove the port, if it's there
        host_name = host.split(":")[0]
        # get the IP address from a structure like ('localhost', [], ['127.0.0.1'])
        host_ip = socket.gethostbyaddr(host_name)[2][0]
        # if the requester's IP is either localhost or the server's public IP, then it's a loopback
        return request.META.get("REMOTE_ADDR") in ["127.0.0.1", host_ip]