Create an instance of an IP object. Data can be a network specification or a single IP. IP addresses can be specified in all forms understood by parseAddress(). The size of a network can be specified as /prefixlen a.b.c.0/24 2001:658:22a:cafe::/
(self, data, ipversion=0, make_net=0)
| 79 | IPint.""" |
| 80 | |
| 81 | def __init__(self, data, ipversion=0, make_net=0): |
| 82 | """Create an instance of an IP object. |
| 83 | |
| 84 | Data can be a network specification or a single IP. IP |
| 85 | addresses can be specified in all forms understood by |
| 86 | parseAddress(). The size of a network can be specified as |
| 87 | |
| 88 | /prefixlen a.b.c.0/24 2001:658:22a:cafe::/64 |
| 89 | -lastIP a.b.c.0-a.b.c.255 2001:658:22a:cafe::-2001:658:22a:cafe:ffff:ffff:ffff:ffff |
| 90 | /decimal netmask a.b.c.d/255.255.255.0 not supported for IPv6 |
| 91 | |
| 92 | If no size specification is given a size of 1 address (/32 for |
| 93 | IPv4 and /128 for IPv6) is assumed. |
| 94 | |
| 95 | If make_net is True, an IP address will be transformed into the network |
| 96 | address by applying the specified netmask. |
| 97 | |
| 98 | >>> print IP('127.0.0.0/8') |
| 99 | 127.0.0.0/8 |
| 100 | >>> print IP('127.0.0.0/255.0.0.0') |
| 101 | 127.0.0.0/8 |
| 102 | >>> print IP('127.0.0.0-127.255.255.255') |
| 103 | 127.0.0.0/8 |
| 104 | >>> print IP('127.0.0.1/255.0.0.0', make_net=True) |
| 105 | 127.0.0.0/8 |
| 106 | |
| 107 | See module documentation for more examples. |
| 108 | """ |
| 109 | |
| 110 | # Print no Prefixlen for /32 and /128 |
| 111 | self.NoPrefixForSingleIp = 1 |
| 112 | |
| 113 | # Do we want prefix printed by default? see _printPrefix() |
| 114 | self.WantPrefixLen = None |
| 115 | |
| 116 | netbits = 0 |
| 117 | prefixlen = -1 |
| 118 | |
| 119 | # handling of non string values in constructor |
| 120 | if type(data) == types.IntType or type(data) == types.LongType: |
| 121 | self.ip = long(data) |
| 122 | if ipversion == 0: |
| 123 | if self.ip < 0x100000000L: |
| 124 | ipversion = 4 |
| 125 | else: |
| 126 | ipversion = 6 |
| 127 | if ipversion == 4: |
| 128 | prefixlen = 32 |
| 129 | elif ipversion == 6: |
| 130 | prefixlen = 128 |
| 131 | else: |
| 132 | raise ValueError, "only IPv4 and IPv6 supported" |
| 133 | self._ipversion = ipversion |
| 134 | self._prefixlen = prefixlen |
| 135 | # handle IP instance as an parameter |
| 136 | elif isinstance(data, IPint): |
| 137 | self._ipversion = data._ipversion |
| 138 | self._prefixlen = data._prefixlen |
nothing calls this directly
no test coverage detected