|
1 | | -============================== |
| 1 | +############################## |
2 | 2 | iptools - IP Address Utilities |
3 | | -============================== |
4 | | - |
5 | | -Utilities for dealing with IPv4 addresses. |
6 | | - |
7 | | - :Functions: |
8 | | - - :func:`cidr2block`: Convert a CIDR notation ip address into a tuple |
9 | | - containing network block start and end addresses. |
10 | | - - :func:`hex2ip`: Convert a hex encoded network byte order 32-bit |
11 | | - integer to a dotted-quad ip address. |
12 | | - - :func:`ip2hex`: Convert a dotted-quad ip address to a hex encoded |
13 | | - network byte order 32-bit integer. |
14 | | - - :func:`ip2long`: Convert a dotted-quad ip address to a network byte |
15 | | - order 32-bit integer. |
16 | | - - :func:`ip2network`: Convert a dotted-quad ip to base network number. |
17 | | - - :func:`long2ip`: Convert a network byte order 32-bit integer to |
18 | | - a dotted quad ip address. |
19 | | - - :func:`netmask2prefix`: Convert a dotted-quad netmask into a CIDR |
20 | | - prefix. |
21 | | - - :func:`subnet2block`: Convert a dotted-quad ip address including |
22 | | - a netmask into a tuple containing the network block start and end |
23 | | - addresses. |
24 | | - - :func:`validate_cidr`: Validate a CIDR notation ip address. |
25 | | - - :func:`validate_ip`: Validate a dotted-quad ip address. |
26 | | - - :func:`validate_netmask`: Validate that a dotted-quad ip address is |
27 | | - a valid netmask. |
28 | | - - :func:`validate_subnet`: Validate a dotted-quad ip address including |
29 | | - a netmask. |
30 | | - |
31 | | - :Objects: |
32 | | - - :class:`IpRange`: Range of ip addresses supporting ``in`` and iteration. |
33 | | - - :class:`IpRangeList`: List of IpRange objects supporting ``in`` and |
34 | | - iteration. |
35 | | - |
36 | | - :Constants: |
37 | | - - :mod:`iptools.constants`: Common and special use IPv4 address blocks. |
38 | | - |
39 | | - The :class:`IpRangeList` object can be used in a django settings file to |
40 | | - allow CIDR notation and/or (start, end) ranges to be used in the |
41 | | - INTERNAL_IPS list. |
42 | | - |
43 | | - **Example**:: |
44 | | - |
45 | | - INTERNAL_IPS = IpRangeList( |
46 | | - '127.0.0.1', |
47 | | - '192.168/16', |
48 | | - ('10.0.0.1', '10.0.0.19'), |
49 | | - ) |
50 | | - |
51 | | -.. automodule:: iptools |
52 | | - :members: |
53 | | - :exclude-members: IpRange, IpRangeList |
| 3 | +############################## |
| 4 | +The iptools_ package is a collection of utilities for dealing with IP |
| 5 | +addresses. |
| 6 | + |
| 7 | +The project was inspired by a desire to be able to use CIDR_ address notation |
| 8 | +to designate ``INTERNAL_IPS`` in a Django_ project's settings file. |
| 9 | + |
| 10 | + |
| 11 | +************ |
| 12 | +Installation |
| 13 | +************ |
| 14 | +Install the latest stable version from PyPi using pip_: |
| 15 | + |
| 16 | +.. code-block:: bash |
| 17 | +
|
| 18 | + pip install iptools |
| 19 | +
|
| 20 | +or setuptools_: |
| 21 | + |
| 22 | +.. code-block:: bash |
| 23 | +
|
| 24 | + easy_install iptools |
| 25 | +
|
| 26 | +Install the latest development version: |
| 27 | + |
| 28 | +.. code-block:: bash |
| 29 | +
|
| 30 | + git clone https://github.com/bd808/python-iptools.git |
| 31 | + cd python-iptools |
| 32 | + python setup.py install |
| 33 | +
|
| 34 | +
|
| 35 | +****************************** |
| 36 | +Using with Django INTERNAL_IPS |
| 37 | +****************************** |
| 38 | +An :class:`iptools.IpRangeList` object can be used in a Django_ settings file |
| 39 | +to allow CIDR_ and/or ``(start, end)`` ranges to be used in the |
| 40 | +``INTERNAL_IPS`` list. |
| 41 | + |
| 42 | +There are many internal and add-on components for Django_ that use the |
| 43 | +INTERNAL_IPS_ configuration setting to alter application behavior or make |
| 44 | +debugging easier. When you are developing and testing an application by |
| 45 | +yourself it's easy to add the ip address that your web browser will be coming |
| 46 | +from to this list. When you are developing in a group or testing from many ips |
| 47 | +it can become cumbersome to add more and more ip addresses to the setting |
| 48 | +individually. |
| 49 | + |
| 50 | +The :class:`iptools.IpRangeList` object can help by replacing the standard |
| 51 | +tuple of addresses recommended by the Django docs with an intelligent object |
| 52 | +that responds to the membership test operator in. This object can be |
| 53 | +configured with dotted quad IP addresses like the default ``INTERNAL_IPS`` |
| 54 | +tuple (eg. '127.0.0.1'), CIDR_ block notation (eg. '127/8', '192.168/16') for |
| 55 | +entire network blocks, and/or (start, end) tuples describing an arbitrary |
| 56 | +range of IP addresses. |
| 57 | + |
| 58 | +Django_'s internal checks against the ``INTERNAL_IPS`` tuple take the form |
| 59 | +``if addr in INTERNAL_IPS`` or ``if addr not in INTERNAL_IPS``. This works |
| 60 | +transparently with the :class:`iptools.IpRangeList` object because it |
| 61 | +implements the magic method ``__contains__`` which python calls when the |
| 62 | +``in`` or ``not in`` operators are used. |
| 63 | + |
| 64 | +.. code-block:: python |
| 65 | +
|
| 66 | + import iptools |
| 67 | +
|
| 68 | + INTERNAL_IPS = iptools.IpRangeList( |
| 69 | + '127.0.0.1', # single ip |
| 70 | + '192.168/16', # CIDR network block |
| 71 | + ('10.0.0.1', '10.0.0.19'), # arbitrary inclusive range |
| 72 | + ) |
| 73 | +
|
| 74 | +
|
| 75 | +*** |
| 76 | +API |
| 77 | +*** |
| 78 | + |
| 79 | +IpRangeList |
| 80 | +=========== |
| 81 | +.. autoclass:: iptools.IpRangeList |
| 82 | + :members: |
| 83 | + :special-members: |
| 84 | + :show-inheritance: |
| 85 | + |
54 | 86 |
|
55 | 87 | IpRange |
56 | 88 | ======= |
57 | | -.. autoclass:: IpRange |
| 89 | +.. autoclass:: iptools.IpRange |
58 | 90 | :members: |
59 | 91 | :special-members: |
| 92 | + :show-inheritance: |
60 | 93 |
|
61 | | -IpRangeList |
62 | | -=========== |
63 | | -.. autoclass:: IpRangeList |
| 94 | + |
| 95 | +IPv4 |
| 96 | +==== |
| 97 | +.. automodule:: iptools.ipv4 |
64 | 98 | :members: |
65 | | - :special-members: |
| 99 | + :show-inheritance: |
66 | 100 |
|
67 | | -Constants |
68 | | -========= |
69 | | -.. automodule:: iptools.constants |
| 101 | + |
| 102 | +IPv6 |
| 103 | +==== |
| 104 | +.. automodule:: iptools.ipv6 |
70 | 105 | :members: |
| 106 | + :show-inheritance: |
71 | 107 |
|
72 | | -Indices and tables |
73 | | -================== |
74 | 108 |
|
| 109 | +****************** |
| 110 | +Indices and tables |
| 111 | +****************** |
75 | 112 | * :ref:`genindex` |
76 | 113 | * :ref:`search` |
| 114 | + |
| 115 | +.. _iptools: http://pypi.python.org/pypi/iptools |
| 116 | +.. _Django: http://www.djangoproject.com/ |
| 117 | +.. _pip: http://www.pip-installer.org/en/latest/ |
| 118 | +.. _setuptools: https://pypi.python.org/pypi/setuptools |
| 119 | +.. _CIDR: http://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing |
| 120 | +.. _INTERNAL_IPS: http://docs.djangoproject.com/en/dev/ref/settings/#internal-ips |
0 commit comments