This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
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


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!

Release History

Release History

This version
History Node


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