Package netaddr :: Module strategy :: Class IPv4StrategyOpt |
[frames] | no frames]

# Class IPv4StrategyOpt

source code

      object --+
               |
    AddrStrategy --+
                   |
     IPv4StrategyStd --+
                       |
                      IPv4StrategyOpt

An optimised AddrStrategy for IPv4 addresses.

It uses `pack()` and `unpack()` from the `struct` module along with the `inet_ntoa()` and `inet_aton()` from the `socket` module great improve the speed of certain operations (approx. 2.5 times faster than a standard AddrStrategy configured for IPv4).

However, keep in mind that these modules might not be available everywhere that Python itself is. Runtimes such as Google App Engine gut the `socket` module. `struct` is also limited to processing 32-bit integers which is fine for IPv4 but isn't suitable for IPv6.

Instance Methods
__init__(self) Constructor. | source code
valid_str(self, addr) Returns: `True` if network address in string form is valid for this address type, `False` otherwise. | source code
str_to_int(self, addr) Returns: A network byte order integer that is equivalent to value represented by the IPv4 dotted decimal address string. | source code
int_to_str(self, int_val) Returns: An IPv4 dotted decimal address string that is equivalent to value represented by a 32 bit integer in network byte order. | source code
int_to_words(self, int_val, num_words=None, word_size=None) Returns: An integer word (octet) sequence that is equivalent to value represented by network byte order integer. | source code
words_to_int(self, octets) Returns: A network byte order integer that is equivalent to value represented by word (octet) sequence. | source code
Inherited from `IPv4StrategyStd`: `int_to_arpa` Inherited from `AddrStrategy`: `__repr__`, `bits_to_int`, `int_to_bin`, `int_to_bits`, `int_to_packed`, `packed_to_int`, `valid_bits`, `valid_int`, `valid_words` Inherited from `object`: `__delattr__`, `__getattribute__`, `__hash__`, `__new__`, `__reduce__`, `__reduce_ex__`, `__setattr__`, `__str__`

Class Variables
Inherited from `AddrStrategy`: `STRUCT_FORMATS`

Properties
Inherited from `object`: `__class__`

Method Details

### __init__(self) (Constructor) | source code
Constructor. Parameters: `width` - size of address in bits. (e.g. 32 - IPv4, 48 - MAC, 128 - IPv6) `word_size` - size of each word. (e.g. 8 - octets, 16 - hextets) `word_sep` - separator between each word. (e.g. '.' - IPv4, ':' - IPv6, '-' - EUI-48) `word_fmt` - format string for each word. (Default: '%x') `addr_type` - address type. (Default: AT_UNSPEC) `word_base` - number base used to convert each word using int(). (Default: 16) `uppercase` - uppercase address. (Default: False) Overrides: object.__init__

### valid_str(self, addr) | source code
Parameters: `addr` - An IP address in presentation (string) format. Returns: `True` if network address in string form is valid for this address type, `False` otherwise. Overrides: AddrStrategy.valid_str

### str_to_int(self, addr) | source code
Parameters: `addr` - An IPv4 dotted decimal address in string form. Returns: A network byte order integer that is equivalent to value represented by the IPv4 dotted decimal address string. Overrides: AddrStrategy.str_to_int

### int_to_str(self, int_val) | source code
Parameters: `int_val` - A network byte order integer. Returns: An IPv4 dotted decimal address string that is equivalent to value represented by a 32 bit integer in network byte order. Overrides: AddrStrategy.int_to_str

### int_to_words(self, int_val, num_words=None, word_size=None) | source code
Parameters: `int_val` - A network byte order integer. `num_words` - (unused) *** interface compatibility only *** `word_size` - (unused) *** interface compatibility only *** Returns: An integer word (octet) sequence that is equivalent to value represented by network byte order integer. Overrides: AddrStrategy.int_to_words

### words_to_int(self, octets) | source code
Parameters: `octets` - A list or tuple containing integer octets. Returns: A network byte order integer that is equivalent to value represented by word (octet) sequence. Overrides: AddrStrategy.words_to_int