summaryrefslogtreecommitdiff
path: root/wiki
diff options
context:
space:
mode:
authorsmart@google.com <smart@google.com@09200d28-7f98-11dd-ad27-0f66e57d2035>2009-01-06 15:50:24 +0000
committersmart@google.com <smart@google.com@09200d28-7f98-11dd-ad27-0f66e57d2035>2009-01-06 15:50:24 +0000
commita50a39a06e72451e4ccddcf2b7924986ef03e03d (patch)
tree30927715fa1fdac40045039816b5844a25462e45 /wiki
parent9e48855f53e2c633d991e46f8bc0347a4bb66bd4 (diff)
downloadipaddr-py-a50a39a06e72451e4ccddcf2b7924986ef03e03d.tar.gz
Created wiki page through web user interface.
git-svn-id: https://ipaddr-py.googlecode.com/svn@41 09200d28-7f98-11dd-ad27-0f66e57d2035
Diffstat (limited to 'wiki')
-rw-r--r--wiki/ExampleUsage.wiki98
1 files changed, 98 insertions, 0 deletions
diff --git a/wiki/ExampleUsage.wiki b/wiki/ExampleUsage.wiki
new file mode 100644
index 0000000..84ce0ab
--- /dev/null
+++ b/wiki/ExampleUsage.wiki
@@ -0,0 +1,98 @@
+Examples of the functionality of the ipaddr library.
+
+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. pending submission of issue 11862. http://codereview.appspot.com/11862/show
+>>> 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')]
+
+}}}
+
+As always, consult your local help(ipaddr) pydoc documentation with questions. \ No newline at end of file