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
    
  • overlap: Given two subnets two subnets will return True if they overlap or are identical. Will return False if the subnets don't overlap.

      overlap('172.16.0.0/12', '172.16.0.0/16')
      True
      overlap('172.16.0.0/12', '172.10.0.0/16')
      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.1.tar.gz (14.6 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.1-py3-none-any.whl (16.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for binip-1.0.1.tar.gz
Algorithm Hash digest
SHA256 cf61ec878054760c7064f2025e8c41b1a2dbab2730f6062f921b576012ddbc1c
MD5 88711d4416ded94294087548fcb3e866
BLAKE2b-256 8225138a550d003b4c622c2b8e115411034c1628377050f71f37e7158cf92198

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for binip-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 edc3c2b54136280dc3b494a283a31b42c6618373cf202d51b0258fc2dd2a5eb4
MD5 cb7fabbfdc1792362a54d272235b7014
BLAKE2b-256 ed3a377df53ff0e81ab2487dcb5f6d75f0fd48b78814537d4c0a1cd2a3aa5320

See more details on using hashes here.

Release history Release notifications | RSS feed

1.0.2

2 files

This release

1.0.1 This release

2 files

1.0.0

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