summaryrefslogtreecommitdiff
path: root/wiki/ExampleUsage-1x.wiki
diff options
context:
space:
mode:
Diffstat (limited to 'wiki/ExampleUsage-1x.wiki')
-rw-r--r--wiki/ExampleUsage-1x.wiki124
1 files changed, 124 insertions, 0 deletions
diff --git a/wiki/ExampleUsage-1x.wiki b/wiki/ExampleUsage-1x.wiki
new file mode 100644
index 0000000..3a978ac
--- /dev/null
+++ b/wiki/ExampleUsage-1x.wiki
@@ -0,0 +1,124 @@
+
+Examples of the functionality of the 1.x version of the ipaddr library. Documentation for the current version (2.x) will be coming shortly.
+
+A lot of the functionality of the IPv4 class is derived from the BaseIP class. The same is true for the IPv6 class. that means that all of the functionality you see here is exactly the same when dealing with IPv6 addresses.
+
+=== IPv4 ===
+_using ipaddr with IPv4 addresses_
+
+Start by creating your address object.
+You can create an IPv4 object with:
+
+netmask
+{{{
+>>> addr = ipaddr.IPv4('1.1.1.1/255.255.255.0')
+>>> addr
+IPv4('1.1.1.1/24')
+}}}
+
+hostmask
+{{{
+>>> addr = ipaddr.IPv4('1.1.1.1/0.0.0.255')
+>>> addr
+IPv4('1.1.1.1/24')
+}}}
+
+prefix length
+{{{
+>>> addr = ipaddr.IPv4('1.1.1.1/24')
+>>> addr
+IPv4('1.1.1.1/24')
+}}}
+
+nothing (nothing implies a /32 netmask)
+{{{
+>>> addr = ipaddr.IPv4('1.1.1.1')
+>>> addr
+IPv4('1.1.1.1/32')
+}}}
+
+Many of the standard Python operations are supported
+{{{
+# comparisons
+>>> ipaddr.IPv4('1.1.1.1') == ipaddr.IPv4('1.1.1.2')
+False
+>>> ipaddr.IPv4('1.1.1.1') < ipaddr.IPv4('1.1.1.2')
+True
+
+# list inclusion
+>>> a = ipaddr.IPv4('1.1.1.1')
+>>> b = ipaddr.IPv4('1.1.1.2')
+>>> c = ipaddr.IPv4('1.1.1.3')
+>>> a in [a, b, c]
+True
+
+# inclusion
+>>> ipaddr.IPv4('1.1.1.1') in ipaddr.IPv4("1.0.0.0/8")
+True
+
+# sorting
+>>> a = ipaddr.IPv4('1.1.1.10')
+>>> b = ipaddr.IPv4('1.10.1.10')
+>>> c = ipaddr.IPv4('1.1.10.10')
+>>> d = ipaddr.IPv4('1.1.1.1')
+>>> sorted([a, b, c, d])
+[IPv4('1.1.1.1/32'), IPv4('1.1.1.10/32'), IPv4('1.1.10.10/32'), IPv4('1.10.1.10/32')]
+
+# str
+>>> str(ipaddr.IP('1.2.3.4'))
+'1.2.3.4/32'
+
+# int/hex.
+>>> int(ipaddr.IP('1.2.3.4'))
+16909060
+>>> hex(ipaddr.IP('1.2.3.4'))
+'0x1020304'
+}}}
+
+Additionally, there are quite a few network-specific features available to ipaddr.
+{{{
+>>> ipaddr.IPv4('10.0.0.0/8').Supernet()
+IPv4('10.0.0.0/7')
+
+>>> ipaddr.IPv4('10.0.0.0/8').Subnet()
+[IPv4('10.0.0.0/9'), IPv4('10.128.0.0/9')]
+
+>>> ipaddr.IPv4('10.0.0.0/8').Subnet(prefixlen_diff=2) # this returns networks with a prefix length of /10
+[IPv4('10.0.0.0/10'), IPv4('10.64.0.0/10'),
+ IPv4('10.128.0.0/10'), IPv4('10.192.0.0/10')]
+
+# AddressExclude removes an address from a superblock.
+>>> ipaddr.IPv4('10.0.0.0/24').AddressExclude(ipaddr.IPv4('10.0.0.0/32'))
+[IPv4('10.0.0.1/32'), IPv4('10.0.0.2/31'),
+ IPv4('10.0.0.4/30'), IPv4('10.0.0.8/29'),
+ IPv4('10.0.0.16/28'), IPv4('10.0.0.32/27'),
+ IPv4('10.0.0.64/26'), IPv4('10.0.0.128/25')]
+
+}}}
+
+=== IPv6 ===
+
+IPv6 methods are exactly the same as their IPv4 counterparts. this is because a lot of the functionality for both the IPv4 and IPv6 classes is inherited from the BaseIP class. For example:
+
+{{{
+>>> addr = ipaddr.IPv6('ffff::1/120')
+>>> addr
+IPv6('ffff::1/120')
+>>> addr.prefixlen
+120
+>>> addr == ipaddr.IPv6('ffff::2/120')
+False
+>>> addr > ipaddr.IPv6('ffff::2/120')
+False
+>>> addr < ipaddr.IPv6('ffff::2/120')
+True
+>>> addr in ipaddr.IPv6('ffff::1/119')
+True
+>>> hex(addr)
+'0xFFFF0000000000000000000000000001L'
+>>> int(addr)
+340277174624079928635746076935438991361L
+}}}
+
+
+As always, consult your local help(ipaddr) pydoc documentation with questions. \ No newline at end of file