# settings.pyclassCheckIPInSG(list):aws_access_key_id=AWS_ACCESS_KEY_IDaws_secret_access_key=AWS_SECRET_ACCESS_KEYregion_name='us-west-2'security_group_ids=['sg-12345678','sg-87654321']def__contains__(self,remote_ip,*args,**kw):result=super(CheckIPInSG,self).__contains__(remote_ip,*args,**kw)ifresult:returnTrue#INFO: above is just equivalent '"X.Y.Z.W" in list', if False, then run belowimportboto3,netaddrec2=boto3.resource('ec2',aws_access_key_id=self.aws_access_key_id,aws_secret_access_key=self.aws_secret_access_key,region_name=self.region_name)forsginec2.security_groups.filter(GroupIds=self.security_group_ids):forruleinsg.ip_permissions:foripinrule['IpRanges']:ifnetaddr.IPAddress(remote_ip)innetaddr.IPNetwork(ip['CidrIp']):returnTruereturnFalseINTERNAL_IPS=CheckIPInSG(['127.0.0.1','192.168.1.1','192.168.1.2','192.168.1.254'])