Skip to content

Commit 582281d

Browse files
authored
Merge pull request #515 from johngmyers/net2
Improve documentation of net system module
2 parents 84842a9 + b1df980 commit 582281d

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

docs/reference/model/net.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,106 @@ assert net.is_unspecified_IP("::") == True
229229
assert net.is_unspecified_IP("2001:0db8::") == False
230230
assert net.is_unspecified_IP("invalid") == False
231231
```
232+
233+
## parse_CIDR
234+
235+
`parse_CIDR(cidr: str) -> {}`
236+
237+
Parses the CIDR block `cidr` into a dict members.
238+
239+
If `cidr` parses, the returned dict has two members:
240+
`ip` has the first IP in the block and `mask` has the prefix length as an `int`.
241+
242+
If `cidr` does not parse, returns the empty dict, `{}`.
243+
244+
```python
245+
import net
246+
247+
assert net.parse_CIDR("10.0.0.0/8") == { ip: "10.0.0.0", mask: 8 }
248+
assert net.parse_CIDR("2001:db8::/56") == { ip: "2001:db8::", mask: 56 }
249+
assert net.parse_CIDR("invalid") == {}
250+
```
251+
252+
## is_IP_in_CIDR
253+
254+
`is_IP_in_CIDR(ip: str, cidr: str) -> bool`
255+
256+
If `ip` is contained in `cidr`, returns `True`. Otherwise, returns `False`.
257+
258+
If `ip` is an IPv4 address and `cidr` is an IPv6 CIDR, treats `ip` as
259+
if it were encoded as an IPv4-mapped IPv6 address.
260+
261+
```python
262+
import net
263+
264+
assert net.is_IP_in_CIDR("10.1.2.3", "10.0.0.0/8")
265+
assert not net.is_IP_in_CIDR("11.1.2.3", "10.0.0.0/8")
266+
assert net.is_IP_in_CIDR("2001:db8::9", "2001:db8::/56")
267+
assert not net.is_IP_in_CIDR("2fff::9", "2001:db8::/56")
268+
assert net.is_IP_in_CIDR("10.1.2.3", "::/0")
269+
```
270+
271+
## CIDR_subnet
272+
273+
`CIDR_subnet(cidr: str, additional_bits: int, net_num: int) -> str`
274+
275+
Calculates a subnet of the CIDR `cidr`.
276+
277+
Extends the prefix of `cidr` by `additional_bits`. For example, if `cidr` is
278+
a `/18` and `additional_bits` is `6`, then the result will be a `/24`.
279+
280+
`net_num` is a non-negative number used to populate the bits added to the prefix.
281+
282+
```python
283+
import net
284+
285+
assert net.CIDR_subnet("10.0.0.0/8", 8, 11) == "10.11.0.0/16"
286+
assert net.CIDR_subnet("2001:db8::/56", 8, 10) == "2001:db8:0:a::/64"
287+
```
288+
289+
## CIDR_subnets
290+
291+
`CIDR_subnets(cidr: str, additional_bits: [int]) -> [str]`
292+
293+
Allocates a sequence of subnets within `cidr`. `additional_bits` specifies,
294+
for each subnet to allocate, the number of bits by which to extend the prefix
295+
of `cidr`. Returns a list of the allocated subnet CIDRs.
296+
297+
If later called with the same `cidr` and an `additional_bits` with only additions
298+
on the end, will return the same allocations for those previous `additional_bits`.
299+
300+
```python
301+
import net
302+
303+
assert net.CIDR_subnets("10.0.0.0/8", [8, 9, 8]) == ["10.0.0.0/16", "10.1.0.0/17", "10.2.0.0/16"]
304+
assert net.CIDR_subnets("2001:db8::/56", [8, 7]) == ["2001:db8::/64", "2001:db8:0:2::/63"]
305+
```
306+
307+
## CIDR_host
308+
309+
`CIDR_host(cidr: str, host_num: int) -> str`
310+
311+
Calculates an IP for a host within `cidr`.
312+
313+
`host_num` is a number used to populate the bits added to the prefix. If the number is negative,
314+
the count starts from the end of the range.
315+
316+
```python
317+
import net
318+
319+
assert net.CIDR_host("10.0.0.0/8", 11) == "10.0.0.11"
320+
assert net.CIDR_host("10.0.0.0/8", -11) == "10.255.255.245"
321+
assert net.CIDR_host("2001:db8::/56", 10) == "2001:db8::a"
322+
```
323+
324+
## CIDR_netmask
325+
326+
`CIDR_netmask(cidr: str) -> str`
327+
328+
Returns the netmask for the IPv4 subnet `cidr`.
329+
330+
```python
331+
import net
332+
333+
assert net.CIDR_netmask("10.0.0.0/8") == "10.255.255.255"
334+
```

0 commit comments

Comments
 (0)