Skip to main content

Binip

Description:

Library for IP networking and subnetting.

Installation:

Download .whl file and run:

pip install binip

Usage:

Import the desired classes and or functions:

from binip.classes import [class]
from binip.functions import [function]

Documentation:

Classes:

IP:

Input: IPv4 or IPv6 address.

ipv4 = IP('192.168.1.24')
ipv6 = IP('ac43:34f:45bc:2c:0:0:0:12')

Attributes:

  • address: str, IP address.

      ipv4.address
      '192.168.1.24'
    
  • iptype: str, 'v4' or 'v6'.

      ipv4.iptype
      'v4'
    
  • expanded(for IPv6 only):str, expanded IPv6 address.

      ipv6.expanded
      'ac43:034f:45bc:002c:0000:0000:0000:0012'
    
  • contracted(for IPv6 only):str, contracted IPv6 address.

      ipv6.contracted
      'ac43:34f:45bc:2c::12'
    

Methods:

  • validate_address:

    Validates a given IP address, works for both IPv4 and IPv6.

  • ip_type:

    Returns either 'v4' or 'v6'.

  • ipv6_expand:

    Given a shortened IPv6 address will return the unshortened version.

  • ipv6_contract:

    Returns shortened IPv6 address. Removes leading zeros and contracts largest set of repeating zero hexadecatets.

  • binip:

    Given an IP will return the IP in binary format. Works for both IPv4 and IPv6.

  • in_subnet

    Given a subnet will return True if the IP is in that subnet, will return False if otherwise. Works for both IPv4 and IPv6.

Subnet:

Input: IPv4 or IPv6 subnet address, CIDR notation.

subnetv4 = Subnet('192.168.1.12/24')
subnetv6 = Subnet('ac43:34f:45bc:2c:0:0:0:12/64')

Attributes:

  • subnet: str, subnet address.

      subnetv4.subnet
      '192.168.1.12/24'
    
  • network: str, network address for subnet.

      subnetv4.network
      '192.168.1.0'
    
  • networkcidr: str, network address for subnet in CIDR notation.

      subnetv4.networkcidr
      '192.168.1.0/24'
    
  • broadcast: str, broadcast address for subnet.

      subnetv4.broadcast
      '192.168.1.255'
    
  • broadcastcidr: str, broadcast address for subnet in CIDR notation.

      subnetv4.broadcastcidr
      '192.168.1.255/24'
    
  • mask: int, subnet mask.

      subnetv4.mask
      24
    
  • iptype: str, 'v4' or 'v6'.

      subnetv4.iptype
      'v4'
    
  • info: dict, info on subnet.

      subnetv4.info
      {'Network address:': '192.168.1.0', 'Broadcast address': '192.168.1.255', 'Number of client IPs:': 254, 'Client IP range:': '192.168.1.1 - 192.168.1.254'}
    

Methods:

  • validate_address:

    Validates a given IP address, works for both IPv4 and IPv6.

  • ip_type:

    Returns either 'v4' or 'v6'.

  • ipv6_expand:

    Given a shortened IPv6 address will return the unshortened version.

  • ipv6_contract:

    Returns shortened IPv6 address. Removes leading zeros and contracts largest set of repeating zero hexadecatets.

  • binip:

    Given an IP will return the IP in binary format. Works for both IPv4 and IPv6.

  • in_subnet:

    Given an IP will return True if the IP is in the subnet, will return False if otherwise. Works for both IPv4 and IPv6.

  • subnet_info:

    Returns the network address, broadcast address and number of client IPs available for the subnet.

  • toRegexv4:

    Returns a RegEx pattern to match the given IPv4 subnet.

  • toRegexv6:

    Returns a RegEx pattern to match the given IPv6 subnet.

  • ipgen:

    Generator function to iterate over IPs in the Subnet given a starting index, ending index and step.

Functions:

  • ip_type: Returns either 'v4' or 'v6'. Works for both IP and subnet addresses.

      ip_type('192.168.1.24')
      v4
    
  • ipv6_expand: Given a shortened IPv6 address will return the unshortened version.

      ipv6_expand('ac43:34f:45bc:2c::12')
      'ac43:034f:45bc:002c:0000:0000:0000:0012'
    
  • ipv6_contract: Returns shortened IPv6 address. Removes leading zeros and contracts largest set of repeating zero hexadecatets.

      ipv6_contract('ac43:034f:45bc:002c:0000:0000:0000:0012')
      'ac43:34f:45bc:2c::12'
    
  • ip2bin: Given an IP address, in either decimal or hexadecimal format, returns the same IP address in binary format.

      ip2bin('192.168.1.24')
      '11000000101010000000000100011000'
    
  • bin2ip: Given an IP address in binary format returns the same IP address in either decimal or hexadecimal format.

      bin2ip('11000000101010000000000100011000')
      '192.168.1.24'
    
  • in_subnet: Given a subnet and an IP will return True if the IP is in that subnet, will return False if otherwise. Works for both IPv4 and IPv6.

      in_subnet('192.168.1.24', '192.168.1.0/24')
      True
      in_subnet('192.168.1.24', '192.168.2.0/24')
      False
    
  • hex_range: Given the first and last hexadecimal values of a range returns a list of ReGex patterns to match each value of that range. Used in the toRegexv6 function.

      hex_range('0000', 'aaaa')
      hex_range('0000', 'aaaa')
      ['[0-9a-f]{0,1}', '[0-9a-f]{0,1}[0-9a-f]{0,1}', '[0-9a-f]{1}[0-9a-f]{0,2}', '[1-9]{0,1}[0-9a-f]{0,3}', 'aaa[0-9a-a]{0,1}', 'aa[0-9a-9]{1}[0-9a-f]{0,1}', 'a[0-9a-9]{1}[0-9a-f]{0,2}']
    
  • toRegexv4: Returns a RegEx pattern to match the given IPv4 subnet. Used in the toRegex function.

      toRegexv4('192.168.1.0/14')
      '192.[1][6][8-9].[0-9]{1,3}.[0-9]{1,3}|192.[1][7][0-1].[0-9]{1,3}.[0-9]{1,3}'
    
  • toRegexv6: Returns a RegEx pattern to match the given IPv6 subnet. Used in the toRegex function.

      toRegexv6('ac43:34f:45bc:2c::12/56')
      'ac43:34f:45bc:[0-9a-f]{0,1}:.*|ac43:34f:45bc:[0-9a-f]{0,1}[0-9a-f]{0,1}:.*|ac43:34f:45bc:[1-9a-e]{1}[0-9a-f]{0,1}:.*|ac43:34f:45bc:f[0-9a-f]{0,1}:.*'
    
  • toRegex: Returns a RegEx pattern to match the given subnet. Works for both IPv4 and IPv6.

      toRegexv4('192.168.1.0/14')
      '192.[1][6][8-9].[0-9]{1,3}.[0-9]{1,3}|192.[1][7][0-1].[0-9]{1,3}.[0-9]{1,3}'
      toRegexv6('ac43:34f:45bc:2c::12/56')
      'ac43:34f:45bc:[0-9a-f]{0,1}:.*|ac43:34f:45bc:[0-9a-f]{0,1}[0-9a-f]{0,1}:.*|ac43:34f:45bc:[1-9a-e]{1}[0-9a-f]{0,1}:.*|ac43:34f:45bc:f[0-9a-f]{0,1}:.*'
    

License:

Basic MIT license.

Download files

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

Source Distribution

binip-1.0.0.tar.gz (14.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

binip-1.0.0-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

Details for the file binip-1.0.0.tar.gz.

File metadata

  • Download URL: binip-1.0.0.tar.gz
  • Upload date:
  • Size: 14.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for binip-1.0.0.tar.gz
Algorithm Hash digest
SHA256 6a4d167e49444dbff52b51447f0531528a19811267c50d7900cd4b6630e3153d
MD5 e5f649ba90a5294eb8e696cda184bbd3
BLAKE2b-256 fa96e2a0590ceb59e4520e8e4b8c9cf338dd449a8f22225910bc752be09f6f8a

See more details on using hashes here.

File details

Details for the file binip-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: binip-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 15.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for binip-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 10b7b4a9b4605024f3c258c19b984f35e285e82653e99541fb3ac7d437f9d8a0
MD5 527731ab8ef68669edc0f369d3f3db31
BLAKE2b-256 d91a13f2a2af9892d582bb3da663330c197c224f1c25a335e0570f096ebda833

See more details on using hashes here.

Release history Release notifications | RSS feed

1.0.2

2 files

1.0.1

2 files

This release

1.0.0 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page