Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
140 lines (103 sloc) 3.89 KB

iptools - IP Address Utilities

The iptools package is a collection of utilities for dealing with IP addresses.

The project was inspired by a desire to be able to use CIDR address notation to designate INTERNAL_IPS in a Django project's settings file.

Using with Django INTERNAL_IPS

An :class:`iptools.IpRangeList` object can be used in a Django settings file to allow CIDR and/or (start, end) ranges to be used in the INTERNAL_IPS list.

There are many internal and add-on components for Django that use the INTERNAL_IPS configuration setting to alter application behavior or make debugging easier. When you are developing and testing an application by yourself it's easy to add the ip address that your web browser will be coming from to this list. When you are developing in a group or testing from many ips it can become cumbersome to add more and more ip addresses to the setting individually.

The :class:`iptools.IpRangeList` object can help by replacing the standard tuple of addresses recommended by the Django docs with an intelligent object that responds to the membership test operator in. This object can be configured with dotted quad IP addresses like the default INTERNAL_IPS tuple (eg. '127.0.0.1'), CIDR block notation (eg. '127/8', '192.168/16') for entire network blocks, and/or (start, end) tuples describing an arbitrary range of IP addresses.

Django's internal checks against the INTERNAL_IPS tuple take the form if addr in INTERNAL_IPS or if addr not in INTERNAL_IPS. This works transparently with the :class:`iptools.IpRangeList` object because it implements the magic method __contains__ which python calls when the in or not in operators are used.

import iptools

INTERNAL_IPS = iptools.IpRangeList(
    '127.0.0.1',                # single ip
    '192.168/16',               # CIDR network block
    ('10.0.0.1', '10.0.0.19'),  # arbitrary inclusive range
    '::1',                      # single IPv6 address
    'fe80::/10',                # IPv6 CIDR block
    '::ffff:172.16.0.2'         # IPv4-mapped IPv6 address
)

Python Version Compatibility

Travis CI automatically runs tests against python 2.6, 2.7, 3.2, 3.3 and pypy.

Current test status: Build Status

Installation

Install the latest stable version from PyPi using pip:

$ pip install iptools

or setuptools:

$ easy_install iptools

Install the latest development version:

$ git clone https://github.com/bd808/python-iptools.git
$ cd python-iptools
$ python setup.py install

API

iptools

.. automodule:: iptools

iptools.IpRangeList

.. autoclass:: iptools.IpRangeList
  :members:
  :special-members:


iptools.IpRange

.. autoclass:: iptools.IpRange
  :members:
  :special-members:


iptools.ipv4

.. automodule:: iptools.ipv4
  :members:


iptools.ipv6

.. automodule:: iptools.ipv6
  :members:


Indices and tables