Skip to main content

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

Project description


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 = {
    '': 'Everything',
    '': 'Office',
    '': 'Region 1',
    '':  'City 1' # Can take multiple netmasks

ns = NetworkDict(networks)

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

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

>>> nd.format = both
# return both the subnet and value in a tuple
>>> nd['']
[('', 'City 1'), ('', 'Region 1'), ('', 'Office'), ('', 'Everything')]

>>> nd.format = key
# return just the subnet address
>>> nd['']
['', '', '', '']

Adding Subnets

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

Hosts and /32 prefixes

>>> nd[''] = 'Router'
>>> nd['']
['', '', '', '']
# Hosts are ignored by default
>>> nd.ignoreHosts = False
>>> nd[''] = 'Router'
['', '', '', '', '']

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 '' which is already set
>>> nd['::1/128'] = 'Localhost'
# Note: /128 is a hostmask, so will be ignored if ignoreHosts = True (default)
>>> nd['::1']

Setting options at creation

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


  • Tested on python 2.8
  • netaddr library


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
$ cd python-network_dict
$ sudo python 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!

Project details

Release history Release notifications | RSS feed

This version


Download files

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

Files for network_dict, version 0.1
Filename, size File type Python version Upload date Hashes
Filename, size network_dict-0.1.tar.gz (15.5 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page