Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 15b3cbb

Browse files
committed
Merged revisions 73060 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r73060 | gregory.p.smith | 2009-05-30 12:58:11 -0700 (Sat, 30 May 2009) | 2 lines Add more examples to the ipaddr documentation. ........
1 parent 54e8ddf commit 15b3cbb

1 file changed

Lines changed: 90 additions & 2 deletions

File tree

Doc/library/ipaddr.rst

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,90 @@ This module implements classes for working with IP host and network addresses,
1616
both IPv4 and IPv6.
1717

1818

19+
.. _ipaddr_examples:
20+
21+
Examples
22+
--------
23+
24+
Netmask.
25+
26+
>>> ipaddr.IP('1.1.1.1/255.255.255.0')
27+
IPv4('1.1.1.1/24')
28+
>>> ipaddr.IP('1080::200C:417B/96')
29+
IPv6('1080::200c:417b/96')
30+
31+
Hostmask.
32+
33+
>>> ipaddr.IPv4('1.1.1.1/0.0.0.255')
34+
IPv4('1.1.1.1/24')
35+
36+
Prefix length.
37+
38+
>>> addr = ipaddr.IPv4('1.1.1.1/24')
39+
>>> addr.prefixlen
40+
24
41+
42+
Individual addresses.
43+
44+
>>> ipaddr.IP('1.1.1.1')
45+
IPv4('1.1.1.1/32')
46+
47+
Many standard Python operations are also supported.
48+
49+
Comparison.
50+
51+
>>> ipaddr.IPv4('1.1.1.1') == ipaddr.IPv4('1.1.1.2')
52+
False
53+
>>> ipaddr.IPv4('1.1.1.1') < ipaddr.IPv4('1.1.1.2')
54+
True
55+
56+
Inclusion.
57+
58+
>>> ipaddr.IPv4('1.1.1.1') in ipaddr.IPv4("1.0.0.0/8")
59+
True
60+
61+
Sorting.
62+
63+
>>> a = ipaddr.IPv4('1.1.1.10')
64+
>>> b = ipaddr.IPv4('1.10.1.10')
65+
>>> c = ipaddr.IPv4('1.1.10.10')
66+
>>> d = ipaddr.IPv4('1.1.1.1')
67+
>>> sorted([a, b, c, d])
68+
[IPv4('1.1.1.1/32'), IPv4('1.1.1.10/32'), IPv4('1.1.10.10/32'), IPv4('1.10.1.10/32')]
69+
70+
Conversion to string and integer forms.
71+
72+
>>> spam = ipaddr.IPv4('192.168.1.254'))
73+
>>> str(spam)
74+
'192.168.1.254/32'
75+
>>> spam.ip_ext
76+
'192.168.1.254'
77+
>>> int(spam)
78+
3232236030
79+
>>> eggs = ipaddr.IPv6('ffff::1/120')
80+
>>> int(eggs)
81+
340277174624079928635746076935438991361
82+
83+
Additionally, there are quite a few network-specific features available to
84+
ipaddr.
85+
86+
>>> ipaddr.IPv4('10.0.0.0/8').supernet()
87+
IPv4('10.0.0.0/7')
88+
>>> ipaddr.IPv4('10.0.0.0/8').subnet()
89+
[IPv4('10.0.0.0/9'), IPv4('10.128.0.0/9')]
90+
# This returns networks with a prefix length of /10
91+
>>> ipaddr.IPv4('10.0.0.0/8').subnet(prefixlen_diff=2)
92+
[IPv4('10.0.0.0/10'), IPv4('10.64.0.0/10'), IPv4('10.128.0.0/10'), IPv4('10.192.0.0/10')]
93+
# Remove an address from a superblock.
94+
>>> ipaddr.IP('10.0.0.0/24').address_exclude(ipaddr.IP('10.0.0.0/28'))
95+
[IPv4('10.0.0.16/28'), IPv4('10.0.0.32/27'), IPv4('10.0.0.64/26'), IPv4('10.0.0.128/25')]
96+
97+
98+
.. _ipaddr_funcs_and_classes:
99+
100+
Functions And Classes
101+
---------------------
102+
19103
.. function:: IP(ipaddr)
20104

21105
Take an IP string or int and return an object of the correct type. Returns
@@ -159,8 +243,7 @@ both IPv4 and IPv6.
159243
>>> addr1 = IP('::1/32')
160244
>>> addr2 = IP('::1/128')
161245
>>> addr1.address_exclude(addr2)
162-
[IP('::0/128'), IP('::2/127'), IP('::4/126'), IP('::8/125'),
163-
... IP('0:0:8000::/33')]
246+
[IP('::0/128'), IP('::2/127'), IP('::4/126'), IP('::8/125'), IP('0:0:8000::/33')]
164247

165248
Raises :exc:`ValueError` if *other* is not completely contained by *self*.
166249

@@ -297,6 +380,11 @@ both IPv4 and IPv6.
297380
2.5.2.
298381

299382

383+
.. _ipaddr_exceptions:
384+
385+
Exceptions
386+
----------
387+
300388
The following exceptions are defined by this module:
301389

302390
.. exception:: Error

0 commit comments

Comments
 (0)