GithubHelp home page GithubHelp logo

jack8daniels2 / pysubnettree Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zeek/pysubnettree

0.0 2.0 0.0 584 KB

Official mirror of git.bro.org/pysubnettree.git

License: Other

CMake 1.36% Makefile 0.63% C++ 79.82% Python 2.89% C 15.30%

pysubnettree's Introduction

PySubnetTree - A Python Module for CIDR Lookups

.. rst-class:: opening

    The PySubnetTree package provides a Python data structure
    ``SubnetTree`` which maps subnets given in `CIDR
    <http://tools.ietf.org/html/rfc4632>`_ notation (incl.
    corresponding IPv6 versions) to Python objects. Lookups are
    performed by longest-prefix matching.


Download

You can find the latest PySubnetTree release for download at http://www.bro.org/download.

PySubnetTree's git repository is located at git://git.bro.org/pysubnettree.git. You can browse the repository here.

This document describes PySubnetTree 0.24. See the CHANGES file for version history.

Example

A simple example which associates CIDR prefixes with strings:

>>> import SubnetTree
>>> t = SubnetTree.SubnetTree()
>>> t["10.1.0.0/16"] = "Network 1"
>>> t["10.1.42.0/24"] = "Network 1, Subnet 42"
>>> print("10.1.42.1" in t)
True
>>> print(t["10.1.42.1"])
Network 1, Subnet 42
>>> print(t["10.1.43.1"])
Network 1
>>> print("10.20.1.1" in t)
False
>>> try:
...     print(t["10.20.1.1"])
... except KeyError as err:
...     print("Error: %s not found" % err)
Error: '10.20.1.1' not found

PySubnetTree also supports IPv6 addresses and prefixes:

>>> import SubnetTree
>>> t = SubnetTree.SubnetTree()
>>> t["2001:db8::/32"] = "Company 1"
>>> t["2001:db8:4000::/48"] = "Company 1, Site 1"
>>> t["2001:db8:4000:abcd::"]
Company 1, Site 1
>>> t["2001:db8:fe:1234::"]
Company 1

By default, CIDR prefixes and IP addresses are given as strings. Alternatively, a SubnetTree object can be switched into binary mode, in which single addresses are passed in the form of packed binary strings as, e.g., returned by socket.inet_aton:

>>> t.get_binary_lookup_mode()
False
>>> t.set_binary_lookup_mode(True)
>>> t.get_binary_lookup_mode()
True
>>> import socket
>>> print(t[socket.inet_aton("10.1.42.1")])
Network 1, Subnet 42

A SubnetTree also provides methods insert(prefix,object=None) for insertion of prefixes (object can be skipped to use the tree like a set), and remove(prefix) for removing entries (remove performs an _exact_ match rather than longest-prefix).

Internally, the CIDR prefixes of a SubnetTree are managed by a Patricia tree data structure and lookups are therefore efficient even with a large number of prefixes.

PySubnetTree comes with a BSD license.

Prerequisites

This package requires Python 2.4 or newer.

Installation

Installation is pretty simple:

> python setup.py install

pysubnettree's People

Contributors

0xxon avatar hstern avatar jsiwek avatar kitterma avatar mavam avatar rsmmr avatar

Watchers

 avatar  avatar

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.