Python ipaddress.IPAddress() Examples

The following are 1 code examples of ipaddress.IPAddress(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module ipaddress , or try the search function .
Example #1
Source File: spf.py    From mailin with MIT License 5 votes vote down vote up
def set_ip(self, i):
        "Set connect ip, and ip6 or ip4 mode."
        self.iplist = False
        if i.lower() == 'list':
            self.iplist = []
            ip6 = False
        elif i.lower() == 'list6':
            self.iplist = []
            ip6 = True
        else:
            try:
                self.ipaddr = ipaddress.ip_address(i)
            except AttributeError:
                self.ipaddr = ipaddress.IPAddress(i)
            if self.ipaddr.version == 6:
                if self.ipaddr.ipv4_mapped:
                    self.ipaddr = ipaddress.IPv4Address(self.ipaddr.ipv4_mapped)
                    ip6 = False
                else:
                    ip6 = True
            else:
                ip6 = False
            self.c = str(self.ipaddr)
        # NOTE: self.A is not lowercase, so isn't a macro.  See query.expand()
        if ip6:
            self.A = 'AAAA'
            self.v = 'ip6'
            self.i = '.'.join(list(self.ipaddr.exploded.replace(':','').upper()))
            self.cidrmax = 128
        else:
            self.A = 'A'
            self.v = 'in-addr'
            self.i = self.ipaddr.exploded
            self.cidrmax = 32