Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

network_dict creates a network subnet based dictionary that returns the most specific subnet(s) for a given IP.

Project Description

Summary

network_dict creates a network subnet based dictionary that returns the most specific subnet(s) for a given IP. It will work equally with both IPv4 and IPv6.

There’s a few more simple bells and whistles to make the library useful in different circumstances.

This is a case where examples speak louder than words…

Simple Example

from network_dict import NetworkDict

networks = {
    '0.0.0.0/0': 'Everything',
    '10.0.0.0/8': 'Office',
    '10.1.0.0/16': 'Region 1',
    '10.1.1.0/255.255.255.0':  'City 1' # Can take multiple netmasks
    }

ns = NetworkDict(networks)

>>> nd['10.1.1.1']
'City 1'

>>> nd.firstHit = False
# Return all matching values in a list
# Results are in order, most to least specific
>>> nd['10.1.1.1']
['City 1', 'Region 1', 'Office', 'Everything']

>>> nd.format = both
# return both the subnet and value in a tuple
>>> nd['10.1.1.1']
[('10.1.1.0/24', 'City 1'), ('10.1.0.0/16', 'Region 1'), ('10.0.0.0/8', 'Office'), ('0.0.0.0/0', 'Everything')]

>>> nd.format = key
# return just the subnet address
>>> nd['10.1.1.1']
['10.1.1.0/24', '10.1.0.0/16', '10.0.0.0/8', '0.0.0.0/0']

Adding Subnets

>>>  nd['192.168.1.1']
['0.0.0.0/0']
# If 0.0.0.0/0 is not set, will return KeyError exception
>>> nd['192.168.1.1/16'] = 'Home'
>>> nd['192.168.1.1']
['192.168.0.0/16', '0.0.0.0/16']
# Note that the key was normalized to a proper subnet

Hosts and /32 prefixes

>>> nd['10.1.1.1'] = 'Router'
>>> nd['10.1.1.1']
['10.1.1.0/24', '10.1.0.0/16', '10.0.0.0/8', '0.0.0.0/0']
# Hosts are ignored by default
>>> nd.ignoreHosts = False
>>> nd['10.1.1.1'] = 'Router'
['10.1.1.1/32', '10.1.1.0/24', '10.1.0.0/16', '10.0.0.0/8', '0.0.0.0/0']

IPv6 Subnets

>>> nd['::1']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: No matching networks found, and no default network set
# we didn't set '::/0', which is different from the '0.0.0.0/0' which is already set
>>> nd['::1/128'] = 'Localhost'
# Note: /128 is a hostmask, so will be ignored if ignoreHosts = True (default)
>>> nd['::1']
['::1/128']

Setting options at creation

>>> nd = NetworkDict(format = 'both', firstHit = False, ignoreHosts = True)
# Returns an empty NetworkDict object, but with default options set

Requirements

  • Tested on python 2.8
  • netaddr library

Installation

Via pip or easy_install

$ sudo pip install network_dict   # If you prefer PIP

$ sudo easy_install network_dict  # If you prefer easy_install

Manual installation

$ git clone https://github.com/neoCrimeLabs/python-network_dict.git
$ cd python-network_dict
$ sudo python setup.py install

Conditions of Use

I wrote this library for my own use, but realized others may find it useful.

Unfortunately I cannot guarentee any active support, but will do my best as time permits. That said, I’ll happily accept push requests with suitable changes that address the general audience of this library.

Put simply, use this at your own risk. If it works, great! If not, I may not be able to help you. If you fix anything, however, please push it back and I’ll likely accept it. :-)

Also, if you use this library in your package, tool, or comercial software, let me know, and I’ll list it here!

Release History

Release History

This version
History Node

0.1

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
network_dict-0.1.tar.gz (15.5 kB) Copy SHA256 Checksum SHA256 Source Aug 13, 2014

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting