Giter Site home page Giter Site logo

bd808 / python-iptools Goto Github PK

View Code? Open in Web Editor NEW
70.0 8.0 17.0 113 KB

A few useful functions and objects for manipulating ip addresses in python.

Home Page: http://python-iptools.readthedocs.org/

License: BSD 2-Clause "Simplified" License

Python 100.00%
python django cidr ipv4 ipv6

python-iptools's Introduction

python-iptools

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

Build Status

A few useful functions and objects for manipulating IPv4 and IPv6 addresses in python. 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

The IpRangeList object can be used in a Django settings file to allow CIDR notation 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 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 IpRangeList object because it implements the magic method __contains__ which python calls when the in or not in operators are used.

Example:

#!/usr/bin/env python
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
)

Documentation

Full pydoc documentation is available at Read the Docs.

Local documentation can be built using Sphinx:

cd docs
make html

Python Version Compatibility

Travis CI automatically runs tests against Python 2.7, 3.5, 3.6, 3.7, 3.8, pypy, and pypy3.

Installation

Install the latest stable version using pip:

pip install iptools

or easy_install:

easy_install iptools

Install the latest development version:

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

Contributions

Bug reports, feature requests and pull requests are accepted. Preference is given to issues with well-defined acceptance criteria and/or unit tests.

This project was originally hosted on Google Code.


python-iptools's People

Contributors

bd808 avatar cclauss avatar jellonek avatar jtriley avatar opoplawski avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-iptools's Issues

OverflowError when getting a list of addresses

Using one of the IPv6 addresses from your example:

x = iptools.IpRangeList('fe80::/10')
print list(x)

Gives the following OverflowError:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/david/.virtualenvs/env/lib/python2.6/site-packages/iptools/__init__.py", line 514, in __len__
    return sum([len(r) for r in self.ips])
OverflowError: long int too large to convert to int

IP address anonymization possible?

Is it possible to use this library to parse IPv4 and IPv6 addresses and anonymize them like Google Analytics does?

That is:

The IP anonymization feature in Analytics sets the last octet of IPv4 user IP addresses and the last 80 bits of IPv6 addresses to zeros

For example, an IP address of 12.214.31.144 would be changed to 12.214.31.0. (If the IP address is an IPv6 address, the last 80 of the 128 bits are set to zero.)

IpRangeList contains duplicates

I did not expect this behavior.

>>> list(iptools.IpRangeList('2.2.2.3/32', ('2.2.2.2', '2.2.2.5')))
['2.2.2.3', '2.2.2.2', '2.2.2.3', '2.2.2.4', '2.2.2.5']

Yes, I know set exists

Fixes for filtering by huge IP lists

At the moment the package is quite slow to use for large IP lists.

The reason is that __contains__ method of IpRangeList does not call _cast, which gets called in the loop of IpRange for each list item. The fix is to duplicate the casting line from IpRange to IpRangeList

In my test for a list of ~1000 IP addresses the __contains__ time for a string input went down from 0.024s to 3.7e-05, almost a factor of 1000 after adding iptools.ipv4.ip2long call on the application side

I am not sure however what is the right way to generalize this for ipv4/ipv6 mix...

IPRangeList() => TypeError: expected string or bytes-like object, got 'list'

I am optionally using this (to time save a local DHCP address allocation and guess work) and I get this when I run:

django-admin check auth admin as I am configuring the Admin Site for Development & Local environments.

 File "D:\Code\Code Institute\dash-and-do-github\dash_and_do\settings\__init__.py", line 7, 
    in <module> from .development import *
  File "D:\Code\Code Institute\dash-and-do-github\dash_and_do\settings\development.py", line 53, 
    in <module> INTERNAL_IPS = IpRangeList(['127.0.0.1/24', '192.168.0.0/16'])
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Code\Code Institute\dash-and-do-github\venv\Lib\site-packages\iptools\__init__.py", line 414, 
    in __init__ self.ips = tuple(map(IpRange, args))
                    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Code\Code Institute\dash-and-do-github\venv\Lib\site-packages\iptools\__init__.py", line 141, 
     in __init__ elif ipv4.validate_cidr(start):
                     ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Code\Code Institute\dash-and-do-github\venv\Lib\site-packages\iptools\ipv4.py", line 257, 
     in validate_cidr if _CIDR_RE.match(s):
                              ^^^^^^^^^^^^^^^^^
TypeError: expected string or bytes-like object, got 'list'

Drop Python 2.x support in 1.0.0 release

January 1, 2020 was quite a while ago. It may not seem like that long to some of us who have lost our ability to measure the passage of time as part of the COVID-19 pandemic, but it really was 3.5+ years ago now. 0.7.0 will live on until PyPi throws it out for folks who still need a py2 compatible library.

Don't install tests

When installing to the system location the tests get installed to the main python library directory. This could conflict with a "tests" package. Also, it just may not make sense to install the tests.

iptools.ipv4.validate_netmask() seems to be auto-filling 0's

import iptools netmask = '255' valid = iptools.ipv4.validate_netmask(netmask)
This returns True. 255 is not a valid netmask. It seems that this is auto-filling the 0's to make it a full netmask. We were using this tool to check and see if our users are entering valid IP addresses, netmasks, and gateways. Since it is auto-filling if you only have the first octet and nothing else this is not a valid tool which we can use.

Also, we tried inputting '0.255.255.0' and this method returned True.

Shortcut IPv6-address is interpreted as IPv4

An address in the form '::N' (with N being a Hexadecimal digit) is interpreted as IPv4-address.

Example:

>>> iptools.IpRangeList(iptools.ipv6.LOCALHOST)
IpRangeList(IpRange('0.0.0.1', '0.0.0.1'),)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.