IPy


NameIPy JSON
Version 1.01 PyPI version JSON
download
home_pagehttps://github.com/autocracy/python-ipy
SummaryClass and tools for handling of IPv4 and IPv6 addresses and networks
upload_time2020-12-01 20:33:09
maintainer
docs_urlNone
authorJeff Ferland
requires_python
licenseBSD License
keywords ipv4 ipv6 netmask
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage
            IPy - class and tools for handling of IPv4 and IPv6 addresses and networks.

Website: https://github.com/autocracy/python-ipy/

Presentation of the API
=======================

The IP class allows a comfortable parsing and handling for most
notations in use for IPv4 and IPv6 addresses and networks. It was
greatly inspired by RIPE's Perl module NET::IP's interface but
doesn't share the implementation. It doesn't share non-CIDR netmasks,
so funky stuff like a netmask of 0xffffff0f can't be done here. ::

    >>> from IPy import IP
    >>> ip = IP('127.0.0.0/30')
    >>> for x in ip:
    ...  print(x)
    ...
    127.0.0.0
    127.0.0.1
    127.0.0.2
    127.0.0.3
    >>> ip2 = IP('0x7f000000/30')
    >>> ip == ip2
    1
    >>> ip.reverseNames()
    ['0.0.0.127.in-addr.arpa.', '1.0.0.127.in-addr.arpa.', '2.0.0.127.in-addr.arpa.', '3.0.0.127.in-addr.arpa.']
    >>> ip.reverseName()
    '0-3.0.0.127.in-addr.arpa.'
    >>> ip.iptype()
    'LOOPBACK'


Supports most IP address formats
================================

It can detect about a dozen different ways of expressing IP addresses
and networks, parse them and distinguish between IPv4 and IPv6 addresses: ::

    >>> IP('10.0.0.0/8').version()
    4
    >>> IP('::1').version()
    6

IPv4 addresses
--------------

::

    >>> print(IP(0x7f000001))
    127.0.0.1
    >>> print(IP('0x7f000001'))
    127.0.0.1
    >>> print(IP('127.0.0.1'))
    127.0.0.1
    >>> print(IP('10'))
    10.0.0.0

IPv6 addresses
--------------

::

    >>> print(IP('1080:0:0:0:8:800:200C:417A'))
    1080::8:800:200c:417a
    >>> print(IP('1080::8:800:200C:417A'))
    1080::8:800:200c:417a
    >>> print(IP('::1'))
    ::1
    >>> print(IP('::13.1.68.3'))
    ::d01:4403

Network mask and prefixes
-------------------------

::

    >>> print(IP('127.0.0.0/8'))
    127.0.0.0/8
    >>> print(IP('127.0.0.0/255.0.0.0'))
    127.0.0.0/8
    >>> print(IP('127.0.0.0-127.255.255.255'))
    127.0.0.0/8


Derive network address
===========================

IPy can transform an IP address into a network address by applying the given
netmask: ::

    >>> print(IP('127.0.0.1/255.0.0.0', make_net=True))
    127.0.0.0/8

This can also be done for existing IP instances: ::

    >>> print(IP('127.0.0.1').make_net('255.0.0.0'))
    127.0.0.0/8


Convert address to string
=========================

Nearly all class methods which return a string have an optional
parameter 'wantprefixlen' which controls if the prefixlen or netmask
is printed. Per default the prefilen is always shown if the network
contains more than one address: ::

    wantprefixlen == 0 / None     don't return anything   1.2.3.0
    wantprefixlen == 1            /prefix                 1.2.3.0/24
    wantprefixlen == 2            /netmask                1.2.3.0/255.255.255.0
    wantprefixlen == 3            -lastip                 1.2.3.0-1.2.3.255

You can also change the defaults on an per-object basis by fiddling with
the class members:

- NoPrefixForSingleIp
- WantPrefixLen

Examples of string conversions: ::

    >>> IP('10.0.0.0/32').strNormal()
    '10.0.0.0'
    >>> IP('10.0.0.0/24').strNormal()
    '10.0.0.0/24'
    >>> IP('10.0.0.0/24').strNormal(0)
    '10.0.0.0'
    >>> IP('10.0.0.0/24').strNormal(1)
    '10.0.0.0/24'
    >>> IP('10.0.0.0/24').strNormal(2)
    '10.0.0.0/255.255.255.0'
    >>> IP('10.0.0.0/24').strNormal(3)
    '10.0.0.0-10.0.0.255'
    >>> ip = IP('10.0.0.0')
    >>> print(ip)
    10.0.0.0
    >>> ip.NoPrefixForSingleIp = None
    >>> print(ip)
    10.0.0.0/32
    >>> ip.WantPrefixLen = 3
    >>> print(ip)
    10.0.0.0-10.0.0.0

Work with multiple networks
===========================

Simple addition of neighboring netblocks that can be aggregated will yield
a parent network of both, but more complex range mapping and aggregation
requires is available with the ``IPSet`` class which will hold any number of
unique address ranges and will aggregate overlapping ranges. ::

    >>> from IPy import IP, IPSet
    >>> IP('10.0.0.0/22') - IP('10.0.2.0/24')
    IPSet([IP('10.0.0.0/23'), IP('10.0.3.0/24')])
    >>> IPSet([IP('10.0.0.0/23'), IP('10.0.3.0/24'), IP('10.0.2.0/24')])
    IPSet([IP('10.0.0.0/22')])
    >>> s = IPSet([IP('10.0.0.0/22')])
    >>> s.add(IP('192.168.1.0/29'))
    >>> s
    IPSet([IP('10.0.0.0/22'), IP('192.168.1.0/29')])
    >>> s.discard(IP('192.168.1.2'))
    >>> s
    IPSet([IP('10.0.0.0/22'), IP('192.168.1.0/31'), IP('192.168.1.3'), IP('192.168.1.4/30')])

``IPSet`` supports the ``set`` method ``isdisjoint``: ::

    >>> s.isdisjoint(IPSet([IP('192.168.0.0/16')]))
    False
    >>> s.isdisjoint(IPSet([IP('172.16.0.0/12')]))
    True

``IPSet`` supports intersection: ::

    >>> s & IPSet([IP('10.0.0.0/8')])
    IPSet([IP('10.0.0.0/22')])

Compatibility and links
=======================

IPy 1.01 works on Python version 2.6 - 3.7.

The IP module should work in Python 2.5 as long as the subtraction operation
is not used. IPSet requires features of the collecitons class which appear
in Python 2.6, though they can be backported.

Eratta
======

When using IPv6 addresses, it is best to compare using  ``IP().len()``
instead of ``len(IP)``. Addresses with an integer value > 64 bits can break
the 2nd method.  See http://stackoverflow.com/questions/15650878 for more
info.

Fuzz testing for ``IPSet`` will throw spurious errors when the ``IPSet`` module
combines two smaller prefixes into a larger prefix that matches the random
prefix tested against.

This Python module is under BSD license: see COPYING file.

Further Information might be available at:
https://github.com/autocracy/python-ipy
            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/autocracy/python-ipy",
    "name": "IPy",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "ipv4 ipv6 netmask",
    "author": "Jeff Ferland",
    "author_email": "jeff_ipy@storyinmemo.com",
    "download_url": "https://files.pythonhosted.org/packages/64/a4/9c0d88d95666ff1571d7baec6c5e26abc08051801feb6e6ddf40f6027e22/IPy-1.01.tar.gz",
    "platform": "",
    "description": "IPy - class and tools for handling of IPv4 and IPv6 addresses and networks.\n\nWebsite: https://github.com/autocracy/python-ipy/\n\nPresentation of the API\n=======================\n\nThe IP class allows a comfortable parsing and handling for most\nnotations in use for IPv4 and IPv6 addresses and networks. It was\ngreatly inspired by RIPE's Perl module NET::IP's interface but\ndoesn't share the implementation. It doesn't share non-CIDR netmasks,\nso funky stuff like a netmask of 0xffffff0f can't be done here. ::\n\n    >>> from IPy import IP\n    >>> ip = IP('127.0.0.0/30')\n    >>> for x in ip:\n    ...  print(x)\n    ...\n    127.0.0.0\n    127.0.0.1\n    127.0.0.2\n    127.0.0.3\n    >>> ip2 = IP('0x7f000000/30')\n    >>> ip == ip2\n    1\n    >>> ip.reverseNames()\n    ['0.0.0.127.in-addr.arpa.', '1.0.0.127.in-addr.arpa.', '2.0.0.127.in-addr.arpa.', '3.0.0.127.in-addr.arpa.']\n    >>> ip.reverseName()\n    '0-3.0.0.127.in-addr.arpa.'\n    >>> ip.iptype()\n    'LOOPBACK'\n\n\nSupports most IP address formats\n================================\n\nIt can detect about a dozen different ways of expressing IP addresses\nand networks, parse them and distinguish between IPv4 and IPv6 addresses: ::\n\n    >>> IP('10.0.0.0/8').version()\n    4\n    >>> IP('::1').version()\n    6\n\nIPv4 addresses\n--------------\n\n::\n\n    >>> print(IP(0x7f000001))\n    127.0.0.1\n    >>> print(IP('0x7f000001'))\n    127.0.0.1\n    >>> print(IP('127.0.0.1'))\n    127.0.0.1\n    >>> print(IP('10'))\n    10.0.0.0\n\nIPv6 addresses\n--------------\n\n::\n\n    >>> print(IP('1080:0:0:0:8:800:200C:417A'))\n    1080::8:800:200c:417a\n    >>> print(IP('1080::8:800:200C:417A'))\n    1080::8:800:200c:417a\n    >>> print(IP('::1'))\n    ::1\n    >>> print(IP('::13.1.68.3'))\n    ::d01:4403\n\nNetwork mask and prefixes\n-------------------------\n\n::\n\n    >>> print(IP('127.0.0.0/8'))\n    127.0.0.0/8\n    >>> print(IP('127.0.0.0/255.0.0.0'))\n    127.0.0.0/8\n    >>> print(IP('127.0.0.0-127.255.255.255'))\n    127.0.0.0/8\n\n\nDerive network address\n===========================\n\nIPy can transform an IP address into a network address by applying the given\nnetmask: ::\n\n    >>> print(IP('127.0.0.1/255.0.0.0', make_net=True))\n    127.0.0.0/8\n\nThis can also be done for existing IP instances: ::\n\n    >>> print(IP('127.0.0.1').make_net('255.0.0.0'))\n    127.0.0.0/8\n\n\nConvert address to string\n=========================\n\nNearly all class methods which return a string have an optional\nparameter 'wantprefixlen' which controls if the prefixlen or netmask\nis printed. Per default the prefilen is always shown if the network\ncontains more than one address: ::\n\n    wantprefixlen == 0 / None     don't return anything   1.2.3.0\n    wantprefixlen == 1            /prefix                 1.2.3.0/24\n    wantprefixlen == 2            /netmask                1.2.3.0/255.255.255.0\n    wantprefixlen == 3            -lastip                 1.2.3.0-1.2.3.255\n\nYou can also change the defaults on an per-object basis by fiddling with\nthe class members:\n\n- NoPrefixForSingleIp\n- WantPrefixLen\n\nExamples of string conversions: ::\n\n    >>> IP('10.0.0.0/32').strNormal()\n    '10.0.0.0'\n    >>> IP('10.0.0.0/24').strNormal()\n    '10.0.0.0/24'\n    >>> IP('10.0.0.0/24').strNormal(0)\n    '10.0.0.0'\n    >>> IP('10.0.0.0/24').strNormal(1)\n    '10.0.0.0/24'\n    >>> IP('10.0.0.0/24').strNormal(2)\n    '10.0.0.0/255.255.255.0'\n    >>> IP('10.0.0.0/24').strNormal(3)\n    '10.0.0.0-10.0.0.255'\n    >>> ip = IP('10.0.0.0')\n    >>> print(ip)\n    10.0.0.0\n    >>> ip.NoPrefixForSingleIp = None\n    >>> print(ip)\n    10.0.0.0/32\n    >>> ip.WantPrefixLen = 3\n    >>> print(ip)\n    10.0.0.0-10.0.0.0\n\nWork with multiple networks\n===========================\n\nSimple addition of neighboring netblocks that can be aggregated will yield\na parent network of both, but more complex range mapping and aggregation\nrequires is available with the ``IPSet`` class which will hold any number of\nunique address ranges and will aggregate overlapping ranges. ::\n\n    >>> from IPy import IP, IPSet\n    >>> IP('10.0.0.0/22') - IP('10.0.2.0/24')\n    IPSet([IP('10.0.0.0/23'), IP('10.0.3.0/24')])\n    >>> IPSet([IP('10.0.0.0/23'), IP('10.0.3.0/24'), IP('10.0.2.0/24')])\n    IPSet([IP('10.0.0.0/22')])\n    >>> s = IPSet([IP('10.0.0.0/22')])\n    >>> s.add(IP('192.168.1.0/29'))\n    >>> s\n    IPSet([IP('10.0.0.0/22'), IP('192.168.1.0/29')])\n    >>> s.discard(IP('192.168.1.2'))\n    >>> s\n    IPSet([IP('10.0.0.0/22'), IP('192.168.1.0/31'), IP('192.168.1.3'), IP('192.168.1.4/30')])\n\n``IPSet`` supports the ``set`` method ``isdisjoint``: ::\n\n    >>> s.isdisjoint(IPSet([IP('192.168.0.0/16')]))\n    False\n    >>> s.isdisjoint(IPSet([IP('172.16.0.0/12')]))\n    True\n\n``IPSet`` supports intersection: ::\n\n    >>> s & IPSet([IP('10.0.0.0/8')])\n    IPSet([IP('10.0.0.0/22')])\n\nCompatibility and links\n=======================\n\nIPy 1.01 works on Python version 2.6 - 3.7.\n\nThe IP module should work in Python 2.5 as long as the subtraction operation\nis not used. IPSet requires features of the collecitons class which appear\nin Python 2.6, though they can be backported.\n\nEratta\n======\n\nWhen using IPv6 addresses, it is best to compare using  ``IP().len()``\ninstead of ``len(IP)``. Addresses with an integer value > 64 bits can break\nthe 2nd method.  See http://stackoverflow.com/questions/15650878 for more\ninfo.\n\nFuzz testing for ``IPSet`` will throw spurious errors when the ``IPSet`` module\ncombines two smaller prefixes into a larger prefix that matches the random\nprefix tested against.\n\nThis Python module is under BSD license: see COPYING file.\n\nFurther Information might be available at:\nhttps://github.com/autocracy/python-ipy",
    "bugtrack_url": null,
    "license": "BSD License",
    "summary": "Class and tools for handling of IPv4 and IPv6 addresses and networks",
    "version": "1.01",
    "project_urls": {
        "Download": "https://github.com/autocracy/python-ipy",
        "Homepage": "https://github.com/autocracy/python-ipy"
    },
    "split_keywords": [
        "ipv4",
        "ipv6",
        "netmask"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "64a49c0d88d95666ff1571d7baec6c5e26abc08051801feb6e6ddf40f6027e22",
                "md5": "d6cf83e7f418ebbd23324ba1c658b907",
                "sha256": "edeca741dea2d54aca568fa23740288c3fe86c0f3ea700344571e9ef14a7cc1a"
            },
            "downloads": -1,
            "filename": "IPy-1.01.tar.gz",
            "has_sig": false,
            "md5_digest": "d6cf83e7f418ebbd23324ba1c658b907",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 33641,
            "upload_time": "2020-12-01T20:33:09",
            "upload_time_iso_8601": "2020-12-01T20:33:09.808845Z",
            "url": "https://files.pythonhosted.org/packages/64/a4/9c0d88d95666ff1571d7baec6c5e26abc08051801feb6e6ddf40f6027e22/IPy-1.01.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2020-12-01 20:33:09",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "autocracy",
    "github_project": "python-ipy",
    "travis_ci": true,
    "coveralls": true,
    "github_actions": false,
    "lcname": "ipy"
}
        
Elapsed time: 0.16197s